Amadeus Travel API的Python模块
amadeus的Python项目详细描述
Amadeus为旅游业提供了一套API。航班,酒店, 地点等等。
有关更多详细信息,请参见github上的Amadeus for Developers Portal和class reference。
安装
此sdk需要python 2.7+或3.4+。你可以直接用pip安装它。
pip install amadeus
您还可以将其添加到requirements.txt文件中,并使用以下命令安装:
pip install -r requirements.txt
开始
要进行第一次api调用,您需要register for an Amadeus Developer Account并设置第一次 申请。
fromamadeusimportClient,ResponseErroramadeus=Client(client_id='REPLACE_BY_YOUR_API_KEY',client_secret='REPLACE_BY_YOUR_API_SECRET')try:response=amadeus.reference_data.urls.checkin_links.get(airlineCode='BA')print(response.data)exceptResponseErroraserror:print(error)
初始化
客户端可以直接初始化。
# Initialize using parametersamadeus=Client(client_id='REPLACE_BY_YOUR_API_KEY',client_secret='REPLACE_BY_YOUR_API_SECRET')
或者,如果 环境变量AMADEUS_CLIENT_ID和 AMADEUS_CLIENT_SECRET存在。
amadeus=Client()
您的凭据可以在Amadeus dashboard上找到。Sign up为今天的帐户。
默认情况下,sdk的环境是test环境。到 切换到生产(付费)环境请切换主机名 如下所示:
amadeus=Client(hostname='production')
进行API调用
这个库方便地将每个api路径映射到类似的路径。
例如,GET /v2/reference-data/urls/checkin-links?airlineCode=BA 将是:
amadeus.reference_data.urls.checkin_links.get(airlineCode='BA')
类似地,要按id选择资源,可以将id传递给 奇异路径。
例如,GET /v2/shopping/hotel-offers/XZY将是:
amadeus.shopping.hotel_offer('4BA070CE929E135B3268A9F2D0C51E9D4A6CF318BA10485322FA2C7E78C7852E').get()
也可以直接使用^{tt6}进行任意api调用。$ 方法:
amadeus.get('/v2/reference-data/urls/checkin-links',airlineCode='BA')
响应
每个api调用都返回一个Response对象。如果包含的api调用 json响应它将json解析为.result属性。 如果此数据还包含一个data键,它将使该键可用 作为.data属性。反应的原始主体总是 可用作.body属性。
fromamadeusimportLocationresponse=amadeus.reference_data.locations.get(keyword='LON',subType=Location.ANY)print(response.body)#=> The raw response, as a stringprint(response.result)#=> The body parsed as JSON, if the result was parsableprint(response.data)#=> The list of locations, extracted from the JSON
分页
如果api端点支持分页,则其他页面可用 在.next、.previous、.last和.first方法下。
fromamadeusimportLocationresponse=amadeus.reference_data.locations.get(keyword='LON',subType=Location.ANY)amadeus.next(response)#=> returns a new response for the next page
如果页面不可用,该方法将返回None。
日志记录和调试
sdk使添加自己的记录器变得容易。
importlogginglogger=logging.getLogger('your_logger')logger.setLevel(logging.DEBUG)amadeus=Client(client_id='REPLACE_BY_YOUR_API_KEY',client_secret='REPLACE_BY_YOUR_API_SECRET',logger=logger)
另外,要启用更详细的日志记录,可以设置 在您自己的记录器上设置适当的级别,尽管最简单的方法是 在初始化时通过参数启用调试,或使用 AMADEUS_LOG_LEVEL环境变量。
amadeus=Client(client_id='REPLACE_BY_YOUR_API_KEY',client_secret='REPLACE_BY_YOUR_API_SECRET',log_level='debug')
支持的终结点列表
# Flight Inspiration Searchamadeus.shopping.flight_destinations.get(origin='MAD')# Flight Cheapest Date Searchamadeus.shopping.flight_dates.get(origin='MAD',destination='MUC')# Flight Low-fare Searchamadeus.shopping.flight_offers.get(origin='MAD',destination='NYC',departureDate='2019-08-01')# Flight Checkin Linksamadeus.reference_data.urls.checkin_links.get(airlineCode='BA')# Airline Code Lookupamadeus.reference_data.airlines.get(airlineCodes='U2')# Airport and City Search (autocomplete)# Find all the cities and airports starting by 'LON'amadeus.reference_data.locations.get(keyword='LON',subType=Location.ANY)# Get a specific city or airport based on its idamadeus.reference_data.location('ALHR').get()# Airport Nearest Relevant Airport (for London)amadeus.reference_data.locations.airports.get(longitude=0.1278,latitude=51.5074)# Flight Most Searched Destinations# Which were the most searched flight destinations from Madrid in August 2017?amadeus.travel.analytics.air_traffic.searched.get(originCityCode='MAD',marketCountryCode='ES',searchPeriod='2017-08')# How many people in Spain searched for a trip from Madrid to New-York in September 2017?amadeus.travel.analytics.air_traffic.searched_by_destination.get(originCityCode='MAD',destinationCityCode='NYC',marketCountryCode='ES',searchPeriod='2017-08')# Flight Most Booked Destinationsamadeus.travel.analytics.air_traffic.booked.get(originCityCode='MAD',period='2017-08')# Flight Most Traveled Destinationsamadeus.travel.analytics.air_traffic.traveled.get(originCityCode='MAD',period='2017-01')# Flight Busiest Travel Periodamadeus.travel.analytics.air_traffic.busiest_period.get(cityCode='MAD',period='2017',direction='ARRIVING')# Hotel Search# Get list of Hotels by city codeamadeus.shopping.hotel_offers.get(cityCode='LON')# Get list of offers for a specific hotelamadeus.shopping.hotel_offers_by_hotel.get(hotelId='IALONCHO')# Confirm the availability of a specific offeramadeus.shopping.hotel_offer('D5BEE9D0D08B6678C2F5FAD910DC110BCDA187D21D4FCE68ED423426D0A246BB').get()# Point of Interest# What are the popular places in Barcelona (based a geo location and a radius)amadeus.reference_data.locations.points_of_interest.get(latitude=41.397158,longitude=2.160873)# What are the popular places in Barcelona? (based on a square)amadeus.reference_data.locations.points_of_interest.by_square.get(north=41.397158,west=2.160873,south=41.394582,east=2.177181)
开发和贡献
想做贡献吗?请阅读我们的Contributors Guide以获取有关安装和 在开发环境中运行此代码。
许可证
这个库是在MIT License下发布的。
帮助
我们的developer support team在这里 帮助你。你可以在StackOverflow和email找到我们。