Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose okhttp3.Call.Factory in all services #344

Merged
merged 4 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
public abstract class MapboxService<T> {

private boolean enableDebug = false;
private OkHttpClient okHttpClient = null;
private okhttp3.Call.Factory callFactory = null;

public abstract Response<T> executeCall() throws IOException;

Expand All @@ -39,22 +41,50 @@ public void setEnableDebug(boolean enableDebug) {
this.enableDebug = enableDebug;
}

/**
* Gets the call factory for creating {@link Call} instances.
*
* @return the call factory, or the default OkHttp client if it's null.
* @since 2.0.0
*/
public okhttp3.Call.Factory getCallFactory() {
if (callFactory == null) {
return getOkHttpClient();
}

return callFactory;
}

/**
* Specify a custom call factory for creating {@link Call} instances.
*
* @param callFactory implementation
* @since 2.0.0
*/
public void setCallFactory(okhttp3.Call.Factory callFactory) {
this.callFactory = callFactory;
}

/**
* Used Internally.
*
* @return OkHttpClient
* @since 1.0.0
*/
public OkHttpClient getOkHttpClient() {
if (isEnableDebug()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
return httpClient.build();
} else {
return new OkHttpClient();
if (okHttpClient == null) {
if (isEnableDebug()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
okHttpClient = httpClient.build();
} else {
okHttpClient = new OkHttpClient();
}
}

return okHttpClient;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private DirectionsService getService() {
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addConverterFactory(GsonConverterFactory.create())
.callFactory(getCallFactory())
.build();

// Directions service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private DistanceService getService() {
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addConverterFactory(GsonConverterFactory.create(getGson()))
.callFactory(getCallFactory())
.build();

// Distance service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public GeocodingService getService() {
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addConverterFactory(GsonConverterFactory.create(getGson()))
.callFactory(getCallFactory())
.build();

service = retrofit.create(GeocodingService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private MapMatchingService getService() {
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addConverterFactory(GsonConverterFactory.create())
.callFactory(getCallFactory())
.build();

// MapMatching service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ public void requiredAccessToken() throws ServicesException {
new MapboxDirections.Builder().build();
}

@Test
public void callFactoryNonNull() throws ServicesException, IOException {
MapboxDirections client = new MapboxDirections.Builder()
.setAccessToken("pk.XXX")
.setCoordinates(positions)
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setBaseUrl(mockUrl.toString())
.build();

// Setting a null call factory doesn't make the request fail
// (the default OkHttp client is used)
client.setCallFactory(null);
Response<DirectionsResponse> response = client.executeCall();
assertEquals(response.code(), 200);
assertEquals(response.body().getCode(), DirectionsCriteria.RESPONSE_OK);
}

@Test
public void coordinatesOverLimit() throws ServicesException {
thrown.expect(ServicesException.class);
Expand Down