使用連結
簡介
Rapid 由多個 API 資源組成,這些資源共同為旅客打造 end-to-end 的預訂體驗。您可以依序使用這些資源來建立完整的交易流程,例如:購物, 比價,預訂,以及最後的管理預訂。
為了簡化這些多步驟的交易流程,Rapid 採用了一種名為「連結」的強大工具。
連 結是對相關資源的引用,其作用如同指引,引導開發人員前往下一個相關的 API 資源, 以便能無縫地完成交易流程。
透過運用連結,開發人員能夠迅速建立並執行新的操作,省去一般的設定流程,並大幅提升效率。
使用連結的好處
連結是一種方便的方式,可讓您在 Rapid API 的各項操作之間進行導航,而無需手動建立下一個 操作。連結定義於每個回應的「links」區段中。您可以從前一項操作的回應中擷取連結,並利用該連結建立下一項操作。
使用連結有以下好處:
- 節省時間: 您無需透過設定請求參數和操作來手動建立下一個操作。
- 簡化流程: 透過前一步驟的連結,可輕鬆瀏覽 Rapid 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();