从 R 调用 Amadeus flight-offers-pricing API?

Call Amadeus flight-offers-pricing API from R?

更新: 这是显示如何获取访问令牌的代码。我也在这里使用免费的测试 api(不需要信用卡)。

显示第一个 api 调用 test.api.amadeus。com/v2/shopping/flight-offers。

这是对test.api.amadeus的第二次api调用。com/v1/shopping/flight-offers/pricing api我不知道如何格式化。

我的问题仍然存在,使用 R 调用第二个 API 的正确方法是什么?

R 脚本

library("tidyverse")
library("httr")
library("rjson")

amadeus_api_key_prod <- Sys.getenv("AMADEUS_API_KEY") 
amadeus_api_secret_prod <- Sys.getenv("AMADEUS_SECRET") 

# Initialize variables
tmp_origin <- NULL
tmp_dest <- NULL
tmp_avg_total_fare <- NULL

# Get Token
response <- POST("https://test.api.amadeus.com/v1/security/oauth2/token",
                 add_headers("Content-Type" = "application/x-www-form-urlencoded"),
                 body = list(
                   "grant_type"     = "client_credentials",
                   "client_id"      = amadeus_api_key_prod,
                   "client_secret"  = amadeus_api_secret_prod),
                 encode = "form")
response
rsp_content <- content(response, as = "parsed", type = "application/json")
access_token <- paste0("Bearer ", rsp_content$access_token)

origin <- "JFK"
dest <- "LHR"
dep_date <- "2021-12-01"
return_date <- "2021-12-18"
max_num_flights <- 1

url <- paste0("https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=", 
              origin, 
              "&destinationLocationCode=",
              dest,
              "&departureDate=",
              dep_date,
              "&returnDate=",
              return_date,
              "&max=",
              max_num_flights,
              "&adults=1&nonStop=false&travelClass=ECONOMY&max=1&currencyCode=CAD")

# Get flight info
response <- GET(url,
                add_headers("Content-Type" = "application/x-www-form-urlencoded",
                            "Authorization" = access_token),
                encode = "form")

response
rsp_content <- content(response, as = "parsed", type = "application/json")
rsp_content

# Get current, more detailed flight info
# This is the part I do not know how to do
url2 <- "https://test.api.amadeus.com/v1/shopping/flight-offers/pricing"

flt_info <- toJSON(rsp_content[["data"]])

response2 <- GET(url2,
                 add_headers("Authorization" = access_token),
                 body = list(
                   "priceFlightOffersBody" = flt_info
                 ),
                 encode = "form")

response2
rsp_content2 <- content(response2, as = "parsed", type = "application/json")
rsp_content2

原问题

我正在使用 Amadeus 航班信息 api 检索航班价格。 我的理解是要获得完整的价格信息需要两个步骤。

  1. 致电https://api.amadeus.com/v2/shopping/flight-offersapi
  2. 致电https://api.amadeus.com/v1/shopping/flight-offers/pricingapi

我可以成功执行第一个 api 调用,包含起点、目的地、日期等。 第二个电话确认价格仍然可用,并且比第一个 api 电话有更详细的票价明细。我最感兴趣的就是这个详细的分解。

我无法理解从第一个 api 调用返回的哪些信息需要传递给第二个 api 调用以及以什么格式传递。

下面,我包含了从第一个 api 调用返回的数据结构和我调用第二个 api.

失败的尝试

使用 R 调用第二个 API 的正确方法是什么?

我认为是相关文档的链接:

