Skip to content

Commit

Permalink
add write time out
Browse files Browse the repository at this point in the history
  • Loading branch information
amkalantari committed Sep 4, 2019
1 parent 377e3d9 commit 0354c15
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
6 changes: 5 additions & 1 deletion app/src/main/java/com/github/restclient/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private void initRestClient() {
.setDebugEnable(true)
.setHeader(header)
.setConnectionTimeOut(15000)
.setWriteTimeOut(15000)
.setReadTimeOut(15000)
.build();
}

Expand All @@ -87,6 +89,8 @@ private void initRestSamlple() {
.Builder(this)
.setHeader(header)
.setConnectionTimeOut(15000)
.setWriteTimeOut(15000)
.setReadTimeOut(15000)
.build();
}

Expand Down Expand Up @@ -122,7 +126,7 @@ private void initRestClientWithCP() {

private void callSimpleApi() {
RequestParams params = new RequestParams(RequestBodyType.FormData);
restClient.GET("https://api.spotify.com/v1/tracks/2TpxZ7JUBn3uw46aR7qd6V",
restClient.GET("http://nestle.koosha.ir/User/UserLogin.ashx?Username=MahdiPiran&Password=12345678&",
"",
new ResponseJsonHandler() {
@Override
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ buildscript {
extra.set("compileSdk", 28)
extra.set("targetSdk", 28)
extra.set("buildTools", "28.0.3")
extra.set("version_code", 16)
extra.set("version_name", "1.1.7")
extra.set("version_code", 17)
extra.set("version_name", "1.1.8")

repositories {
google()
jcenter()
maven("https://jitpack.io")
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.1")
classpath("com.android.tools.build:gradle:3.5.0")
classpath("com.github.dcendents:android-maven-gradle-plugin:2.1")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
11 changes: 7 additions & 4 deletions library/src/main/java/com/github/library/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ abstract class BaseClient {

int timeRead = 30;

int timeWrite = 30;

boolean debugEnable = true;

private static OkHttpClient instance;

static CertificatePinner certificatePinner;

static synchronized OkHttpClient getClient(int timeOut, int readTime, boolean enableDebug) {
static synchronized OkHttpClient getClient(int timeOut, int readTime,int timeWrite, boolean enableDebug) {

if (instance == null) {

OkHttpClient.Builder instanceBuilder = new OkHttpClient()
.newBuilder()
.readTimeout(readTime, TimeUnit.MILLISECONDS)
.connectTimeout(timeOut, TimeUnit.MILLISECONDS);
.connectTimeout(timeOut, TimeUnit.MILLISECONDS)
.writeTimeout(timeWrite,TimeUnit.MILLISECONDS);

if (enableDebug)
instanceBuilder.addInterceptor(new LoggingInterceptor());
Expand All @@ -76,13 +79,13 @@ static synchronized OkHttpClient getClient(int timeOut, int readTime, boolean en
}

public void cancelAllRequest() {
for (Call call : getClient(timeMilliSecond, timeRead, debugEnable).dispatcher().queuedCalls()) {
for (Call call : getClient(timeMilliSecond, timeRead,timeWrite, debugEnable).dispatcher().queuedCalls()) {
call.cancel();
}
}

public void cancelCallWithTag(String tag) {
for (Call call : getClient(timeMilliSecond, timeRead, debugEnable).dispatcher().queuedCalls()) {
for (Call call : getClient(timeMilliSecond, timeRead,timeWrite, debugEnable).dispatcher().queuedCalls()) {
try {
Object item = call.request().tag();
if (item != null) {
Expand Down
31 changes: 20 additions & 11 deletions library/src/main/java/com/github/library/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static class Builder {

private Integer timeRead = 30;

private Integer timeWrite = 30;

private boolean debugEnable = true;

private AuthType authType = AuthType.NO_AUTH;
Expand Down Expand Up @@ -78,7 +80,12 @@ public Builder setConnectionTimeOut(Integer timeMilliSecond) {
}

public Builder setReadTimeOut(Integer timeMilliSecond) {
this.timeMilliSecond = timeMilliSecond;
this.timeRead = timeMilliSecond;
return this;
}

public Builder setWriteTimeOut(Integer timeMilliSecond) {
this.timeWrite = timeMilliSecond;
return this;
}

Expand Down Expand Up @@ -114,6 +121,8 @@ private RestClient(Builder builder) {
this.password = builder.password;
this.debugEnable = builder.debugEnable;
this.timeMilliSecond = builder.timeMilliSecond;
this.timeRead = builder.timeRead;
this.timeWrite = builder.timeWrite;
this.authType = builder.authType;
this.headers = builder.headers;

Expand All @@ -131,9 +140,9 @@ public void POST(
}
//endregion
if (this.authType == AuthType.OAUTH2_AUTH) {
POST.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
POST.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
} else {
POST.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
POST.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
}
}

Expand All @@ -150,9 +159,9 @@ public void POST(
}
//endregion
if (this.authType == AuthType.OAUTH2_AUTH) {
POST.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder);
POST.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder);
} else {
POST.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder);
POST.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(extraHeaders), params, responder);
}
}

Expand All @@ -167,9 +176,9 @@ public void GET(

//endregion
if (this.authType == AuthType.OAUTH2_AUTH) {
GET.oauth2_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), responder);
GET.oauth2_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), responder);
} else {
GET.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), responder);
GET.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), responder);
}
}

Expand All @@ -185,9 +194,9 @@ public void DELETE(
}

if (this.authType == AuthType.OAUTH2_AUTH) {
DELETE.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
DELETE.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
} else {
DELETE.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
DELETE.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
}
}

Expand All @@ -203,9 +212,9 @@ public void PUT(
}

if (this.authType == AuthType.OAUTH2_AUTH) {
PUT.basic_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
PUT.basic_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
} else {
PUT.no_Auth(getClient(timeMilliSecond, timeRead, debugEnable), url, tag, getAuthModel(), params, responder);
PUT.no_Auth(getClient(timeMilliSecond, timeRead,timeWrite, debugEnable), url, tag, getAuthModel(), params, responder);
}
}
}

0 comments on commit 0354c15

Please sign in to comment.