Skip to content
This repository was archived by the owner on Aug 23, 2024. It is now read-only.

Commit 22df320

Browse files
author
Stan Wijckmans
committed
Merge branch 'feature/3-don-t-let-volly-retry-requests' into 'develop'
Feature/3 don t let volly retry requests See merge request Stannieman/RestAndroid!11
2 parents a1d0801 + 6ae83f8 commit 22df320

13 files changed

+71
-51
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
*.iml
22
.gradle
33
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
4+
.idea
65
.DS_Store
76
/build
87
/captures

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:2.2.3'
6+
classpath 'com.android.tools.build:gradle:3.0.1'
77
}
88
}
99

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-all.zip

rest/build.gradle

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ apply plugin: 'com.android.library'
22
apply plugin: 'maven-publish'
33

44
android {
5-
compileSdkVersion 25
6-
buildToolsVersion "25.0.2"
5+
compileSdkVersion 27
6+
buildToolsVersion "27.0.3"
77

88
defaultConfig {
99
minSdkVersion 16
1010
targetSdkVersion 27
11-
versionCode 4
12-
versionName "1.2.0"
11+
versionCode 5
12+
versionName "1.2.1"
1313
}
1414

1515
lintOptions {
@@ -18,9 +18,9 @@ android {
1818
}
1919

2020
dependencies {
21-
compile 'be.stannieman:commonservices:1.0.1.0'
22-
compile 'com.android.volley:volley:1.0.0'
23-
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.7'
21+
compile 'be.stannieman:commonservices:1.0.2.0'
22+
compile 'com.android.volley:volley:1.1.0'
23+
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.4'
2424
}
2525

2626
task createJavaDoc(type: Javadoc) {
@@ -44,8 +44,8 @@ publishing {
4444
aar(MavenPublication) {
4545
groupId 'be.stannieman'
4646
artifactId 'rest'
47-
version '1.0.1'
48-
artifact "$buildDir\\outputs\\aar\\signed-${project.getName()}-release.aar"
47+
version '1.2.1'
48+
artifact "$buildDir\\outputs\\aar\\${project.getName()}-release.aar"
4949
artifact createJavaDocJar
5050
artifact createSourcesJar
5151

rest/src/main/java/stannieman/rest/BasicAuthRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.android.volley.Request;
66
import com.android.volley.RequestQueue;
7+
import com.android.volley.RetryPolicy;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89

910
import java.util.HashMap;
@@ -23,8 +24,8 @@ public final class BasicAuthRestClient extends RestClientBase {
2324
private static final String BasicAuthHeaderValuePrefix = "Basic ";
2425
private final Map<String, String> authHeader;
2526

26-
BasicAuthRestClient(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme schema, String host, int port, String apiBasePath, String endpointPath, long timeout, String username, String password) {
27-
super(objectMapper, requestQueue, schema, host, port, apiBasePath, endpointPath, timeout);
27+
BasicAuthRestClient(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme schema, String host, int port, String apiBasePath, String endpointPath, long timeout, RetryPolicy retryPolicy, String username, String password) {
28+
super(objectMapper, requestQueue, schema, host, port, apiBasePath, endpointPath, timeout, retryPolicy);
2829
authHeader = getBasicAuthHeader(username, password);
2930
}
3031

rest/src/main/java/stannieman/rest/BasicAuthRestClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public BasicAuthRestClientFactory(Context context) {
2222
@Override
2323
public IRestClient getRestClient(String endpointPath) {
2424
configLock.lock();
25-
IRestClient client = new BasicAuthRestClient(objectMapper, requestQueue, scheme, host, port, apiBasePath, endpointPath, timeout, username, password);
25+
IRestClient client = new BasicAuthRestClient(objectMapper, requestQueue, scheme, host, port, apiBasePath, endpointPath, timeout, retryPolicy, username, password);
2626
configLock.unlock();
2727

2828
return client;

rest/src/main/java/stannieman/rest/KeyAuthRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.android.volley.Request;
44
import com.android.volley.RequestQueue;
5+
import com.android.volley.RetryPolicy;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67

78
import java.util.AbstractMap;
@@ -20,8 +21,8 @@
2021
public final class KeyAuthRestClient extends RestClientBase {
2122
private final AbstractMap.SimpleEntry<String, String> keyQueryParameter;
2223

23-
KeyAuthRestClient(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme schema, String host, int port, String apiBasePath, String endpointPath, long timeout, String keyParameterName, String key) {
24-
super(objectMapper, requestQueue, schema, host, port, apiBasePath, endpointPath, timeout);
24+
KeyAuthRestClient(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme schema, String host, int port, String apiBasePath, String endpointPath, long timeout, RetryPolicy retryPolicy, String keyParameterName, String key) {
25+
super(objectMapper, requestQueue, schema, host, port, apiBasePath, endpointPath, timeout, retryPolicy);
2526
keyQueryParameter = new AbstractMap.SimpleEntry<>(keyParameterName, key);
2627
}
2728

rest/src/main/java/stannieman/rest/KeyAuthRestClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public KeyAuthRestClientFactory(Context context) {
2222
@Override
2323
public IRestClient getRestClient(String endpointPath) {
2424
configLock.lock();
25-
IRestClient client = new KeyAuthRestClient(objectMapper, requestQueue, scheme, host, port, apiBasePath, endpointPath, timeout, keyParameterName, key);
25+
IRestClient client = new KeyAuthRestClient(objectMapper, requestQueue, scheme, host, port, apiBasePath, endpointPath, timeout, retryPolicy, keyParameterName, key);
2626
configLock.unlock();
2727

2828
return client;

rest/src/main/java/stannieman/rest/NetworkResponseRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.android.volley.NetworkResponse;
44
import com.android.volley.Request;
55
import com.android.volley.Response;
6+
import com.android.volley.RetryPolicy;
67
import com.android.volley.toolbox.HttpHeaderParser;
78

89
import java.io.UnsupportedEncodingException;
@@ -17,12 +18,13 @@ class NetworkResponseRequest extends Request<NetworkResponse> {
1718
private final String body;
1819
private final String encoding;
1920

20-
public NetworkResponseRequest(int method, String url, Map<String, String> headers, String body, Response.Listener<NetworkResponse> responseListener, Response.ErrorListener errorListener, String encoding) {
21+
public NetworkResponseRequest(int method, String url, Map<String, String> headers, String body, RetryPolicy retryPolicy, Response.Listener<NetworkResponse> responseListener, Response.ErrorListener errorListener, String encoding) {
2122
super(method, url, errorListener);
2223
this.responseListener = responseListener;
2324
this.headers = headers;
2425
this.body = body;
2526
this.encoding = encoding;
27+
this.setRetryPolicy(retryPolicy);
2628
}
2729

2830
@Override

rest/src/main/java/stannieman/rest/RestClientBase.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.android.volley.NetworkResponse;
44
import com.android.volley.RequestQueue;
5+
import com.android.volley.RetryPolicy;
56
import com.android.volley.VolleyError;
67
import com.android.volley.toolbox.RequestFuture;
78
import com.fasterxml.jackson.core.JsonParseException;
@@ -21,6 +22,7 @@
2122
import java.util.concurrent.TimeoutException;
2223

2324
import stannieman.commonservices.helpers.ResultCodeHelper;
25+
import stannieman.commonservices.models.DataServiceResult;
2426
import stannieman.commonservices.models.GeneralResultCodes;
2527
import stannieman.commonservices.models.IHasDataAndSuccessState;
2628
import stannieman.commonservices.models.ServiceResult;
@@ -52,8 +54,9 @@ abstract class RestClientBase implements IRestClient {
5254
private final int port;
5355
private final String absoluteEndpointPath;
5456
private final long timeout;
57+
private final RetryPolicy retryPolicy;
5558

56-
protected RestClientBase(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme scheme, String host, int port, String apiBasePath, String endpointPath, long timeout) {
59+
protected RestClientBase(ObjectMapper objectMapper, RequestQueue requestQueue, Scheme scheme, String host, int port, String apiBasePath, String endpointPath, long timeout, RetryPolicy retryPolicy) {
5760
this.objectMapper = objectMapper;
5861
this.requestQueue = requestQueue;
5962

@@ -62,33 +65,34 @@ protected RestClientBase(ObjectMapper objectMapper, RequestQueue requestQueue, S
6265
this.port = port;
6366
this.absoluteEndpointPath = getAbsoluteEndpointPath(apiBasePath, endpointPath);
6467
this.timeout = timeout;
68+
this.retryPolicy = retryPolicy;
6569
}
6670

6771
protected <SuccessResponseDataType, ErrorResponseDataType extends ErrorResponseDataBase> IHasDataAndSuccessState<RestResult<SuccessResponseDataType, ErrorResponseDataType>> doRequest(int method, RequestProperties<SuccessResponseDataType, ErrorResponseDataType> requestProperties, List<AbstractMap.SimpleEntry<String, String>> queryParameters, Map<String, String> headers) {
6872
IHasDataAndSuccessState<String> uriResult = getUriString(requestProperties.getSubPath(), requestProperties.getSubPathParameters(), queryParameters);
6973
if (!uriResult.isSuccess()) {
7074
RestClientResultCodes resultCode = ResultCodeHelper.GetResultCodeOrNull(uriResult, RestClientResultCodes.class);
7175
return resultCode != null
72-
? new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
73-
: new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.CANNOT_CREATE_URI);
76+
? new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
77+
: new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.CANNOT_CREATE_URI);
7478
}
7579
String uriString = uriResult.getData();
7680

7781
IHasDataAndSuccessState<String> bodyStringResult = getBodyString(requestProperties.getBody());
7882
if (!bodyStringResult.isSuccess()) {
7983
RestClientResultCodes resultCode = ResultCodeHelper.GetResultCodeOrNull(uriResult, RestClientResultCodes.class);
8084
return resultCode != null
81-
? new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
82-
: new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.CANNOT_CREATE_JSON_STRING_FROM_OBJECT);
85+
? new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
86+
: new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.CANNOT_CREATE_JSON_STRING_FROM_OBJECT);
8387
}
8488
String bodyString = bodyStringResult.getData();
8589

8690
IHasDataAndSuccessState<NetworkResponse> networkResponseResult = getNetworkResponse(method, uriString, headers, bodyString);
8791
if (!networkResponseResult.isSuccess()) {
8892
RestClientResultCodes resultCode = ResultCodeHelper.GetResultCodeOrNull(uriResult, RestClientResultCodes.class);
8993
return resultCode != null
90-
? new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
91-
: new ServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.REQUEST_FAILED);
94+
? new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(resultCode)
95+
: new DataServiceResult<RestResult<SuccessResponseDataType, ErrorResponseDataType>, RestClientResultCodes>(RestClientResultCodes.REQUEST_FAILED);
9296
}
9397
NetworkResponse networkResponse = networkResponseResult.getData();
9498

@@ -101,84 +105,92 @@ private IHasDataAndSuccessState<String> getUriString(String subPath, String[] su
101105
try {
102106
String uriString = new URI(scheme, null, host, port, absoluteEndpointPath + parametrizedSubPath, null, null).toString();
103107
String queryString = QueryParamsHelper.getQueryString(queryParameters, ENCODING);
104-
return new ServiceResult<>(uriString + queryString, GeneralResultCodes.OK);
108+
return new DataServiceResult<>(uriString + queryString, GeneralResultCodes.OK);
105109
} catch (Exception e) {
106-
return new ServiceResult<>(CANNOT_CREATE_URI);
110+
return new DataServiceResult<>(CANNOT_CREATE_URI);
107111
}
108112
}
109113

110114
private IHasDataAndSuccessState<String> getBodyString(Object body) {
111115
if (body != null) {
112116
try {
113-
return new ServiceResult<>(objectMapper.writeValueAsString(body), GeneralResultCodes.OK);
117+
return new DataServiceResult<>(objectMapper.writeValueAsString(body), GeneralResultCodes.OK);
114118
} catch (Exception e) {
115-
return new ServiceResult<>(RestClientResultCodes.CANNOT_CREATE_JSON_STRING_FROM_OBJECT);
119+
return new DataServiceResult<>(RestClientResultCodes.CANNOT_CREATE_JSON_STRING_FROM_OBJECT);
116120
}
117121
}
118-
return new ServiceResult<>();
122+
return new DataServiceResult<>();
119123
}
120124

121125
private IHasDataAndSuccessState<NetworkResponse> getNetworkResponse(int method, String uriString, Map<String, String> headers, String bodyString) {
122126
RequestFuture<NetworkResponse> future = RequestFuture.newFuture();
123-
requestQueue.add(new NetworkResponseRequest(method, uriString, getHeadersWithRequestDefaultHeaders(headers), bodyString, future, future, ENCODING));
127+
requestQueue.add(new NetworkResponseRequest(
128+
method,
129+
uriString,
130+
getHeadersWithRequestDefaultHeaders(headers),
131+
bodyString,
132+
retryPolicy,
133+
future,
134+
future,
135+
ENCODING));
124136

125137
NetworkResponse response;
126138
try {
127139
response = future.get(timeout, TimeUnit.MILLISECONDS);
128140
}
129141
catch (InterruptedException e) {
130-
return new ServiceResult<>(RestClientResultCodes.REQUEST_INTERRUPTED);
142+
return new DataServiceResult<>(RestClientResultCodes.REQUEST_INTERRUPTED);
131143
} catch (ExecutionException e) {
132144
Throwable cause = e.getCause();
133145
if (!(cause instanceof VolleyError)) {
134-
return new ServiceResult<>(RestClientResultCodes.REQUEST_FAILED);
146+
return new DataServiceResult<>(RestClientResultCodes.REQUEST_FAILED);
135147
}
136148
VolleyError error = (VolleyError) cause;
137149
if (error.networkResponse == null) {
138-
return new ServiceResult<>(RestClientResultCodes.REQUEST_FAILED);
150+
return new DataServiceResult<>(RestClientResultCodes.REQUEST_FAILED);
139151
}
140152
response = error.networkResponse;
141153
} catch (TimeoutException e) {
142-
return new ServiceResult<>(RestClientResultCodes.REQUEST_TIMED_OUT);
154+
return new DataServiceResult<>(RestClientResultCodes.REQUEST_TIMED_OUT);
143155
}
144156

145-
return new ServiceResult<>(response, GeneralResultCodes.OK);
157+
return new DataServiceResult<>(response, GeneralResultCodes.OK);
146158
}
147159

148160
private <SuccessResponseDataType, ErrorResponseDataType extends ErrorResponseDataBase> IHasDataAndSuccessState<RestResult<SuccessResponseDataType, ErrorResponseDataType>> createRestResultFromNetworkResponse(Class<SuccessResponseDataType> successResponseDataType, Class<ErrorResponseDataType> errorResponseDataType, NetworkResponse networkResponse, Integer[] successStatusCodes) {
149161
String jsonString = NetworkResponseRequest.parseToString(networkResponse);
150162

151163
if (isStatusCodeOk(networkResponse.statusCode, successStatusCodes)) {
152164
if (successResponseDataType == null) {
153-
return new ServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode), GeneralResultCodes.OK);
165+
return new DataServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode), GeneralResultCodes.OK);
154166
}
155167
else {
156168
try {
157169
SuccessResponseDataType successObject = objectMapper.readValue(jsonString, successResponseDataType);
158-
return new ServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode, successObject), GeneralResultCodes.OK);
170+
return new DataServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode, successObject), GeneralResultCodes.OK);
159171
} catch (JsonMappingException e) {
160-
return new ServiceResult<>(RestClientResultCodes.JSON_RESPONSE_DATA_TYPE_MISMATCH);
172+
return new DataServiceResult<>(RestClientResultCodes.JSON_RESPONSE_DATA_TYPE_MISMATCH);
161173
} catch (JsonParseException e) {
162-
return new ServiceResult<>(RestClientResultCodes.RESPONSE_IS_NOT_VALID_JSON);
174+
return new DataServiceResult<>(RestClientResultCodes.RESPONSE_IS_NOT_VALID_JSON);
163175
} catch (IOException e) {
164-
return new ServiceResult<>(RestClientResultCodes.CANNOT_CREATE_OBJECT_FROM_SUCCESS_RESPONSE);
176+
return new DataServiceResult<>(RestClientResultCodes.CANNOT_CREATE_OBJECT_FROM_SUCCESS_RESPONSE);
165177
}
166178
}
167179
}
168180

169181
if (errorResponseDataType == null) {
170-
return new ServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(false, networkResponse.statusCode));
182+
return new DataServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(false, networkResponse.statusCode));
171183
}
172184
else {
173185
try {
174186
ErrorResponseDataType errorObject = objectMapper.readValue(jsonString, errorResponseDataType);
175-
return new ServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode, errorObject), GeneralResultCodes.OK);
187+
return new DataServiceResult<>(new RestResult<SuccessResponseDataType, ErrorResponseDataType>(networkResponse.statusCode, errorObject), GeneralResultCodes.OK);
176188
} catch (JsonMappingException e) {
177-
return new ServiceResult<>(RestClientResultCodes.JSON_ERROR_DATA_TYPE_MISMATCH);
189+
return new DataServiceResult<>(RestClientResultCodes.JSON_ERROR_DATA_TYPE_MISMATCH);
178190
} catch (JsonParseException e) {
179-
return new ServiceResult<>(RestClientResultCodes.RESPONSE_IS_NOT_VALID_JSON);
191+
return new DataServiceResult<>(RestClientResultCodes.RESPONSE_IS_NOT_VALID_JSON);
180192
} catch (IOException e) {
181-
return new ServiceResult<>(RestClientResultCodes.CANNOT_CREATE_OBJECT_FROM_ERROR_RESPONSE);
193+
return new DataServiceResult<>(RestClientResultCodes.CANNOT_CREATE_OBJECT_FROM_ERROR_RESPONSE);
182194
}
183195
}
184196
}

0 commit comments

Comments
 (0)