Skip to content

Commit 7098e2f

Browse files
committed
Improve header handling
The User-Agent and proxer-api-key header are now added always to ProxerRequests but not to the OkHttp instance itself. This makes sure that all calls to the Proxer Api have those headers, but the OkHttp is unaffacted and can reused for different things. This also allows for re-adding the ApiKeyHeaderTest.
1 parent a907738 commit 7098e2f

2 files changed

Lines changed: 39 additions & 59 deletions

File tree

library/src/androidTest/java/com/proxerme/library/connection/ProxerConnectionTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ProxerConnectionTest {
5050

5151
private static final String API_KEY = "test";
5252

53+
private static final String API_KEY_HEADER = "proxer-api-key";
5354
private static final String USER_AGENT_HEADER = "User-Agent";
5455
private static final String DEFAULT_USER_AGENT = "ProxerLibAndroid/" + BuildConfig.VERSION_NAME;
5556
private static final String CUSTOM_USER_AGENT = "Test/123";
@@ -70,6 +71,24 @@ public static void tearDownServer() throws IOException {
7071
server.shutdown();
7172
}
7273

74+
@Test
75+
public void testApiKeyHeader() throws Exception {
76+
server.enqueue(new MockResponse().setBody(loadResponse(R.raw.news)));
77+
78+
connection.executeSynchronized(new NewsRequest(0)
79+
.withCustomHost(buildHostUrl(server.url(URL))));
80+
81+
assertEquals(API_KEY, server.takeRequest().getHeader(API_KEY_HEADER));
82+
}
83+
84+
@Test
85+
public void testApiKey() throws Exception {
86+
ProxerConnection testConnection = new ProxerConnection.Builder(API_KEY,
87+
InstrumentationRegistry.getContext()).build();
88+
89+
assertSame(API_KEY, testConnection.getApiKey());
90+
}
91+
7392
@Test
7493
public void testUserAgentHeader() throws Exception {
7594
server.enqueue(new MockResponse().setBody(loadResponse(R.raw.news)));
@@ -95,14 +114,6 @@ public void testCustomUserAgentHeader() throws Exception {
95114
assertEquals(CUSTOM_USER_AGENT, server.takeRequest().getHeader(USER_AGENT_HEADER));
96115
}
97116

98-
@Test
99-
public void testApiKey() throws Exception {
100-
ProxerConnection testConnection = new ProxerConnection.Builder(API_KEY,
101-
InstrumentationRegistry.getContext()).build();
102-
103-
assertSame(API_KEY, testConnection.getApiKey());
104-
}
105-
106117
@Test
107118
public void testCustomMoshi() throws Exception {
108119
Moshi moshi = new Moshi.Builder().build();

library/src/main/java/com/proxerme/library/connection/ProxerConnection.java

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
1212
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
1313
import com.proxerme.library.BuildConfig;
14-
import com.proxerme.library.info.ProxerUrlHolder;
1514
import com.proxerme.library.util.SaveAllSharedPrefCookiePersistor;
1615
import com.squareup.moshi.JsonDataException;
1716
import com.squareup.moshi.Moshi;
@@ -22,8 +21,8 @@
2221
import okhttp3.Call;
2322
import okhttp3.Callback;
2423
import okhttp3.CookieJar;
25-
import okhttp3.Interceptor;
2624
import okhttp3.OkHttpClient;
25+
import okhttp3.Request;
2726
import okhttp3.Response;
2827

2928
/**
@@ -67,7 +66,11 @@
6766
*/
6867
public final class ProxerConnection {
6968

69+
private static final String API_KEY_HEADER = "proxer-api-key";
70+
private static final String USER_AGENT_HEADER = "User-Agent";
71+
7072
private String apiKey;
73+
private String userAgent;
7174
private Moshi moshi;
7275
private OkHttpClient httpClient;
7376

@@ -76,9 +79,10 @@ public final class ProxerConnection {
7679
private Handler handler = new Handler(Looper.getMainLooper());
7780
private ConcurrentHashMap<Integer, ErrorListener> listenerMap;
7881

79-
private ProxerConnection(@NonNull String apiKey, Moshi moshi, OkHttpClient httpClient,
80-
boolean deliverCancelledRequests) {
82+
private ProxerConnection(@NonNull String apiKey, @NonNull String userAgent, Moshi moshi,
83+
OkHttpClient httpClient, boolean deliverCancelledRequests) {
8184
this.apiKey = apiKey;
85+
this.userAgent = userAgent;
8286
this.moshi = moshi;
8387
this.httpClient = httpClient;
8488
this.deliverCancelledRequests = deliverCancelledRequests;
@@ -99,7 +103,7 @@ private ProxerConnection(@NonNull String apiKey, Moshi moshi, OkHttpClient httpC
99103
public <T> ProxerCall execute(@NonNull final ProxerRequest<T> request,
100104
@Nullable final ProxerCallback<T> callback,
101105
@Nullable final ProxerErrorCallback errorCallback) {
102-
Call call = httpClient.newCall(request.build());
106+
Call call = httpClient.newCall(buildRequest(request));
103107

104108
call.enqueue(new Callback() {
105109
@Override
@@ -148,7 +152,7 @@ public void onFailure(Call call, IOException exception) {
148152
@RequiresPermission(android.Manifest.permission.INTERNET)
149153
public <T> T executeSynchronized(@NonNull final ProxerRequest<T> request)
150154
throws ProxerException {
151-
final Call call = httpClient.newCall(request.build());
155+
final Call call = httpClient.newCall(buildRequest(request));
152156
Response response = null;
153157

154158
try {
@@ -297,6 +301,13 @@ private void notifyListener(@NonNull ProxerException exception) {
297301
}
298302
}
299303

304+
private <T> Request buildRequest(ProxerRequest<T> request) {
305+
return request.build().newBuilder()
306+
.header(API_KEY_HEADER, apiKey)
307+
.header(USER_AGENT_HEADER, userAgent)
308+
.build();
309+
}
310+
300311
public interface ErrorListener {
301312
void onError(@NonNull ProxerException exception);
302313
}
@@ -306,9 +317,7 @@ public interface ErrorListener {
306317
*/
307318
public static class Builder {
308319

309-
private static final String API_KEY_HEADER = "proxer-api-key";
310320
private static final String DEFAULT_USER_AGENT = "ProxerLibAndroid";
311-
private static final String USER_AGENT_HEADER = "User-Agent";
312321

313322
private String apiKey;
314323
private Context context;
@@ -354,7 +363,8 @@ public ProxerConnection build() {
354363
configureUserAgent();
355364
configureOkHttp();
356365

357-
return new ProxerConnection(apiKey, moshi, httpClient, deliverCancelledRequests);
366+
return new ProxerConnection(apiKey, userAgent, moshi, httpClient,
367+
deliverCancelledRequests);
358368
}
359369

360370
/**
@@ -435,48 +445,7 @@ private void configureOkHttp() {
435445
builder = httpClient.newBuilder();
436446
}
437447

438-
httpClient = builder.cookieJar(cookieJar)
439-
.addInterceptor(new ApiKeyInterceptor(apiKey))
440-
.addInterceptor(new UserAgentInterceptor(userAgent))
441-
.build();
442-
}
443-
444-
private class ApiKeyInterceptor implements Interceptor {
445-
446-
private String apiKey;
447-
448-
ApiKeyInterceptor(String apiKey) {
449-
this.apiKey = apiKey;
450-
}
451-
452-
@Override
453-
public Response intercept(Chain chain) throws IOException {
454-
if (chain.request().url().host()
455-
.equals(ProxerUrlHolder.getBaseApiHost().host())) {
456-
return chain.proceed(chain.request().newBuilder()
457-
.header(API_KEY_HEADER, apiKey)
458-
.build());
459-
} else {
460-
return chain.proceed(chain.request());
461-
}
462-
}
463-
}
464-
465-
private class UserAgentInterceptor implements Interceptor {
466-
467-
private String userAgent;
468-
469-
UserAgentInterceptor(@NonNull String userAgent) {
470-
this.userAgent = userAgent;
471-
}
472-
473-
@Override
474-
public Response intercept(Chain chain) throws IOException {
475-
return chain.proceed(chain.request().newBuilder()
476-
.header(USER_AGENT_HEADER, userAgent)
477-
.build());
478-
}
448+
httpClient = builder.cookieJar(cookieJar).build();
479449
}
480-
481450
}
482451
}

0 commit comments

Comments
 (0)