@@ -2,65 +2,183 @@ package com.stop.data.di
2
2
3
3
import com.squareup.moshi.Moshi
4
4
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
5
- import com.stop.data.remote.ResponseAdapterFactory
6
- import com.stop.data.remote.network.NearPlaceApiService
7
5
import com.stop.data.BuildConfig
6
+ import com.stop.data.remote.ResultCallAdapter
7
+ import com.tickaroo.tikxml.TikXml
8
+ import com.tickaroo.tikxml.retrofit.TikXmlConverterFactory
8
9
import dagger.Module
9
10
import dagger.Provides
10
11
import dagger.hilt.InstallIn
11
12
import dagger.hilt.components.SingletonComponent
13
+ import okhttp3.Interceptor
14
+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
12
15
import okhttp3.OkHttpClient
16
+ import okhttp3.Protocol
17
+ import okhttp3.Response
18
+ import okhttp3.ResponseBody.Companion.toResponseBody
13
19
import okhttp3.logging.HttpLoggingInterceptor
14
20
import retrofit2.Retrofit
15
21
import retrofit2.converter.moshi.MoshiConverterFactory
16
- import java.util.concurrent.TimeUnit
22
+ import javax.inject.Named
17
23
import javax.inject.Singleton
18
24
19
- @Module
20
25
@InstallIn(SingletonComponent ::class )
26
+ @Module
21
27
internal object NetworkModule {
22
28
29
+ private const val T_MAP_APP_KEY_NAME = " appKey"
30
+ private const val OPEN_API_SEOUL_KEY_NAME = " KEY"
31
+ private const val APIS_KEY_NAME = " ServiceKey"
32
+ private const val WS_KEY_NAME = " ServiceKey"
33
+
34
+ private const val T_MAP_ROUTE_URL = " transit/routes"
35
+
36
+ /* *
37
+ * resources에 fake 데이터가 담긴 파일을 넣어줘야 Fake TMap이 정상적으로 동작합니다.
38
+ */
39
+ private const val FAKE_JSON_URI = " response.json"
40
+
23
41
@Provides
24
42
@Singleton
25
- fun provideHttpClient (): OkHttpClient {
43
+ fun provideOkHttpClient (
44
+ customInterceptor : CustomInterceptor ,
45
+ loggingInterceptor : HttpLoggingInterceptor ,
46
+ ): OkHttpClient {
26
47
return OkHttpClient .Builder ()
27
- .readTimeout(10 , TimeUnit .SECONDS )
28
- .connectTimeout(10 , TimeUnit .SECONDS )
29
- .writeTimeout(15 , TimeUnit .SECONDS )
30
- .addInterceptor(getLoggingInterceptor())
48
+ .addInterceptor(loggingInterceptor)
49
+ .addInterceptor(customInterceptor)
31
50
.build()
32
51
}
33
52
34
- private fun getLoggingInterceptor (): HttpLoggingInterceptor =
35
- HttpLoggingInterceptor ().apply {
36
- level = HttpLoggingInterceptor .Level .BODY
37
- }
38
53
39
54
@Provides
40
55
@Singleton
41
- fun provideMoshi (): Moshi =
42
- Moshi .Builder ()
43
- .add (KotlinJsonAdapterFactory ())
56
+ fun provideMoshi (): Moshi {
57
+ return Moshi .Builder ()
58
+ .addLast (KotlinJsonAdapterFactory ())
44
59
.build()
60
+ }
45
61
62
+ @Provides
46
63
@Singleton
64
+ fun provideTikXmlConverterFactory (): TikXmlConverterFactory {
65
+ return TikXmlConverterFactory .create(TikXml .Builder ().exceptionOnUnreadXml(false ).build())
66
+ }
67
+
68
+ @Provides
69
+ fun provideCustomInterceptor (): CustomInterceptor {
70
+ return CustomInterceptor ()
71
+ }
72
+
47
73
@Provides
48
- fun provideRetrofitInstance (
74
+ fun provideHttpLoggingInterceptor (): HttpLoggingInterceptor {
75
+ return HttpLoggingInterceptor ().setLevel(HttpLoggingInterceptor .Level .BODY )
76
+ }
77
+
78
+ @Provides
79
+ fun provideResultCallAdapter (): ResultCallAdapter .Factory {
80
+ return ResultCallAdapter .Factory ()
81
+ }
82
+
83
+ @Provides
84
+ @Named(" Tmap" )
85
+ fun provideTmapRetrofitInstance (
49
86
okHttpClient : OkHttpClient ,
50
- moshi : Moshi
87
+ moshi : Moshi ,
88
+ resultCallAdapter : ResultCallAdapter .Factory ,
51
89
): Retrofit {
52
90
return Retrofit .Builder ()
53
- .baseUrl(BuildConfig .TMAP_URL )
91
+ .baseUrl(BuildConfig .T_MAP_URL )
54
92
.client(okHttpClient)
55
- .addCallAdapterFactory(ResponseAdapterFactory () )
93
+ .addCallAdapterFactory(resultCallAdapter )
56
94
.addConverterFactory(MoshiConverterFactory .create(moshi))
57
95
.build()
58
96
}
59
97
60
98
@Provides
61
- @Singleton
62
- fun providePlaceApiService (retrofit : Retrofit ): NearPlaceApiService {
63
- return retrofit.create(NearPlaceApiService ::class .java)
99
+ @Named(" OpenApiSeoul" )
100
+ fun provideOpenApiSeoulRetrofitInstance (
101
+ okHttpClient : OkHttpClient ,
102
+ moshi : Moshi ,
103
+ resultCallAdapter : ResultCallAdapter .Factory ,
104
+ ): Retrofit {
105
+ return Retrofit .Builder ()
106
+ .baseUrl(BuildConfig .OPEN_API_SEOUL_URL )
107
+ .client(okHttpClient)
108
+ .addCallAdapterFactory(resultCallAdapter)
109
+ .addConverterFactory(MoshiConverterFactory .create(moshi))
110
+ .build()
64
111
}
65
112
113
+ @Provides
114
+ @Named(" WsBus" )
115
+ fun provideWsBusRetrofitInstance (
116
+ okHttpClient : OkHttpClient ,
117
+ moshi : Moshi ,
118
+ resultCallAdapter : ResultCallAdapter .Factory ,
119
+ ): Retrofit {
120
+ return Retrofit .Builder ()
121
+ .baseUrl(BuildConfig .WS_BUS_URL )
122
+ .client(okHttpClient)
123
+ .addCallAdapterFactory(resultCallAdapter)
124
+ .addConverterFactory(MoshiConverterFactory .create(moshi))
125
+ .build()
126
+ }
127
+
128
+ @Provides
129
+ @Named(" ApisData" )
130
+ fun provideApisDataRetrofitInstance (
131
+ okHttpClient : OkHttpClient ,
132
+ tikXmlConverterFactory : TikXmlConverterFactory ,
133
+ resultCallAdapter : ResultCallAdapter .Factory ,
134
+ ): Retrofit {
135
+ return Retrofit .Builder ()
136
+ .baseUrl(BuildConfig .APIS_URL )
137
+ .client(okHttpClient)
138
+ .addCallAdapterFactory(resultCallAdapter)
139
+ .addConverterFactory(tikXmlConverterFactory)
140
+ .build()
141
+ }
142
+
143
+ class CustomInterceptor : Interceptor {
144
+ override fun intercept (chain : Interceptor .Chain ): Response {
145
+ val url = chain.request().url.toUri().toString()
146
+
147
+ if (url.contains(T_MAP_ROUTE_URL )) {
148
+ val response = readJson(FAKE_JSON_URI )
149
+ return chain.proceed(chain.request())
150
+ .newBuilder()
151
+ .code(200 )
152
+ .protocol(Protocol .HTTP_2 )
153
+ .message(" success" )
154
+ .body(
155
+ response.toByteArray()
156
+ .toResponseBody(" application/json" .toMediaTypeOrNull())
157
+ ).addHeader(" content-type" , " application/json" )
158
+ .build()
159
+ }
160
+
161
+ val (name: String , key: String ) = when {
162
+ url.contains(BuildConfig .OPEN_API_SEOUL_URL ) -> Pair (OPEN_API_SEOUL_KEY_NAME , BuildConfig .BUS_KEY )
163
+ url.contains(BuildConfig .T_MAP_URL ) -> Pair (T_MAP_APP_KEY_NAME , BuildConfig .T_MAP_APP_KEY )
164
+ url.contains(BuildConfig .APIS_URL ) -> Pair (APIS_KEY_NAME , BuildConfig .BUS_KEY )
165
+ url.contains(BuildConfig .WS_BUS_URL ) -> Pair (WS_KEY_NAME , BuildConfig .BUS_KEY )
166
+ else -> {
167
+ return chain.proceed(chain.request())
168
+ }
169
+ }
170
+
171
+ return with (chain) {
172
+ val newRequest = request().newBuilder()
173
+ .addHeader(name, key)
174
+ .build()
175
+ proceed(newRequest)
176
+ }
177
+ }
178
+
179
+ private fun readJson (fileName : String ): String {
180
+ return Thread .currentThread().contextClassLoader?.getResource(fileName)
181
+ ?.readText() ? : " "
182
+ }
183
+ }
66
184
}
0 commit comments