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 889c8ad
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 72 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 @@ -28,9 +30,15 @@ public HttpLoggingInterceptor provideHttpLoggingInterceptor() {
return new HttpLoggingInterceptor(message -> Timber.d(message));
}

@Provides @Singleton @NonNull
public HostSelectionInterceptor provideHostSelectionInterceptor() {
return new HostSelectionInterceptor();
}

@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 android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.io.IOException;
import java.net.URL;
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!
*/
public class HostSelectionInterceptor implements Interceptor {

@Nullable
private volatile String baseUrl;

@Override @NonNull
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
final String baseUrl = this.baseUrl;
if (baseUrl != null) {
final URL url = new URL(baseUrl);
final HttpUrl newUrl = request.url()
.newBuilder()
.scheme(url.getProtocol())
.host(url.getHost())
.port(url.getPort())
.build();
request = request.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(request);
}

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

@Nullable
public String getBaseUrl() {
return baseUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.NonNull;

import java.util.Collections;
import java.util.List;

import javax.inject.Singleton;
Expand All @@ -11,16 +12,22 @@
import okhttp3.Interceptor;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

/**
* Provides OkHttp interceptors for release build.
*/
@Module
public class OkHttpInterceptorsModule {

@Provides @Singleton @NonNull
public HostSelectionInterceptor provideHostSelectionInterceptor() {
return new HostSelectionInterceptor();
}

@Provides @OkHttpInterceptors @Singleton @NonNull
public List<Interceptor> provideOkHttpInterceptors() {
return emptyList();
public List<Interceptor> provideOkHttpInterceptors(@NonNull HostSelectionInterceptor hostSelectionInterceptor) {
return singletonList(hostSelectionInterceptor);
}

@Provides @OkHttpNetworkInterceptors @Singleton @NonNull
Expand Down

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 889c8ad

Please sign in to comment.