Skip to content

Commit

Permalink
feat: timeout override
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdev01 committed Apr 7, 2024
1 parent 21ad216 commit aafdafc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
12 changes: 12 additions & 0 deletions docs/extra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Sandbox
You can use the sandbox environment to test your integration. To do this, you need to call the `initSandbox` method before making any requests.
```java
Asaas.initSandbox("your_api_key");
```

## Timeout

The default timeout is 30000 milliseconds. You can change it by calling the `setTimeout` method.
```java
Asaas.setTimeout(10000);
```
11 changes: 10 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ The recommended way to use the AWS SDK for Java in your project is to consume it
</dependency>
```

Before making any requests, you need to call the `init` method using your API key.
```java
Asaas.init("your_api_key");
```

The API official documentation can be found [here](https://docs.asaas.com/docs/visao-geral)

# Documentation
Expand All @@ -32,4 +37,8 @@ The API official documentation can be found [here](https://docs.asaas.com/docs/v
- [Notificações](notification.md)
- [Nota fiscal](invoice.md)
- [Assinaturas](subscription.md)
- [Webhooks](webhook.md)
- [Webhooks](webhook.md)

## Extra

- [Configurações extras](extra.md)
9 changes: 1 addition & 8 deletions src/main/java/io/github/jpdev/asaassdk/doc/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ public class Examples {

public static void main(String[] args) {
Asaas.init(Secret.getAccessToken());

Webhook configuredWebhook = Webhook.creator()
.setName("Teste")
.setUrl("https://www.exemplo.com/webhook/asaas")
.setEmail("[email protected]")
.setSendType(SendType.SEQUENTIALLY)
.addEvent(Event.PAYMENT_RECEIVED)
.create();
Asaas.setTimeout(10000);
}

private void pixTransaction() {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/github/jpdev/asaassdk/http/ApacheHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

Expand All @@ -20,9 +22,19 @@ public class ApacheHttpClient {
private final CloseableHttpClient httpclient;
private final static String ACCESS_TOKEN_HEADER = "access_token";

public ApacheHttpClient(String acessToken) {
public ApacheHttpClient(String acessToken, int timeout) {
this.accessToken = acessToken;
httpclient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();

CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.build();

httpclient = httpClient;
}

public Response get(String url) throws ConnectionException {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/github/jpdev/asaassdk/http/Asaas.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Asaas {
private static final String ENDPOINT_SANDBOX = "https://sandbox.asaas.com/api/v3";

private static String token;
private static Integer timeout;

public static AsaasRestClient restClient;

Expand All @@ -24,6 +25,10 @@ public static void setToken(String token) {
Asaas.token = token;
}

public static void setTimeout(Integer timeout) {
Asaas.timeout = timeout;
}

public static AsaasRestClient getRestClient() {
if (Asaas.restClient == null) {
synchronized (Asaas.class) {
Expand All @@ -47,7 +52,7 @@ private static AsaasRestClient buildRestClient() {
);
}

AsaasRestClient.Builder builder = new AsaasRestClient.Builder(Asaas.token);
AsaasRestClient.Builder builder = new AsaasRestClient.Builder(Asaas.token, Asaas.timeout);

return builder.build();
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/github/jpdev/asaassdk/http/AsaasRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class AsaasRestClient {
private final ApacheHttpClient client;
private final List<String> userAgentExtensions;

private final static int DEFAULT_TIMEOUT = 30000;

public ObjectMapper getObjectMapper() {
return objectMapper;
}
Expand Down Expand Up @@ -65,18 +67,25 @@ public Response delete(String url, String body) {

public static class Builder {
private String token;
private Integer timeout;

private ApacheHttpClient httpClient;
private List<String> userAgentExtensions;

public Builder(final String token) {
public Builder(final String token, final Integer timeout) {
this.token = token;
if (timeout != null) {
this.timeout = timeout;
} else {
this.timeout = DEFAULT_TIMEOUT;
}
}


public AsaasRestClient build() {
if (this.httpClient == null) {
this.httpClient = new ApacheHttpClient(this.token);
if (this.timeout == null) this.timeout = DEFAULT_TIMEOUT;
this.httpClient = new ApacheHttpClient(this.token, this.timeout);
}
return new AsaasRestClient(this);
}
Expand Down

0 comments on commit aafdafc

Please sign in to comment.