Skip to content

Commit

Permalink
add custom timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Asep Rojali committed Nov 25, 2022
1 parent bcb2445 commit d42e2a9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>avew.github.io</groupId>
<artifactId>retrofit-mask</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<description>Custom retrofit with masking log</description>

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/github/avew/CustomHttpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,22 @@ public class CustomHttpConfig {
private String proxyHost;
private int proxyPort;
private String agent;
@Builder.Default
private CustomTimeout customTimeout = new CustomTimeout();

@Override
public String toString() {
return "CustomHttpConfig{" +
"url='" + url + '\'' +
", proxy=" + proxy +
", proxyAuth=" + proxyAuth +
", proxyUsername='" + proxyUsername + '\'' +
", proxyPassword='" + proxyPassword + '\'' +
", urlSkipProxy=" + urlSkipProxy +
", proxyHost='" + proxyHost + '\'' +
", proxyPort=" + proxyPort +
", agent='" + agent + '\'' +
", customTimeout=" + customTimeout +
'}';
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/github/avew/CustomTimeout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.avew;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.TimeUnit;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CustomTimeout {

@Builder.Default
private long connectTimeout = 1L;
@Builder.Default
private TimeUnit connectTimeoutUnit = TimeUnit.MINUTES;

@Builder.Default
private long readTimeout = 10L;
@Builder.Default
private TimeUnit readTimeoutUnit = TimeUnit.MINUTES;

@Builder.Default
private long writeTimeout = 5L;
@Builder.Default
private TimeUnit writeTimeoutUnit = TimeUnit.MINUTES;
}
7 changes: 3 additions & 4 deletions src/main/java/io/github/avew/OkHttpCustomConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -46,9 +45,9 @@ public Retrofit.Builder builder() throws RuntimeException {

OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder();

httpClient.connectTimeout(5L, TimeUnit.MINUTES);
httpClient.readTimeout(10L, TimeUnit.MINUTES);
httpClient.writeTimeout(5L, TimeUnit.MINUTES);
httpClient.connectTimeout(config.getCustomTimeout().getConnectTimeout(), config.getCustomTimeout().getConnectTimeoutUnit());
httpClient.readTimeout(config.getCustomTimeout().getReadTimeout(), config.getCustomTimeout().getReadTimeoutUnit());
httpClient.writeTimeout(config.getCustomTimeout().getWriteTimeout(), config.getCustomTimeout().getWriteTimeoutUnit());

httpClient.addInterceptor(chain -> {
Request original = chain.request();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/github/avew/OkHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void init() {
.url("https://reqres.in")
.agent("OkHttp/4.1.0")
.build();
log.debug("CONFIG {}", customHttpConfig.getCustomTimeout());
retrofit = new OkHttpCustomConfiguration(customHttpConfig, maskingLog, true)
.builder()
.addConverterFactory(GsonConverterFactory.create())
Expand Down

0 comments on commit d42e2a9

Please sign in to comment.