使用链接
概述
Rapid 由多种 API 资源组成,共同为 旅行者打造端到端的预订体验。您可以按顺序使用这些资源来构建完整的交易,例如购物、 价格查询、预订,以及最后的管理预订。
为了简化这些多步骤交易,Rapid 使用了一种称为链接的强大工具。
链接是对相关资源的引用,作为指导,引导开发人员找到无缝完成交易所需的下一个相关 API 资源。
通过利用链接,开发人员可以快速创建和执行新操作,绕过典型的设置过程并显著提高效率。
使用链接的好处
链接是浏览快速 API 操作的便捷方式,无需手动创建下一个 操作。链接在每个响应的 links
部分中定义。您可以从上一个操作的响应中提取一个链接并使用它来创建下一个操作。
使用链接有以下好处:
- **节省时间:**您不必通过设置请求参数和操作来手动创建下一个操作。
- **简化流程:**使用上一个操作的链接轻松浏览快速 API 操作,而无需手动提取所需的信息。
- **减少错误:**通过使用链接,您可以避免手动创建下一个 操作时可能发生的错误。
如何使用链接?
在操作响应中,查找返回的 links
部分,然后使用链接构建下一个合适的操作。 然后,您可以从 operation
创建下一个 link
,并使用 RapidClient
执行它。
例如,您可以通过 GetAvailabilityOperation
的响应创建 PriceCheckOperationLink
,并将其用于创建 PriceCheckOperation
:
// 1. Create and execute the GetAvailabilityOperation (The first operation)
GetAvailabilityOperation getAvailabilityOperation = new GetAvailabilityOperation(getAvailabilityOperationParams);
Response<List<Property>> propertiesResponse = rapidClient.execute(getAvailabilityOperation);
// 2a. Select the needed property from the response (Here, we select the first property)
Property property = propertiesResponse.getData().get(0);
// 2b. Make sure the property is an instance of PropertyAvailability
if (!(property instanceof PropertyAvailability)) {
return;
}
PropertyAvailability propertyAvailability = (PropertyAvailability) property;
// 3a. Extract the PriceCheckOperationLink from PropertyAvailability operation (Here, we select the first rate for the first room, then get the PriceCheckOperationLink)
PriceCheckOperationLink priceCheckLink = propertyAvailability.getRooms().get(0).getRates().get(0).getBedGroups().entrySet().stream().findFirst().get().getValue().getLinks().getPriceCheck();
// 3b. Create the needed context for the PriceCheckOperation
PriceCheckOperationContext priceCheckOperationContext = PriceCheckOperationContext.builder().customerIp("1.2.3.4").build(); // fill the context as needed
// 4. Create and execute the PriceCheckOperation using the Link
PriceCheckOperation priceCheckOperation = new PriceCheckOperation(priceCheckLink, priceCheckOperationContext);
Response<RoomPriceCheck> roomPriceCheckResponse = rapidClient.execute(priceCheckOperation);
// ...
另一个示例是,您可以通过 PriceCheckOperation
的响应创建 PostItineraryOperationLink
,并将其用于创建预订。
// 1. Get the RoomPriceCheck from the previous step
RoomPriceCheck roomPriceCheck = roomPriceCheckResponse.getData(); // from the previous step
// 2a. Extract the Link from the RoomPriceCheck
PostItineraryOperationLink postItineraryLink = roomPriceCheck.getLinks().getBook();
// 2b. Create the needed context for the PostItineraryOperation
PostItineraryOperationContext postItineraryOperationContext = PostItineraryOperationContext.builder().customerIp("1.2.3.4").build(); // fill the context as needed
// 2c. Create the CreateItineraryRequest
CreateItineraryRequest createItineraryRequest = CreateItineraryRequest.builder().build(); // fill the request as needed
// 3. Create and execute the PostItineraryOperation using the Link
PostItineraryOperation postItineraryOperation = new PostItineraryOperation(postItineraryLink, postItineraryOperationContext, createItineraryRequest);
// 4. Execute the PostItineraryOperation
Response<ItineraryCreation> itineraryCreationResponse = rapidClient.execute(postItineraryOperation);
ItineraryCreation itineraryCreation = itineraryCreationResponse.getData();
有关如何使用链接的更多示例,请参阅使用示例部分。