gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:04:09 25 4
gpt4 key购买 nike

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

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

这是对 test.api.amadeus.com/v1/shopping/flight-offers/pricing api 的第二次 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-offers接口(interface)
  2. 调用 https://api.amadeus.com/v1/shopping/flight-offers/pricing接口(interface)

我可以成功执行第一个 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 (使用不同的编程语言,但不是 R)这里是 Flight Create Orders (包括前面的搜索和定价步骤)。

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

关于r - 从 R 调用 Amadeus flight-offers-pricing API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68675873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com