rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.doumee.lib_coremodel.di.module;
 
 
import androidx.annotation.Nullable;
 
import com.doumee.lib_coremodel.config.AppliesOptions;
import com.doumee.lib_coremodel.http.InterceptorConfig;
import com.doumee.lib_coremodel.http.interceptor.LogInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.king.retrofit.retrofithelper.RetrofitHelper;
 
import javax.inject.Singleton;
 
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
 
/**
 * @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
 */
@InstallIn(SingletonComponent.class)
@Module
public class HttpModule {
 
 
    @Singleton
    @Provides
    Retrofit provideRetrofit(Retrofit.Builder builder, @Nullable AppliesOptions.RetrofitOptions options){
        if(options != null){
            options.applyOptions(builder);
        }
        return builder.build();
    }
 
    @Singleton
    @Provides
    OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, @Nullable AppliesOptions.OkHttpClientOptions options){
        if(options != null) {
            options.applyOptions(builder);
        }
        return builder.build();
    }
 
    @Singleton
    @Provides
    Gson provideGson(GsonBuilder builder, @Nullable AppliesOptions.GsonOptions options){
        if(options != null){
            options.applyOptions(builder);
        }
        return builder.create();
    }
 
    @Singleton
    @Provides
    Retrofit.Builder provideRetrofitBuilder(HttpUrl httpUrl, OkHttpClient client, Gson gson, InterceptorConfig config) {
        Retrofit.Builder builder = new Retrofit.Builder();
            builder.baseUrl(httpUrl)
                    .client(client);
            if(config.isAddGsonConverterFactory()){
                builder.addConverterFactory(GsonConverterFactory.create(gson));
            }
 
        return builder;
    }
 
    @Singleton
    @Provides
    OkHttpClient.Builder provideClientBuilder(InterceptorConfig config) {
        OkHttpClient.Builder builder = RetrofitHelper.getInstance().createClientBuilder();
 
        if(config.isAddLog()){
            builder.addInterceptor(new LogInterceptor());
        }
 
        return builder;
    }
 
    @Singleton
    @Provides
    GsonBuilder provideGsonBuilder(){
        return new GsonBuilder();
    }
 
    @Singleton
    @Provides
    InterceptorConfig provideInterceptorConfig(InterceptorConfig.Builder builder, @Nullable AppliesOptions.InterceptorConfigOptions options){
        if(options != null){
            options.applyOptions(builder);
        }
        return builder.build();
    }
 
    @Singleton
    @Provides
    InterceptorConfig.Builder provideInterceptorConfigBuilder(){
        return InterceptorConfig.newBuilder();
    }
 
 
}