"""Travlix API v1 - signed request in Python 3 (requests). Usage: TLX_KEY=... TLX_SECRET=... python3 sign.py""" import datetime, hashlib, hmac, json, os, secrets, time from urllib.parse import urlsplit import requests BASE = os.environ.get("TLX_BASE", "https://api.travlix.com") KEY, SECRET = os.environ["TLX_KEY"], os.environ["TLX_SECRET"] def tlx_call(method, path, body=None, headers=None): raw = b"" if body is None else json.dumps(body, separators=(",", ":")).encode() u = urlsplit(path) ts, nonce = str(int(time.time())), secrets.token_hex(16) sts = "\n".join([method.upper(), u.path, u.query, ts, nonce, hashlib.sha256(raw).hexdigest()]) sig = hmac.new(SECRET.encode(), sts.encode(), hashlib.sha256).hexdigest() h = {"Content-Type": "application/json", "X-Api-Key": KEY, "X-Timestamp": ts, "X-Nonce": nonce, "X-Signature": sig} h.update(headers or {}) r = requests.request(method, BASE + path, data=raw if method != "GET" else None, headers=h, timeout=90) return r.status_code, r.json() if __name__ == "__main__": print("balance", *tlx_call("GET", "/v1/wallet/balance")) date = (datetime.date.today() + datetime.timedelta(days=30)).isoformat() code, r = tlx_call("POST", "/v1/flight/search", {"origin": "DEL", "destination": "BOM", "departure_date": date, "adults": 1}) print("search", code, r.get("data", {}).get("count"), "offers") if r.get("data", {}).get("offers"): code, q = tlx_call("POST", "/v1/flight/fare-quote", {"offer_id": r["data"]["offers"][0]["offer_id"]}) print("fare-quote", code, q.get("data", {}).get("price", {}).get("total"))