|
| 1 | +--- |
| 2 | +intro: Introducing Configurable HTTP Client |
| 3 | +shortTitle: Introducing Configurable HTTP Client |
| 4 | +title: Introducing Configurable HTTP Client |
| 5 | +tags: |
| 6 | + - new |
| 7 | +--- |
| 8 | + |
| 9 | +# Introducing Configurable HTTP Client |
| 10 | + |
| 11 | +## What is a Configurable HTTP Client? |
| 12 | + |
| 13 | +The RAPID SDK is built on top of an OkHttpClient that is not open for you to configure and tune. Lately, we've come to |
| 14 | +realize your need to tune and optimize the HTTP client to your needs. So in order to give developers using the RAPID SDK |
| 15 | +more control over the underlying HTTP client of the SDK, we're introducing a new builder which you can use to pass your |
| 16 | +own HTTP Client for the SDK to use internally. |
| 17 | + |
| 18 | +Using this builder, you can build an HTTP client with you own configurations and pass it to the SDK, or even pass a |
| 19 | +client you're already using in your application. |
| 20 | + |
| 21 | +{% messageCard type="info" %} |
| 22 | +{% messageCardHeader %} |
| 23 | +Note |
| 24 | +{% /messageCardHeader %} |
| 25 | +This feature is available in the `rapid-sdk` v5.2.0 and later. |
| 26 | +{% /messageCard %} |
| 27 | + |
| 28 | +## Why use a Configurable HTTP Client? |
| 29 | + |
| 30 | +A configurable HTTP client will benefit you in the following ways: |
| 31 | + |
| 32 | +- **Optimization:** Fine-tune the client for better performance, such as connection pooling, timeouts, and retries. |
| 33 | +- **Integration:** Use an existing HTTP client that is already configured and tested within your application. |
| 34 | + |
| 35 | +## How to configure your HTTP client? |
| 36 | + |
| 37 | +To configure your HTTP client, you need to create an instance of `OkHttpClient` and pass it to the `RapidClient` builder. |
| 38 | + |
| 39 | +#### 1. Create an instance of `OkHttpClient` with your configurations. You may use an existing instance or create a new one. For example: |
| 40 | +```java |
| 41 | +OkHttpClient customClient = new OkHttpClient.Builder() |
| 42 | + .connectTimeout(30, TimeUnit.SECONDS) |
| 43 | + .readTimeout(30, TimeUnit.SECONDS) |
| 44 | + .writeTimeout(30, TimeUnit.SECONDS) |
| 45 | + .build(); |
| 46 | +``` |
| 47 | +For more information on configuring the `OkHttpClient`, refer to the [OkHttp documentation](https://square.github.io/okhttp/). |
| 48 | + |
| 49 | +#### 2. Pass the `OkHttpClient` instance to the `RapidClient` builder `builderWithHttpClient` |
| 50 | +```java |
| 51 | +RapidClient rapidClient = RapidClient.builderWithHttpClient() |
| 52 | + .key("YOUR_API_KEY") |
| 53 | + .secret("YOUR_API_SECRET") |
| 54 | + .okHttpClient(customClient) |
| 55 | + .build(); |
| 56 | +``` |
| 57 | + |
| 58 | +#### 3. Make API Calls: Use the configured rapidClient to make API calls as usual |
| 59 | +```java |
| 60 | +GetAvailabilityOperationParams getAvailabilityOperationParams = GetAvailabilityOperationParams.builder() |
| 61 | + .checkin("YYYY-MM-DD") |
| 62 | + .checkout("YYYY-MM-DD") |
| 63 | + .currency("USD") |
| 64 | + .language("en_US") |
| 65 | + /* ... */ |
| 66 | + .build(); |
| 67 | + |
| 68 | +GetAvailabilityOperation operation = new GetAvailabilityOperation(params); |
| 69 | +Response<List<Property>> response = rapidClient.execute(operation); |
| 70 | +``` |
0 commit comments