|
| 1 | +--- |
| 2 | +intro: Introducing Links |
| 3 | +shortTitle: Introducing Links |
| 4 | +title: Introducing Links |
| 5 | +tags: |
| 6 | + - new |
| 7 | +--- |
| 8 | + |
| 9 | +# Introducing Links |
| 10 | + |
| 11 | +## What is a Link? |
| 12 | + |
| 13 | +RapidAPI consists of several API resources that enable you to create an end-to-end booking experience for your |
| 14 | +travelers. |
| 15 | +This means you'll use these resources in sequence to build complete transactions, such as **shopping**, |
| 16 | +**price checking**, **booking**, and **managing bookings**. |
| 17 | + |
| 18 | +To streamline these multistep transactions, RapidAPI employs a powerful tool called `Link`. |
| 19 | + |
| 20 | +A `Link` is a reference to a related resource, acting as a guide that directs developers to the next relevant API |
| 21 | +resource needed to seamlessly progress through the transaction. |
| 22 | + |
| 23 | +By leveraging Links from previous operations within the Rapid SDK, developers can swiftly create and execute new |
| 24 | +operations, bypassing the typical setup process and significantly enhancing efficiency. |
| 25 | + |
| 26 | +{% messageCard type="info" %} |
| 27 | +{% messageCardHeader %} |
| 28 | +Note |
| 29 | +{% /messageCardHeader %} |
| 30 | +This feature is available in the `rapid-sdk` v4.3.0 and later. |
| 31 | +{% /messageCard %} |
| 32 | + |
| 33 | +## Why to use a Link? |
| 34 | + |
| 35 | +`Link` is a convenient way to navigate through the Rapid API operations, without having to manually create the next |
| 36 | +operation. You can extract a `Link` from the response of the previous operation and use it to create the next operation. |
| 37 | + |
| 38 | +A link will benefit you in the following ways: |
| 39 | + |
| 40 | +- **Saves time:** You don't have to manually create the next operation by setting up the request parameters and the |
| 41 | + operation. |
| 42 | +- **Simplifies the process:** You can easily navigate through the Rapid API operations by using the `Link` from the |
| 43 | + previous operation, without having to extract the needed information manually. |
| 44 | +- **Reduces errors:** By using the `Link`, you can avoid errors that might occur when manually creating the next |
| 45 | + operation. |
| 46 | + |
| 47 | +## How to use a Link? |
| 48 | + |
| 49 | +To get a `Link`, you need to extract it from the previous `Operation` response. |
| 50 | +Then, you can create the next `Operation` from the `Link` and execute it with the `RapidClient`. |
| 51 | + |
| 52 | +For example, you can create a `Link` from the response of a `GetAvailabilityOperation` and use it in creating |
| 53 | +a `PriceCheckOperation`: |
| 54 | + |
| 55 | +```java |
| 56 | +// 1. Create and execute the GetAvailabilityOperation (The first operation) |
| 57 | +GetAvailabilityOperation getAvailabilityOperation = new GetAvailabilityOperation(getAvailabilityOperationParams); |
| 58 | +Response<List<Property>> propertiesResponse = rapidClient.execute(getAvailabilityOperation); |
| 59 | + |
| 60 | +// 2a. Select the needed property from the response (Here, we select the first property) |
| 61 | +Property property = propertiesResponse.getData().get(0); |
| 62 | + |
| 63 | +// 2b. Make sure the property is an instance of PropertyAvailability |
| 64 | +if (!(property instanceof PropertyAvailability)) { |
| 65 | + return; |
| 66 | +} |
| 67 | + |
| 68 | +PropertyAvailability propertyAvailability = (PropertyAvailability) property; |
| 69 | + |
| 70 | +// 3a. Extract the priceCheck link from PropertyAvailability operation (Here, we select the first rate for the first room, then get the priceCheck link) |
| 71 | +Link priceCheckLink = propertyAvailability.getRooms().get(0).getRates().get(0).getBedGroups().entrySet().stream().findFirst().get().getValue().getLinks().getPriceCheck(); |
| 72 | + |
| 73 | +// 3b. Create the needed context for the PriceCheckOperation |
| 74 | +PriceCheckOperationContext priceCheckOperationContext = PriceCheckOperationContext.builder().customerIp("1.2.3.4").build(); // fill the context as needed |
| 75 | + |
| 76 | +// 4. Create and execute the PriceCheckOperation using the Link |
| 77 | +PriceCheckOperation priceCheckOperation = new PriceCheckOperation(priceCheckLink, priceCheckOperationContext); |
| 78 | +Response<RoomPriceCheck> roomPriceCheckResponse = rapidClient.execute(priceCheckOperation); |
| 79 | +// ... |
| 80 | +``` |
| 81 | + |
| 82 | +Another example would be to create a `Link` from the response of a `PriceCheckOperation` and use it in creating a |
| 83 | +booking. |
| 84 | + |
| 85 | +```java |
| 86 | +// 1. Get the RoomPriceCheck from the previous step |
| 87 | +RoomPriceCheck roomPriceCheck = roomPriceCheckResponse.getData(); // from the previous step |
| 88 | + |
| 89 | +// 2a. Extract the Link from the RoomPriceCheck |
| 90 | +Link postItineraryLink = roomPriceCheck.getLinks().getBook(); |
| 91 | + |
| 92 | +// 2b. Create the needed context for the PostItineraryOperation |
| 93 | +PostItineraryOperationContext postItineraryOperationContext = PostItineraryOperationContext.builder().customerIp("1.2.3.4").build(); // fill the context as needed |
| 94 | + |
| 95 | +// 2c. Create the CreateItineraryRequest |
| 96 | +CreateItineraryRequest createItineraryRequest = CreateItineraryRequest.builder().build(); // fill the request as needed |
| 97 | + |
| 98 | +// 3. Create and execute the PostItineraryOperation using the Link |
| 99 | +PostItineraryOperation postItineraryOperation = new PostItineraryOperation(postItineraryLink, postItineraryOperationContext, createItineraryRequest); |
| 100 | + |
| 101 | +// 4. Execute the PostItineraryOperation |
| 102 | +Response<ItineraryCreation> itineraryCreationResponse = rapidClient.execute(postItineraryOperation); |
| 103 | +ItineraryCreation itineraryCreation = itineraryCreationResponse.getData(); |
| 104 | +``` |
| 105 | + |
| 106 | +For more examples on how to use `Link`, see the [Usage Examples](products/rapid/sdk/java/usage-examples) section. |
| 107 | + |
0 commit comments