Skip to content

Commit 4a42f31

Browse files
committed
Added HTTP interceptors
1 parent 67c0245 commit 4a42f31

4 files changed

Lines changed: 144 additions & 2 deletions

File tree

net/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
apply plugin: 'java'
22

3+
sourceCompatibility = "1.7"
4+
targetCompatibility = "1.7"
5+
36
dependencies {
47
compile "com.squareup.retrofit2:retrofit:${retrofit2Version}"
58
compile "com.squareup.retrofit2:converter-gson:${retrofitGsonVersion}"
@@ -9,5 +12,3 @@ dependencies {
912
compile project(':steam')
1013
}
1114

12-
sourceCompatibility = "1.7"
13-
targetCompatibility = "1.7"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.michaelfotiadis.steam.net;
2+
3+
4+
import com.michaelfotiadis.steam.net.client.interceptors.HeadersInterceptor;
5+
import com.michaelfotiadis.steam.net.client.interceptors.RetryPolicyInterceptor;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import okhttp3.Interceptor;
11+
import okhttp3.OkHttpClient;
12+
import okhttp3.logging.HttpLoggingInterceptor;
13+
14+
public class OkHttpFactoryImpl implements OkHttpFactory {
15+
16+
private final boolean enableDebug;
17+
18+
public OkHttpFactoryImpl(final boolean enableDebug) {
19+
this.enableDebug = enableDebug;
20+
}
21+
22+
public OkHttpClient create(final Class<?> clazz) {
23+
/**
24+
* This is important to avoid using a {@link TokenManager} in {@link LoginApi}
25+
* as the job of the {@link TokenManager} is to use the {@link LoginApi} to
26+
* refresh the token.
27+
*/
28+
final List<Interceptor> interceptors = new ArrayList<>();
29+
interceptors.add(new HeadersInterceptor());
30+
interceptors.add(new RetryPolicyInterceptor());
31+
interceptors.add(createLoggingInterceptor());
32+
33+
return createClient(interceptors);
34+
}
35+
36+
private OkHttpClient createClient(final List<Interceptor> interceptors) {
37+
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
38+
39+
for (final Interceptor interceptor : interceptors) {
40+
builder.addInterceptor(interceptor);
41+
}
42+
43+
return builder.build();
44+
}
45+
46+
private Interceptor createLoggingInterceptor() {
47+
final HttpLoggingInterceptor.Level level = enableDebug ? HttpLoggingInterceptor.Level.BASIC : HttpLoggingInterceptor.Level.NONE;
48+
49+
final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
50+
interceptor.setLevel(level);
51+
52+
return interceptor;
53+
}
54+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.michaelfotiadis.steam.net.client.interceptors;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.Request;
7+
import okhttp3.Response;
8+
9+
public class HeadersInterceptor implements Interceptor {
10+
11+
private static final String CONTENT_TYPE = "Content-Type";
12+
private static final String APPLICATION_JSON = "application/json";
13+
14+
@Override
15+
public Response intercept(final Chain chain) throws IOException {
16+
final Request originalRequest = chain.request();
17+
final Request.Builder builder = originalRequest.newBuilder();
18+
builder.addHeader(CONTENT_TYPE, APPLICATION_JSON);
19+
20+
return chain.proceed(builder.build());
21+
}
22+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.michaelfotiadis.steam.net.client.interceptors;
2+
3+
import java.io.IOException;
4+
import java.net.SocketTimeoutException;
5+
6+
import okhttp3.Interceptor;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
public class RetryPolicyInterceptor implements Interceptor {
11+
private static final int HTTP_CODE_202 = 202;
12+
private static final long MAX_RETRY_TIME = 60 * 60 * 1000; // 1 minute
13+
private static final long SLEEP_TIME = 3 * 1000; // 3 seconds
14+
15+
@Override
16+
public Response intercept(final Chain chain) throws IOException {
17+
18+
final Request request = chain.request();
19+
final Response response;
20+
try {
21+
// try the call
22+
response = chain.proceed(request);
23+
24+
//noinspection IfStatementWithNegatedCondition
25+
if (response.code() != HTTP_CODE_202) {
26+
return response;
27+
} else {
28+
response.body().close();
29+
return repeatRequest(chain, request);
30+
}
31+
} catch (final SocketTimeoutException e) {
32+
throw e;
33+
}
34+
}
35+
36+
@SuppressWarnings("MethodMayBeStatic")
37+
private Response repeatRequest(final Chain chain, final Request authorisedRequest) throws IOException {
38+
39+
Response repeatResponse = chain.proceed(authorisedRequest);
40+
41+
final long startTime = System.currentTimeMillis();
42+
while (repeatResponse.code() == HTTP_CODE_202) {
43+
final long elapsedTime = System.currentTimeMillis() - startTime;
44+
if (elapsedTime > MAX_RETRY_TIME) {
45+
return chain.proceed(authorisedRequest);
46+
} else {
47+
try {
48+
Thread.sleep(SLEEP_TIME);
49+
} catch (final InterruptedException e) {
50+
//Do nothing.
51+
}
52+
repeatResponse = chain.proceed(authorisedRequest);
53+
//noinspection IfStatementWithNegatedCondition
54+
if (repeatResponse.code() != HTTP_CODE_202) {
55+
return chain.proceed(authorisedRequest);
56+
} else {
57+
chain.proceed(authorisedRequest);
58+
}
59+
}
60+
}
61+
62+
return repeatResponse;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)