# Data structure returned from call to https://api.amadeus.com/v2/shopping/flight-offers
# For YYZ to YOW return, Dec 1-18 economy
rsp_content <- list(meta = list(count = 1L, links = list(self = "https://api.amadeus.com/v2/shopping/flight-offers?originLocationCode=YYZ&destinationLocationCode=YOW&departureDate=2021-12-01&returnDate=2021-12-18&max=1&adults=1&nonStop=false&travelClass=ECONOMY&max=1&currencyCode=CAD")), 
                    data = list(list(type = "flight-offer", id = "1", source = "GDS", 
                                     instantTicketingRequired = FALSE, nonHomogeneous = FALSE, 
                                     oneWay = FALSE, lastTicketingDate = "2021-08-07", numberOfBookableSeats = 7L, 
                                     itineraries = list(list(duration = "PT1H10M", segments = list(
                                       list(departure = list(iataCode = "YYZ", terminal = "3", 
                                                             at = "2021-12-01T11:00:00"), arrival = list(iataCode = "YOW", 
                                                                                                         at = "2021-12-01T12:10:00"), carrierCode = "WS", 
                                            number = "3462", aircraft = list(code = "DH4"), 
                                            duration = "PT1H10M", id = "1", numberOfStops = 0L, 
                                            blacklistedInEU = FALSE))), list(duration = "PT1H21M", 
                                                                             segments = list(list(departure = list(iataCode = "YOW", 
                                                                                                                   at = "2021-12-18T10:45:00"), arrival = list(iataCode = "YYZ", 
                                                                                                                                                               terminal = "3", at = "2021-12-18T12:06:00"), 
                                                                                                  carrierCode = "WS", number = "3463", aircraft = list(
                                                                                                    code = "DH4"), duration = "PT1H21M", id = "2", 
                                                                                                  numberOfStops = 0L, blacklistedInEU = FALSE)))), 
                                     price = list(currency = "CAD", total = "232.78", base = "125.00", 
                                                  fees = list(list(amount = "0.00", type = "SUPPLIER"), 
                                                              list(amount = "0.00", type = "TICKETING")), grandTotal = "232.78"), 
                                     pricingOptions = list(fareType = list("PUBLISHED"), includedCheckedBagsOnly = FALSE), 
                                     validatingAirlineCodes = list("WS"), travelerPricings = list(
                                       list(travelerId = "1", fareOption = "STANDARD", travelerType = "ADULT", 
                                            price = list(currency = "CAD", total = "232.78", 
                                                         base = "125.00"), fareDetailsBySegment = list(
                                                           list(segmentId = "1", cabin = "ECONOMY", fareBasis = "LAVD0TBJ", 
                                                                brandedFare = "BASIC", class = "E", includedCheckedBags = list(
                                                                  quantity = 0L)), list(segmentId = "2", 
                                                                                        cabin = "ECONOMY", fareBasis = "LAVD0ZBI", 
                                                                                        brandedFare = "BASIC", class = "E", includedCheckedBags = list(
                                                                                          quantity = 0L))))))), dictionaries = list(
                                                                                            locations = list(YOW = list(cityCode = "YOW", countryCode = "CA"), 
                                                                                                             YYZ = list(cityCode = "YTO", countryCode = "CA")), 
                                                                                            aircraft = list(DH4 = "DE HAVILLAND DHC-8 400 SERIES"), 
                                                                                            currencies = list(CAD = "CANADIAN DOLLAR"), carriers = list(
                                                                                              WS = "WESTJET")))


# Get full pricing info
url2 <- "https://api.amadeus.com/v1/shopping/flight-offers/pricing"

# Get pricing info
response2 <- GET(url2,
                add_headers("Authorization" = access_token),
                body = list(
                  "priceFlightOffersBody" = rsp_content[["data"]][[1]]
                ),
                encode = "form")

response2
rsp_content2 <- content(response2, as = "parsed", type = "application/json")
rsp_content2

您可以看看这个 blog article,它解释了数据需要如何在 3 个端点之间传递,并且在 Postman 上有一个视频显示它。

您可以查看 Amadeus for Developers 团队构建的一些代码示例。这里为 Flight Offers Price (in different programming languages but not R) and here for Flight Create Orders(包括前面的搜索和价格步骤)。

他们还有几个演示应用程序,向您展示如何组合这些端点来构建航班预订引擎,其中一个在 Python 中,您可以找到 here

对于第二个请求,它似乎适用于以下内容。

变化:

  • 使用POST,而不是GET
  • 虽然documentation命名bodypriceFlightOffersBody,但实际上应该只是数据部分,不需要封装在另一个列表中。
  • 数据部分与第一次请求的结果不完全相同
  • 将数据部分作为 R 列表传递并使用 POST 参数 encode = "json"
url2 <- "https://test.api.amadeus.com/v1/shopping/flight-offers/pricing"

flt_info <- list(data = list(type = "flight-offers-pricing",
                             flightOffers = rsp_content$data))
response2 <- POST(url2,
                  add_headers(Authorization = access_token),
                  body = flt_info,
                  encode = "json")

rsp_content2 <- content(response2, as = "parsed", type = "application/json")
rsp_content2