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

111 fixed, updated OkHttp 3.2.0->3.9.0 #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,19 +1,17 @@
package com.artemzin.qualitymatters.network;

import android.support.annotation.NonNull;

import com.facebook.stetho.okhttp3.StethoInterceptor;

import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;
import timber.log.Timber;

import javax.inject.Singleton;
import java.util.Arrays;
import java.util.List;

import static java.util.Collections.singletonList;

/**
Expand All @@ -28,9 +26,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that the proper indentation for this project?

return Arrays.asList(httpLoggingInterceptor, hostSelectionInterceptor);
}

@Provides @OkHttpNetworkInterceptors @Singleton @NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.artemzin.qualitymatters.functional_tests.rules;

import android.support.annotation.NonNull;

import com.artemzin.qualitymatters.functional_tests.TestUtils;

import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.lang.reflect.Method;

import okhttp3.mockwebserver.MockWebServer;

/**
* JUnit test rule for mocking web server!
*/
Expand All @@ -36,7 +33,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().setHost(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 @@ -3,19 +3,17 @@
import com.artemzin.qualitymatters.QualityMattersIntegrationRobolectricTestRunner;
import com.artemzin.qualitymatters.api.QualityMattersRestApi;
import com.artemzin.qualitymatters.api.entities.Item;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import retrofit2.adapter.rxjava.HttpException;

import java.io.IOException;
import java.util.List;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import retrofit2.adapter.rxjava.HttpException;

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

Expand All @@ -39,7 +37,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().setHost(mockWebServer.url("").toString());

qualityMattersRestApi = QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().qualityMattersApi();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.artemzin.qualitymatters;

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.DevMetricsProxy;
import com.artemzin.qualitymatters.developer_settings.DeveloperSettingsComponent;
Expand All @@ -12,18 +10,18 @@
import com.artemzin.qualitymatters.developer_settings.LeakCanaryProxy;
import com.artemzin.qualitymatters.models.AnalyticsModel;
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;
import com.artemzin.qualitymatters.performance.AsyncJobsObserver;
import com.artemzin.qualitymatters.ui.activities.MainActivity;
import com.artemzin.qualitymatters.ui.fragments.ItemsFragment;
import com.google.gson.Gson;
import dagger.Component;

import javax.inject.Singleton;

import dagger.Component;

@Singleton
@Component(modules = {
ApplicationModule.class,
Expand All @@ -44,9 +42,6 @@ public interface ApplicationComponent {
@NonNull
QualityMattersRestApi qualityMattersApi();

@NonNull
ChangeableBaseUrl changeableBaseUrl();

// Provide AsyncJobObserver from the real app to the tests without need in injection to the test.
@NonNull
AsyncJobsObserver asyncJobsObserver();
Expand All @@ -68,5 +63,7 @@ public interface ApplicationComponent {

DevMetricsProxy devMetricsProxy();

HostSelectionInterceptor hostSelectionInterceptor();

void inject(@NonNull MainActivity mainActivity);
}
23 changes: 9 additions & 14 deletions app/src/main/java/com/artemzin/qualitymatters/api/ApiModule.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package com.artemzin.qualitymatters.api;

import android.support.annotation.NonNull;

import com.artemzin.qualitymatters.BuildConfig;
import com.google.gson.Gson;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import javax.inject.Singleton;

@Module
public class ApiModule {

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

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

@Provides @NonNull @Singleton
public ChangeableBaseUrl provideChangeableBaseUrl() {
return changeableBaseUrl;
this.baseUrl = baseUrl;
}

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

This file was deleted.

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

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;

import java.io.IOException;
import java.net.URI;

/**
* An interceptor that allows runtime changes to the URL hostname.
* As per https://gist.github.com/swankjesse/8571a8207a5815cca1fb
*/
public final class HostSelectionInterceptor implements Interceptor {
/**
* Using static variable in order to avoid adding this interceptor to ApplicationComponent

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems to be out of sync with code

*/
private volatile String host;

public void setHost(@Nullable String host) {
this.host = host;
}

@Override
public okhttp3.Response intercept(@NonNull Chain chain) throws IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import Response?

Request request = chain.request();
String host = this.host;
if (host != null) {
URI uri = URI.create(host);
HttpUrl newUrl = request.url().newBuilder()
.host(uri.getHost())
.port(uri.getPort())
.scheme("http") //in order to avoid SSL Handshake failure

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow wow, this is pretty dangerous code in case if someone will copy-paste it and use to switch to lets say "staging/prod" environment in the app

I'd prefer to store schema in base url that we want to swap to, maybe use HttpUrl class instead of String and get basic info like host, schema and port

.build();
request = request.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.artemzin.qualitymatters.network;

import android.support.annotation.NonNull;

import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

import javax.inject.Singleton;
import java.util.List;

@Module
public class NetworkModule {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.artemzin.qualitymatters.network;

import android.support.annotation.NonNull;

import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;

import javax.inject.Singleton;
import java.util.List;

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

/**
* Provides OkHttp interceptors for release build.
Expand All @@ -19,12 +18,17 @@
public class OkHttpInterceptorsModule {

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

@Provides @OkHttpNetworkInterceptors @Singleton @NonNull
public List<Interceptor> provideOkHttpNetworkInterceptors() {
return emptyList();
}

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

This file was deleted.

2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext.versions = [
rxJavaProguardRules : '1.1.8.0',
rxLint : '1.6',
supportLibs : '23.1.1',
okHttp : '3.2.0',
okHttp : '3.9.0',
retrofit : '2.0.0-beta4',
gson : '2.7',
autoValue : '1.2',
Expand Down
2 changes: 2 additions & 0 deletions proguard/proguard-square-okhttp.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# OkHttp3
-dontwarn okhttp3.**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need more rules. there's one more for the publicsuffixdatabase thing. they've got it documented.