-
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.
feature: Create Retrofit CallAdapter to convert the Response to ApiRe…
…sult in the interface
- Loading branch information
1 parent
601c2d8
commit 3c322a1
Showing
9 changed files
with
97 additions
and
18 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/nagpal/shivam/vtucslab/retrofit/ApiResultCall.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,39 @@ | ||
package com.nagpal.shivam.vtucslab.retrofit | ||
|
||
import com.nagpal.shivam.vtucslab.retrofit.ApiResult.ApiException | ||
import okhttp3.Request | ||
import okio.Timeout | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class ApiResultCall<T : Any>(private val proxy: Call<T>) : Call<ApiResult<T>> { | ||
override fun enqueue(callback: Callback<ApiResult<T>>) { | ||
proxy.enqueue(object : Callback<T> { | ||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
val apiResult = handleApiResult { response } | ||
callback.onResponse(this@ApiResultCall, Response.success(apiResult)) | ||
} | ||
|
||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
val apiResult = ApiException<T>(t) | ||
callback.onResponse(this@ApiResultCall, Response.success(apiResult)) | ||
} | ||
}) | ||
} | ||
|
||
override fun execute(): Response<ApiResult<T>> = throw NotImplementedError() | ||
|
||
override fun clone(): Call<ApiResult<T>> = ApiResultCall(proxy.clone()) | ||
|
||
override fun isExecuted(): Boolean = proxy.isExecuted | ||
|
||
override fun cancel() = proxy.cancel() | ||
|
||
override fun isCanceled(): Boolean = proxy.isCanceled | ||
|
||
override fun request(): Request = proxy.request() | ||
|
||
override fun timeout(): Timeout = proxy.timeout() | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/nagpal/shivam/vtucslab/retrofit/ApiResultCallAdapter.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,15 @@ | ||
package com.nagpal.shivam.vtucslab.retrofit | ||
|
||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import java.lang.reflect.Type | ||
|
||
class ApiResultCallAdapter(private val resultType: Type) : | ||
CallAdapter<Type, Call<ApiResult<Type>>> { | ||
override fun responseType(): Type = resultType | ||
|
||
override fun adapt(call: Call<Type>): Call<ApiResult<Type>> { | ||
return ApiResultCall(call) | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/nagpal/shivam/vtucslab/retrofit/ApiResultCallAdapterFactory.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,31 @@ | ||
package com.nagpal.shivam.vtucslab.retrofit | ||
|
||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
class ApiResultCallAdapterFactory private constructor() : CallAdapter.Factory() { | ||
override fun get( | ||
returnType: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit | ||
): CallAdapter<*, *>? { | ||
if (getRawType(returnType) != Call::class.java) { | ||
return null | ||
} | ||
|
||
val callType = getParameterUpperBound(0, returnType as ParameterizedType) | ||
if (getRawType(callType) != ApiResult::class.java) { | ||
return null | ||
} | ||
|
||
val resultType = getParameterUpperBound(0, callType as ParameterizedType) | ||
return ApiResultCallAdapter(resultType) | ||
} | ||
|
||
companion object { | ||
fun create(): ApiResultCallAdapterFactory = ApiResultCallAdapterFactory() | ||
} | ||
} |
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
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