【问题标题】:How to support multiple endpoints with Dagger 2.0 while using MockWebServer如何在使用 MockWebServer 时使用 Dagger 2.0 支持多个端点
【发布时间】:2016-12-08 02:38:38
【问题描述】:

对于一个 Android 项目,我使用 Dagger 2.6 配置了 Retrofit 2.1.0 和 OkHttp 3.4.1,如下面的 Dagger 模块所示。我的目标是使用@Named 限定符支持多个后端。

@Module
public class ApiModule {

    private static final String GITHUB_QUALIFIER = "GitHub";

    private static final String TWITTER_QUALIFIER = "Twitter";

    @Provides
    GitHubClient provideGitHubClient(@Named(GITHUB_QUALIFIER) Retrofit retrofit) { /* ... */ }

    @Provides
    TwitterClient provideTwitterClient(@Named(TWITTER_QUALIFIER) Retrofit retrofit) { /* ... */ }

    @Named(GITHUB_QUALIFIER)
    @Provides
    @Singleton
    HttpUrl provideGitHubBaseUrl() { /* ... */ }

    @Named(TWITTER_QUALIFIER)
    @Provides
    @Singleton
    HttpUrl provideTwitterBaseUrl() { /* ... */ }

    @Named(GITHUB_QUALIFIER)
    @Provides
    @Singleton
    Retrofit getGitHubRetrofit(@Named(GITHUB_QUALIFIER) HttpUrl httpUrl, 
                               OkHttpClient okHttpClient) { /* ... */ }

    @Named(TWITTER_QUALIFIER)
    @Provides
    @Singleton
    Retrofit getTwitterRetrofit(@Named(TWITTER_QUALIFIER) HttpUrl httpUrl, 
                                OkHttpClient okHttpClient) { /* ... */ }

    private Retrofit getRetrofit(String baseUrl, 
                                 OkHttpClient okHttpClient) { /* ... */ }

    @Provides
    @Singleton
    public OkHttpClient provideOkHttpClient() { /* ... */ }

}

我想使用MockWebServer 进行测试。但是,我不知道如何在支持多个后端的同时传入 MockWebServer 的 URL:

// From a unit test

mockWebServer = new MockWebServer();
mockWebServer.start();
ApiModule apiModule = new ApiModule();
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

String url = mockWebServer.url("/").toString();
// Here I cannot pass the URL to Retrofit
Retrofit retrofit = apiModule.provideRetrofit(/* HERE */ okHttpClient);

gitHubClient = apiModule.provideGitHubClient(retrofit);

【问题讨论】:

    标签: android retrofit2 dagger-2 okhttp3 mockwebserver


    【解决方案1】:

    可以使用 okhttp 拦截器覆盖基本 url。

    public final class HostSelectionInterceptor implements Interceptor {
        private volatile String mHost;
    
        public void setHost(String host) {
            this.mHost = checkNotNull(host);
        }
    
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            String host = this.mHost;
            if (host != null) {
                HttpUrl overriddenBaseUrl = HttpUrl.parse(host);
                HttpUrl newUrl = request.url()
                        .newBuilder()
                        .host(overriddenBaseUrl.host())
                        .build();
                request = request.newBuilder()
                        .url(newUrl)
                        .build();
            }
            return chain.proceed(request);
        }
    }
    

    取自这里:https://gist.github.com/swankjesse/8571a8207a5815cca1fb

    您可以使用此拦截器来提供 MockWebServer url,甚至可以在暂存端点之间切换以进行调试构建。

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2017-08-06
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多