Last active
March 16, 2023 21:52
-
-
Save alwarren/9e2b18abe9af7dd4a1ea39442ed6945a to your computer and use it in GitHub Desktop.
Using generics with Retrofit repositories. A work in progress.
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
package com.example.domain.api | |
import retrofit2.Call | |
import retrofit2.Callback | |
import retrofit2.Response | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
open class RetrofitRepo<out S>(clazz: Class<S>) { | |
val service: S = Api.service(clazz) | |
fun <T> enqueue(call: Call<T>, listener: ApiListener<T>) { | |
call.enqueue(object : Callback<T> { | |
override fun onResponse(call: Call<T>, response: Response<T>) { | |
val data = response.body() | |
if (response.isSuccessful) { | |
if (data != null) { | |
println("SUCCESS") | |
listener.onDataLoaded(data) | |
} | |
} else { | |
// error response, no access to resource? | |
println("FAIL: Response Unsuccessful") | |
} | |
} | |
override fun onFailure(call: Call<T>, error: Throwable) { | |
// something went completely south (like no internet connection) | |
// handle failure | |
println("FAIL: Call Failed") | |
println(error.message) | |
} | |
}) | |
} | |
fun <T> execute(call: Call<T>): T? { | |
var data: T? = null | |
try { | |
val result = call.execute() | |
println("SUCCESS") | |
if (result?.body() != null) | |
data = result.body()!! | |
} catch (e: Exception) { | |
println("FAIL: Call Failed") | |
println(e.message) | |
} | |
return data | |
} | |
object Api { | |
fun <T> service(clazz: Class<T>): T { | |
return Retrofit.Builder() | |
.baseUrl("https://api.spacexdata.com/") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build().create(clazz) | |
} | |
} | |
} |
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
package com.example.domain.rocket.api | |
import com.example.domain.api.ApiListener | |
import com.example.domain.api.RetrofitRepo | |
import com.example.domain.rocket.Rocket | |
class RocketRepo : RetrofitRepo<RocketService>(RocketService::class.java) { | |
fun loadRocket(id: String, listener: ApiListener<Rocket>) { | |
enqueue(service.loadRocket(id), listener) | |
} | |
fun getRocket(id: String): Rocket? { | |
return execute(service.loadRocket(id)) | |
} | |
fun loadRockets(listener: ApiListener<Array<Rocket>>) { | |
enqueue(service.loadRockets(), listener) | |
} | |
fun getRockets(): Array<Rocket>? { | |
return execute(service.loadRockets()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for saving my time, i finally understood how to do it!