Skip to content

Commit

Permalink
feat: http timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Mar 3, 2024
1 parent 178b640 commit e72485c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ Usage of these libraries is wrapped into interfaces and gives possibility to ove

The library entry point is `com.crowdin.client.Client` and this class has additional constructor where you can specify additional configurations (please refer to javadoc).

#### Http timeouts

Library allows you to configure http requests timeout.

```java
import com.crowdin.client.Client;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;

public class Main {

public static void main(String[] args) {
Credentials credentials = new Credentials("token", "organization");
Client client = new Client(credentials, ClientConfig.builder().httpTimeoutMs(5000).build());
}

}
```

## Seeking Assistance

If you find any problems or would like to suggest a feature, please read the [How can I contribute](/CONTRIBUTING.md#how-can-i-contribute) section in our contributing guidelines.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/crowdin/client/Sandbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
import com.crowdin.client.core.http.exceptions.HttpException;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.users.model.User;
Expand All @@ -15,7 +16,7 @@ public static void main(String[] args) throws FileNotFoundException, Interrupted
if (args.length > 0) {
try {
Credentials credentials = new Credentials(args[0], "oliynyk");
var client = new Client(credentials);
var client = new Client(credentials, ClientConfig.builder().httpTimeoutMs(5000).build());
ResponseObject<User> user = client.getUsersApi().getAuthenticatedUser();
System.out.println(user.getData());
} catch (HttpException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/client/core/CrowdinApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CrowdinApi(Credentials credentials, ClientConfig clientConfig) {
? clientConfig.getJsonTransformer() : new JacksonJsonTransformer();
this.httpClient = (clientConfig.getHttpClient() != null)
? clientConfig.getHttpClient()
: new ApacheHttpClient(credentials, jsonTransformer, defaultHeaders, clientConfig.getProxy(), clientConfig.getProxyCreds());
: new ApacheHttpClient(credentials, jsonTransformer, defaultHeaders, clientConfig.getProxy(), clientConfig.getProxyCreds(), clientConfig.getHttpTimeoutMs());
this.clientConfig = clientConfig;
if (credentials.getBaseUrl() != null) {
if (credentials.getBaseUrl().endsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer
.build();
}

public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer, Map<String, ?> defaultHeaders, ClientConfig.Host proxy, ClientConfig.UsernamePasswordCredentials proxyCreds) {
public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer, Map<String, ?> defaultHeaders, ClientConfig.Host proxy, ClientConfig.UsernamePasswordCredentials proxyCreds, Integer timeoutMs) {
this.credentials = credentials;
this.jsonTransformer = jsonTransformer;
this.defaultHeaders = defaultHeaders;
this.proxy = proxy;
this.proxyCreds = proxyCreds;
RequestConfig.Builder requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD);
if (timeoutMs != null) {
requestConfig.setConnectionRequestTimeout(timeoutMs);
requestConfig.setConnectTimeout(timeoutMs);
requestConfig.setSocketTimeout(timeoutMs);
}
this.httpClient = (proxy != null)
? HttpClientBuilder.create()
.setProxy(new HttpHost(proxy.getHost(), proxy.getPort()))
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setDefaultRequestConfig(requestConfig.build())
.setDefaultCredentialsProvider((proxyCreds != null)
? new BasicCredentialsProvider() {{
setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxyCreds.getUsername(), proxyCreds.getPassword()));
Expand All @@ -77,7 +83,7 @@ public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer
.build()
: HttpClientBuilder
.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setDefaultRequestConfig(requestConfig.build())
.build();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/client/core/model/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class ClientConfig {
* Provide custom http client
*/
private HttpClient httpClient;
/**
* Http request timeout in milliseconds
*/
private Integer httpTimeoutMs;
/**
* Provide custom json transformer
*/
Expand Down

0 comments on commit e72485c

Please sign in to comment.