Skip to content

Commit

Permalink
Bump Retrofit version to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyZaitsev committed Mar 24, 2016
1 parent 19912a0 commit 91b5e43
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import okhttp3.logging.HttpLoggingInterceptor;
import timber.log.Timber;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;

/**
* Provides OkHttp interceptors for debug build.
Expand All @@ -29,8 +31,9 @@ public HttpLoggingInterceptor provideHttpLoggingInterceptor() {
}

@Provides @OkHttpInterceptors @Singleton @NonNull
public List<Interceptor> provideOkHttpInterceptors(@NonNull HttpLoggingInterceptor httpLoggingInterceptor) {
return singletonList(httpLoggingInterceptor);
public List<Interceptor> provideOkHttpInterceptors(@NonNull HttpLoggingInterceptor httpLoggingInterceptor,
@NonNull HostSelectionInterceptor hostSelectionInterceptor) {
return unmodifiableList(asList(httpLoggingInterceptor, hostSelectionInterceptor));
}

@Provides @OkHttpNetworkInterceptors @Singleton @NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void evaluate() throws Throwable {
final MockWebServer mockWebServer = new MockWebServer();
mockWebServer.start();

TestUtils.app().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());
TestUtils.app().applicationComponent().hostSelectionInterceptor().setBaseUrl(mockWebServer.url("").toString());

if (!needsMockWebServer.setupMethod().isEmpty()) {
final Method setupMethod = testClassInstance.getClass().getDeclaredMethod(needsMockWebServer.setupMethod(), MockWebServer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void beforeEachTest() throws IOException {
mockWebServer.start();

// Change base url to the mocked
QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());
QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().hostSelectionInterceptor().setBaseUrl(mockWebServer.url("").toString());

qualityMattersRestApi = QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().qualityMattersApi();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import android.support.annotation.NonNull;

import com.artemzin.qualitymatters.api.ApiModule;
import com.artemzin.qualitymatters.api.ChangeableBaseUrl;
import com.artemzin.qualitymatters.api.QualityMattersRestApi;
import com.artemzin.qualitymatters.developer_settings.DeveloperSettingsComponent;
import com.artemzin.qualitymatters.developer_settings.DeveloperSettingsModule;
import com.artemzin.qualitymatters.developer_settings.LeakCanaryProxy;
import com.artemzin.qualitymatters.models.ModelsModule;
import com.artemzin.qualitymatters.network.HostSelectionInterceptor;
import com.artemzin.qualitymatters.network.NetworkModule;
import com.artemzin.qualitymatters.network.OkHttpInterceptorsModule;
import com.artemzin.qualitymatters.performance.AsyncJobsModule;
Expand Down Expand Up @@ -42,7 +42,7 @@ public interface ApplicationComponent {
QualityMattersRestApi qualityMattersApi();

@NonNull
ChangeableBaseUrl changeableBaseUrl();
HostSelectionInterceptor hostSelectionInterceptor();

// Provide AsyncJobObserver from the real app to the tests without need in injection to the test.
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void onCreate() {
protected DaggerApplicationComponent.Builder prepareApplicationComponent() {
return DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
// This url may be changed dynamically for tests! See ChangeableBaseUrl.
// This url may be changed dynamically for tests! See HostSelectionInterceptor.
.apiModule(new ApiModule("https://raw.githubusercontent.com/artem-zinnatullin/qualitymatters/master/rest_api/"));
}

Expand Down
13 changes: 4 additions & 9 deletions app/src/main/java/com/artemzin/qualitymatters/api/ApiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@
public class ApiModule {

@NonNull
private final ChangeableBaseUrl changeableBaseUrl;
private final String baseUrl;

public ApiModule(@NonNull String baseUrl) {
changeableBaseUrl = new ChangeableBaseUrl(baseUrl);
this.baseUrl = baseUrl;
}

@Provides @NonNull @Singleton
public ChangeableBaseUrl provideChangeableBaseUrl() {
return changeableBaseUrl;
}

@Provides @NonNull @Singleton
public QualityMattersRestApi provideQualityMattersApi(@NonNull OkHttpClient okHttpClient, @NonNull ObjectMapper objectMapper, @NonNull ChangeableBaseUrl changeableBaseUrl) {
public QualityMattersRestApi provideQualityMattersApi(@NonNull OkHttpClient okHttpClient, @NonNull ObjectMapper objectMapper) {
return new Retrofit.Builder()
.baseUrl(changeableBaseUrl)
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.artemzin.qualitymatters.network;

import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
* Such implementation allows us easily change base url in the integration and functional tests!
*/
@Singleton
public class HostSelectionInterceptor implements Interceptor {

private volatile String baseUrl;

@Inject
public HostSelectionInterceptor() {
// Add a no-argument constructor with the @Inject annotation to indicate that Dagger may create instances.
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
final String host = baseUrl;
if (host != null) {
final HttpUrl newUrl = request.url()
.newBuilder()
.host(host)
.build();
request = request.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(request);
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public String getBaseUrl() {
return baseUrl;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.artemzin.qualitymatters.network;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class HostSelectionInterceptorTest {

private static final String BASE_URL = "http://www.example.com";
private HostSelectionInterceptor interceptor = new HostSelectionInterceptor();

@Test
public void shouldNotHaveBaseUrlAfterCreation() {
assertThat(interceptor.getBaseUrl()).isNull();
}

@Test
public void shouldStoreBaseUrl() {
interceptor.setBaseUrl(BASE_URL);
assertThat(interceptor.getBaseUrl()).isEqualTo(BASE_URL);
}
}
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ext.versions = [
rxJava : '1.1.2',
supportLibs : '23.1.1',
okHttp : '3.2.0',
retrofit : '2.0.0-beta4',
retrofit : '2.0.0',
jackson : '2.6.3',
autoValue : '1.2-rc1',
butterKnife : '7.0.1',
Expand Down

0 comments on commit 91b5e43

Please sign in to comment.