Skip to content

Commit 9b61d4d

Browse files
committed
Send a User-Agent and make it possible to set a custom one
1 parent 007be24 commit 9b61d4d

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
1212
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
13+
import com.proxerme.library.BuildConfig;
1314
import com.proxerme.library.info.ProxerUrlHolder;
1415
import com.proxerme.library.util.SaveAllSharedPrefCookiePersistor;
1516
import com.squareup.moshi.JsonDataException;
@@ -306,10 +307,12 @@ public interface ErrorListener {
306307
public static class Builder {
307308

308309
private static final String API_KEY_HEADER = "proxer-api-key";
310+
private static final String USER_AGENT_HEADER = "User-Agent";
309311

310312
private String apiKey;
311313
private Context context;
312314
private boolean deliverCancelledRequests;
315+
private String userAgent = "ProxerLibAndroid/" + BuildConfig.VERSION_NAME;
313316

314317
private Moshi moshi;
315318
private CookieJar cookieJar;
@@ -382,6 +385,12 @@ public Builder withCustomOkHttp(OkHttpClient httpClient) {
382385
return this;
383386
}
384387

388+
public Builder withCustomUserAgent(String userAgent) {
389+
this.userAgent = userAgent;
390+
391+
return this;
392+
}
393+
385394
/**
386395
* Allows to set if requests should be delivered on the errorCallback if they have been
387396
* cancelled.
@@ -425,13 +434,21 @@ public Response intercept(Chain chain) throws IOException {
425434
if (chain.request().url().host()
426435
.equals(ProxerUrlHolder.getBaseApiHost().host())) {
427436
return chain.proceed(chain.request().newBuilder()
428-
.addHeader(API_KEY_HEADER, apiKey)
437+
.header(API_KEY_HEADER, apiKey)
429438
.build());
430439
} else {
431440
return chain.proceed(chain.request());
432441
}
433442
}
434443
})
444+
.addInterceptor(new Interceptor() {
445+
@Override
446+
public Response intercept(Chain chain) throws IOException {
447+
return chain.proceed(chain.request().newBuilder()
448+
.header(USER_AGENT_HEADER, userAgent)
449+
.build());
450+
}
451+
})
435452
.build();
436453
}
437454
}

0 commit comments

Comments
 (0)