Sometimes when I run this code it throws a Key error for "data", other times (without changing anything) it retrieves the data.
Heres the code for the FlightSearch class, I removed my API Key and Secret
from flight_search import FlightSearch
flight_search_One = FlightSearch()
JSON = flight_search_One.flight_JSON("PAR")
print(JSON["data"])I'm running it on Pycharm. The flight_search_One.fligh_JSON function makes an API get request to amedeus and returns a JSON for flight offers.Heres the code for the FlightSearch class, I removed my API Key and Secret
import requests
from datetime import datetime, timedelta
class FlightSearch:
#This class is responsible for talking to the Flight Search API.\
def __init__(self):
self.API_Key = "NA"
self.API_Secret = "NA"
self.Auth_Data = data = {"grant_type": "client_credentials",
"client_id" : self.API_Key,
"client_secret" : self.API_Secret}
self.Auth_Token_Endpoint = "https://test.api.amadeus.com/v1/security/oauth2/token"
self.Auth_Token_Header = {"Content-Type": "application/x-www-form-urlencoded"}
def get_token(self):
response = requests.post(url=self.Auth_Token_Endpoint, headers=self.Auth_Token_Header, data= self.Auth_Data)
access_token = response.json()['access_token']
return access_token
def flight_JSON(self, iata):
dt = datetime.now()
td = timedelta(days=100)
future = dt + td
departure_date = future.date()
API_Endpoint = "https://test.api.amadeus.com/v2/shopping/flight-offers"
params = {
"originLocationCode": "YYZ",
"destinationLocationCode": iata,
"departureDate": departure_date,
"adults": 2,
"maxPrice" : 1000,
# "currencyCode" : "EUR"
}
headers = {
"Authorization": f"Bearer {self.get_token()}"
}
response = requests.get(url=API_Endpoint, headers=headers, params=params)
JSON = response.json()
return JSON
