Skip to content

Commit

Permalink
[#17] 통신을 위한 네트워크 관련 베이스 코드 작성 - 컨버터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-223 committed Aug 4, 2022
1 parent d9f6263 commit 6424263
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies {
// Retrofit2
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation "com.squareup.okhttp3:okhttp:4.10.0"

// Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.moyerun.moyeorun_android.network

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import com.moyerun.moyeorun_android.network.api.Success
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody
import okio.Buffer
import retrofit2.Converter
import retrofit2.Retrofit
import java.io.IOException
import java.io.OutputStreamWriter
import java.lang.reflect.Type
import java.nio.charset.Charset

class MoyeorunJsonConverterFactory private constructor(
private val gson: Gson
) : Converter.Factory() {

override fun responseBodyConverter(
type: Type,
annotations: Array<out Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *> {
val adapter = gson.getAdapter(TypeToken.get(type))
return MoyeorunResponseBodyConverter(gson, adapter)
}

override fun requestBodyConverter(
type: Type,
parameterAnnotations: Array<out Annotation>,
methodAnnotations: Array<out Annotation>,
retrofit: Retrofit
): Converter<*, RequestBody> {
val adapter = gson.getAdapter(TypeToken.get(type))
return MoyeorunRequestBodyConverter(gson, adapter)
}

companion object {
fun create(): MoyeorunJsonConverterFactory {
return MoyeorunJsonConverterFactory(Gson())
}
}

class MoyeorunResponseBodyConverter<T>(
private val gson: Gson,
private val adapter: TypeAdapter<T>
) : Converter<ResponseBody, T> {

override fun convert(value: ResponseBody): T? {
val jsonReader = gson.newJsonReader(value.charStream())
try {
val result: Success<T> = gson.fromJson(jsonReader, Success::class.java)
val data = gson.toJson(result.data)
return adapter.fromJson(data)
} catch (e: Exception) {
throw IOException(e)
} finally {
value.close()
}
}
}

class MoyeorunRequestBodyConverter<T>(
private val gson: Gson,
private val adapter: TypeAdapter<T>
): Converter<T, RequestBody> {

@Throws(IOException::class)
override fun convert(value: T): RequestBody {
val buffer = Buffer()
val writer = OutputStreamWriter(buffer.outputStream(), UTF_8)
val jsonWriter = gson.newJsonWriter(writer)
adapter.write(jsonWriter, value)
jsonWriter.close()
return buffer.readByteString().toRequestBody(MEDIA_TYPE)
}

companion object {
private val MEDIA_TYPE: MediaType = "application/json; charset=UTF-8".toMediaType()
private val UTF_8 = Charset.forName("UTF-8")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ApiResultCall<S>(
override fun enqueue(callback: Callback<ApiResult<S>>) {
delegate.enqueue(object : Callback<S> {
override fun onResponse(call: Call<S>, response: Response<S>) {
val requestUrl = delegate.request().url().toString()
val requestUrl = delegate.request().url.toString()

// status code 200번대.
if (response.isSuccessful) {
Expand Down Expand Up @@ -76,7 +76,7 @@ class ApiResultCall<S>(
Response.success(
ApiResult.Failure(
NetworkException(
call.request().url().toString(),
call.request().url.toString(),
throwable
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.moyerun.moyeorun_android.network.client

import com.moyerun.moyeorun_android.BuildConfig
import com.moyerun.moyeorun_android.network.calladapter.ApiResultCallAdapterFactory
import com.moyerun.moyeorun_android.network.MoyeorunJsonConverterFactory
import com.moyerun.moyeorun_android.network.api.ApiService
import com.moyerun.moyeorun_android.network.calladapter.ApiResultCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

private const val BASE_URL = BuildConfig.BASE_URL

val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(ApiResultCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(MoyeorunJsonConverterFactory.create())
.build()

val apiService: ApiService = retrofit.create(ApiService::class.java)

0 comments on commit 6424263

Please sign in to comment.