使用連結
簡介
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();
有關如何使用連結的更多範例,請參閱使用範例部分。