Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
planmate/clients/__init__.py
ADDED
|
File without changes
|
planmate/clients/amadeus_client.py
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import requests
|
| 3 |
+
from typing import Dict
|
| 4 |
+
from ..config import AMADEUS_BASE, get_amadeus_credentials
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class AmadeusClient:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.client_id, self.client_secret = get_amadeus_credentials()
|
| 10 |
+
self._token = None
|
| 11 |
+
self._exp = 0
|
| 12 |
+
|
| 13 |
+
def _auth(self):
|
| 14 |
+
if self._token and time.time() < self._exp - 10:
|
| 15 |
+
return
|
| 16 |
+
url = f"{AMADEUS_BASE}/v1/security/oauth2/token"
|
| 17 |
+
data = {
|
| 18 |
+
"grant_type": "client_credentials",
|
| 19 |
+
"client_id": self.client_id,
|
| 20 |
+
"client_secret": self.client_secret,
|
| 21 |
+
}
|
| 22 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 23 |
+
r = requests.post(url, data=data, headers=headers, timeout=20)
|
| 24 |
+
r.raise_for_status()
|
| 25 |
+
j = r.json()
|
| 26 |
+
self._token = j["access_token"]
|
| 27 |
+
self._exp = time.time() + int(j.get("expires_in", 1799))
|
| 28 |
+
|
| 29 |
+
def _get(self, path: str, params: Dict):
|
| 30 |
+
self._auth()
|
| 31 |
+
url = f"{AMADEUS_BASE}{path}"
|
| 32 |
+
headers = {"Authorization": f"Bearer {self._token}"}
|
| 33 |
+
r = requests.get(url, params=params, headers=headers, timeout=30)
|
| 34 |
+
if r.status_code == 401:
|
| 35 |
+
# retry once
|
| 36 |
+
self._token = None
|
| 37 |
+
self._auth()
|
| 38 |
+
headers = {"Authorization": f"Bearer {self._token}"}
|
| 39 |
+
r = requests.get(url, params=params, headers=headers, timeout=30)
|
| 40 |
+
r.raise_for_status()
|
| 41 |
+
return r.json()
|
| 42 |
+
|
| 43 |
+
def locations(self, keyword: str, subtypes="CITY,AIRPORT", page_limit=10):
|
| 44 |
+
return self._get(
|
| 45 |
+
"/v1/reference-data/locations",
|
| 46 |
+
{
|
| 47 |
+
"keyword": keyword,
|
| 48 |
+
"subType": subtypes,
|
| 49 |
+
"page[limit]": page_limit,
|
| 50 |
+
},
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
def flight_offers(
|
| 54 |
+
self,
|
| 55 |
+
origin: str,
|
| 56 |
+
dest: str,
|
| 57 |
+
depart: str,
|
| 58 |
+
ret: str,
|
| 59 |
+
adults=1,
|
| 60 |
+
currency="PKR",
|
| 61 |
+
non_stop=False,
|
| 62 |
+
max_results=20,
|
| 63 |
+
):
|
| 64 |
+
params = {
|
| 65 |
+
"originLocationCode": origin,
|
| 66 |
+
"destinationLocationCode": dest,
|
| 67 |
+
"departureDate": depart,
|
| 68 |
+
"adults": adults,
|
| 69 |
+
"currencyCode": currency,
|
| 70 |
+
"max": max_results,
|
| 71 |
+
}
|
| 72 |
+
if ret:
|
| 73 |
+
params["returnDate"] = ret
|
| 74 |
+
if non_stop:
|
| 75 |
+
params["nonStop"] = "true"
|
| 76 |
+
return self._get("/v2/shopping/flight-offers", params)
|