Skip to content

Commit

Permalink
Adding configurable timeout for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
soareswallace committed Feb 13, 2025
1 parent a3fe3fe commit 4378a80
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ And then add the artifact `incognia-api-client` **or** `incognia-api-client-shad
<dependency>
<groupId>com.incognia</groupId>
<artifactId>incognia-api-client</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</dependency>
```
```xml
<dependency>
<groupId>com.incognia</groupId>
<artifactId>incognia-api-client-shaded</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</dependency>
```

Expand All @@ -47,13 +47,13 @@ repositories {
And then add the dependency
```gradle
dependencies {
implementation 'com.incognia:incognia-api-client:3.3.0'
implementation 'com.incognia:incognia-api-client:3.4.0'
}
```
OR
```gradle
dependencies {
implementation 'com.incognia:incognia-api-client-shaded:3.3.0'
implementation 'com.incognia:incognia-api-client-shaded:3.4.0'
}
```

Expand All @@ -71,6 +71,19 @@ IncogniaAPI api = IncogniaAPI.init("your-client-id", "your-client-secret");

This will create a singleton instance of the IncogniaAPI class, which will handle token renewal automatically. You should reuse this instance throughout your application.

The library also allow the users to configure the call timeout themselves. This will give them more control over the expected time response. This can be done by calling the init passing the CustomOptions object as a parameter.


```java
IncogniaAPI api = IncogniaAPI.init(
"your-client-id",
"your-client-secret",
CustomOptions.builder().timeout(20).build()
);
```

If no parameter is passed the library will use the default timeout of 10 seconds of the OkHttp.

After calling `init`, you can get the singleton instance simply calling `IncogniaAPI.instance()`.

#### Dependency Injection integration examples
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.incognia"
version = "3.3.0"
version = "3.4.0"

task createProjectVersionFile {
def projectVersionDir = "$projectDir/src/main/java/com/incognia/api"
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/incognia/api/IncogniaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.incognia.common.exceptions.IncogniaAPIException;
import com.incognia.common.exceptions.IncogniaException;
import com.incognia.common.utils.Asserts;
import com.incognia.common.utils.CustomOptions;
import com.incognia.feedback.FeedbackEvent;
import com.incognia.feedback.FeedbackIdentifiers;
import com.incognia.feedback.PostFeedbackRequestBody;
Expand All @@ -24,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import okhttp3.OkHttpClient;
Expand All @@ -50,18 +52,35 @@ public class IncogniaAPI {
* @param clientId the client id
* @param clientSecret the client secret
*/
IncogniaAPI(String clientId, String clientSecret) {
this(clientId, clientSecret, API_URL);
IncogniaAPI(String clientId, String clientSecret, CustomOptions options) {
this(clientId, clientSecret, options, API_URL);
}

IncogniaAPI(String clientId, String clientSecret, String apiUrl) {
IncogniaAPI(String clientId, String clientSecret, CustomOptions options, String apiUrl) {
Asserts.assertNotEmpty(clientId, "client id");
Asserts.assertNotEmpty(clientSecret, "client secret");
Asserts.assertNotEmpty(apiUrl, "api url");
// TODO (rato): set client timeout
tokenAwareNetworkingClient =
new TokenAwareNetworkingClient(
new OkHttpClient.Builder().build(), apiUrl, clientId, clientSecret);
new OkHttpClient.Builder()
.callTimeout(
Optional.ofNullable(options.getTimeout()).orElse(10000L), TimeUnit.MILLISECONDS)
.build(),
apiUrl,
clientId,
clientSecret);
}

/**
* Initializes a IncogniaAPI singleton instance and returns it
*
* @param clientId the client id
* @param clientSecret the client secret
* @return the singleton instance
*/
public static IncogniaAPI init(String clientId, String clientSecret, CustomOptions options) {
INSTANCE.compareAndSet(null, new IncogniaAPI(clientId, clientSecret, options));
return INSTANCE.get();
}

/**
Expand All @@ -72,7 +91,8 @@ public class IncogniaAPI {
* @return the singleton instance
*/
public static IncogniaAPI init(String clientId, String clientSecret) {
INSTANCE.compareAndSet(null, new IncogniaAPI(clientId, clientSecret));
INSTANCE.compareAndSet(
null, new IncogniaAPI(clientId, clientSecret, CustomOptions.builder().build()));
return INSTANCE.get();
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/incognia/common/utils/CustomOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.incognia.common.utils;

import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class CustomOptions {
Long timeout;
}
17 changes: 13 additions & 4 deletions src/test/java/com/incognia/api/IncogniaAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.incognia.common.ReasonCode;
import com.incognia.common.ReasonSource;
import com.incognia.common.StructuredAddress;
import com.incognia.common.utils.CustomOptions;
import com.incognia.feedback.FeedbackEvent;
import com.incognia.feedback.FeedbackIdentifiers;
import com.incognia.feedback.PostFeedbackRequestBody;
Expand Down Expand Up @@ -56,7 +57,12 @@ class IncogniaAPITest {
@BeforeEach
void setUp() {
mockServer = new MockWebServer();
client = new IncogniaAPI(CLIENT_ID, CLIENT_SECRET, mockServer.url("").toString());
client =
new IncogniaAPI(
CLIENT_ID,
CLIENT_SECRET,
CustomOptions.builder().build(),
mockServer.url("").toString());
}

@AfterEach
Expand All @@ -66,14 +72,17 @@ void tearDown() throws IOException {

@Test
void testInit_shouldReturnASingleton() {
IncogniaAPI instance1 = IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET);
IncogniaAPI instance2 = IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET);
IncogniaAPI instance1 =
IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET, CustomOptions.builder().timeout(10000L).build());
IncogniaAPI instance2 =
IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET, CustomOptions.builder().timeout(10000L).build());
assertThat(instance1).isSameAs(instance2);
}

@Test
void testInstance_shouldReturnASingleton() {
IncogniaAPI instance1 = IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET);
IncogniaAPI instance1 =
IncogniaAPI.init(CLIENT_ID, CLIENT_SECRET, CustomOptions.builder().timeout(10000L).build());
IncogniaAPI instance2 = IncogniaAPI.instance();
assertThat(instance1).isSameAs(instance2);
}
Expand Down

0 comments on commit 4378a80

Please sign in to comment.