-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 레트로핏 CallAdapter 적용
- Loading branch information
Showing
13 changed files
with
158 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 2 additions & 6 deletions
8
app/src/main/java/com/moyerun/moyeorun_android/network/api/ApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
package com.moyerun.moyeorun_android.network.api | ||
|
||
import com.moyerun.moyeorun_android.network.Failure | ||
import com.moyerun.moyeorun_android.network.Success | ||
import com.moyerun.moyeorun_android.network.callAdapter.NetworkResult | ||
import com.moyerun.moyeorun_android.network.calladapter.ApiResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
|
||
interface ApiService { | ||
@GET("users/{user}/repos") | ||
suspend fun getUserRepoList(@Path("user") user: String): NetworkResult<Success<Any>, Failure<Any>> | ||
|
||
@GET("appName") | ||
suspend fun getAppName(): NetworkResult<Success<Any>, Failure<Any>> | ||
suspend fun getUserRepoList(@Path("user") user: String): ApiResponse<Success<Any>> | ||
} |
47 changes: 0 additions & 47 deletions
47
...in/java/com/moyerun/moyeorun_android/network/callAdapter/NetworkResponseAdapterFactory.kt
This file was deleted.
Oops, something went wrong.
94 changes: 0 additions & 94 deletions
94
app/src/main/java/com/moyerun/moyeorun_android/network/callAdapter/NetworkResponseCall.kt
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
.../main/java/com/moyerun/moyeorun_android/network/callAdapter/NetworkResponseCallAdapter.kt
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
app/src/main/java/com/moyerun/moyeorun_android/network/callAdapter/NetworkResult.kt
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/moyerun/moyeorun_android/network/calladapter/ApiResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.moyerun.moyeorun_android.network.calladapter | ||
|
||
/** | ||
* Success : API 호출 성공 시, 데이터를 Wrapping 합니다. | ||
* Failure : API 호출 실패 시, 에러를 Wrapping 합니다. | ||
*/ | ||
sealed class ApiResponse<out T> { | ||
data class Success<T>(val data: T) : ApiResponse<T>() | ||
data class Failure(val error: Throwable) : ApiResponse<Nothing>() | ||
} |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/moyerun/moyeorun_android/network/calladapter/ApiResponseCall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.moyerun.moyeorun_android.network.calladapter | ||
|
||
import okhttp3.Request | ||
import okio.Timeout | ||
import retrofit2.* | ||
import java.lang.UnsupportedOperationException | ||
|
||
internal class ApiResponseCall<T : Any>( | ||
private val delegate: Call<T> | ||
) : Call<ApiResponse<T>> { | ||
|
||
override fun enqueue(callback: Callback<ApiResponse<T>>) { | ||
delegate.enqueue(object : Callback<T> { | ||
|
||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
if (response.isSuccessful) { | ||
val body = response.body() | ||
|
||
if (body != null) { | ||
callback.onResponse( | ||
this@ApiResponseCall, | ||
Response.success(ApiResponse.Success(body)) | ||
) | ||
} else { | ||
val invocation = call.request().tag(Invocation::class.java) | ||
val method = invocation?.method() | ||
val message = if (method != null) { | ||
"Response from " + | ||
method.declaringClass.name + | ||
'.' + | ||
method.name + | ||
" was null but response body type was declared as non-null" | ||
} else { | ||
"No tag is attached with Invocation::class.java. So, invocation can't find." | ||
} | ||
val e = KotlinNullPointerException(message) | ||
|
||
callback.onResponse( | ||
this@ApiResponseCall, | ||
Response.success(ApiResponse.Failure(e)) | ||
) | ||
} | ||
} else { | ||
callback.onResponse( | ||
this@ApiResponseCall, | ||
Response.success(ApiResponse.Failure(HttpException(response))) | ||
) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
callback.onResponse( | ||
this@ApiResponseCall, | ||
Response.success(ApiResponse.Failure(t)) | ||
) | ||
} | ||
}) | ||
} | ||
|
||
override fun clone(): Call<ApiResponse<T>> = ApiResponseCall(delegate.clone()) | ||
|
||
override fun execute(): Response<ApiResponse<T>> { | ||
throw UnsupportedOperationException("ApiResponseCall doesn't support execute") | ||
} | ||
|
||
override fun isExecuted(): Boolean = delegate.isExecuted | ||
|
||
override fun cancel() { | ||
delegate.cancel() | ||
} | ||
|
||
override fun isCanceled(): Boolean = delegate.isCanceled | ||
|
||
override fun request(): Request = delegate.request() | ||
|
||
override fun timeout(): Timeout = delegate.timeout() | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/moyerun/moyeorun_android/network/calladapter/ApiResponseCallAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.moyerun.moyeorun_android.network.calladapter | ||
|
||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import java.lang.reflect.Type | ||
|
||
class ApiResponseCallAdapter<T: Any>( | ||
private val returnType: Type | ||
) : CallAdapter<T, Call<ApiResponse<T>>> { | ||
|
||
override fun responseType(): Type = returnType | ||
|
||
override fun adapt(call: Call<T>): Call<ApiResponse<T>> = ApiResponseCall(call) | ||
} |
Oops, something went wrong.