当前位置: 首页 > 知识库问答 >
问题:

调用Amadeus flight提供R的定价API?

鲁杜吟
2023-03-14

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

测试的第一个api调用。应用程序编程接口。阿马迪斯。显示com/v2/shopping/flight优惠。

这是我不知道如何格式化test.api.amadeus.com/v1/shopping/flight-offers/pricingapi的第二个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

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

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

下面,我包含了从第一个api调用返回的数据结构以及我调用第二个api的失败尝试。

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

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

  • https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-price/api-reference
# 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

共有2个答案

壤驷华美
2023-03-14

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

变化:

  • 使用POST,而不是GET
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
陆俭
2023-03-14

你可以看看这篇博客文章,它解释了数据需要如何在3个endpoint之间传递,它有一个视频在Postman上显示。

您可以检查Amadeus for Developers团队构建的一些代码示例。这里是航班报价价格(使用不同的编程语言,但不是R),这里是航班创建订单(包括之前的搜索和价格步骤)。

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

 类似资料:
  • 我们正在使用flight提供的搜索api,并准备转移到prod。但当我们搜索经济舱航班时,价格甚至不接近“amadeus.net”搜索引擎结果或TK(土耳其航空公司)网站价格。如果我们使用BUSINESS class作为参数,api结果更接近实际价格。我们如何解决这个问题? 示例查询为:(IST-CGN 5月25日经济和TK运营航班起飞16:05) /v2/购物/航班优惠?原始位置代码=IST a

  • 在阅读了AWS文档和其他类似问题的答案后,DynamoDB供应容量模型的定价对我来说仍然有些不清楚。 正如AWS文档中所述:“即使您没有充分利用所提供的容量,您也将为您在Amazon DynamoDB表中提供的吞吐能力(读写)付费。DynamoDB表的实际读写性能可能会有所不同,并且可能低于您提供的吞吐能力。” 因此,对于类似问题之一的回答的最后一部分:“最后,使用迪纳摩,你为你保留的容量付费,而

  • 我正在阅读Symfony通过自定义用户提供商管理用户的方法:https://symfony.com/doc/current/security/custom_provider.html 当用户提交用户名和密码时,身份验证层要求配置的用户提供程序返回给定用户名的用户对象。Symfony然后检查此用户的密码是否正确,并生成安全令牌 当我们在本地使用用户/密码连接到数据库时,这非常有效,但是我想对另一个服

  • 我尝试使用以下R语句,并使用NumPy将其转换为Python: 有与which()等价的Python吗?这里,x是矩阵tmp中的行,k对应于另一个矩阵中的列数。 之前,我尝试了以下Python代码,并收到一个值错误(操作数无法与形状一起广播):

  • 问题内容: 我是AngularJS的新手,正在尝试调试一些路由,但是我不知道如何显示/查看传递给routeprovider的路由。 例如,如果我当前的路由设置如下: 调试时,我想破坏代码,并在控制台日志中输入类似内容; 以显示将由“ .when”评估的路线。 问题答案: 您可以收听发出的多个事件。这些事件是: 和, (我鼓励阅读链接提供的文档,以获取每个文件的描述。) 此时,您可以在一个控制器或指

  • 使用指南 - 数据报告 - 访问分析 - 受访页面提供的信息及价值分析 受访页面提供的信息及价值分析 受访页面报告提供了访客对您网站内各个页面的访问情况数据。通过这个报告,您可以获得以下一些信息: 1)访客进入您网站后通常首要访问和次要访问的页面是哪些。 这些页面是访客形成对您网站第一印象的重要页面,对于访客是否继续关注您的网站、以及最终是否选择您的产品或服务起着决定性的作用。您可以从界面美观性、