Retrogrid is a library built on top of retrofit library for the easy intergation of your application.
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2. Add the dependency in your app module
dependencies {
implementation 'com.github.DeepuGeorgeJacob:retrogrid:v1.0.2'
}
There are two ways(property file and annotation) to use this this library. Easy way is by creating annotation
- Create your repository like below
@RetrofitServiceConfiguration(baseUrl = "http://example.com/v1/")
interface RetrogridService {
@GET(value = "method_url")
@ErrorResponseMap(errorClass = NewsErrorResponse::class)
suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>
}
@RetrofitServiceConfiguration
is the annotation that you can add your base url.
@ErrorResponseMap
Which you can mention your class which hold error response. Error Response will be different for different project your back end team define the error structure.
Eg:-
data class NewsErrorResponse(
private val status: String,
private val code: String,
private val message: String
)
Make sure your method to get the data is suspended
function.
Eg:-
suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>
RetrogridResponse
is the class which containing your response object or available error objects if available. You should pass your success response class as typed argument. Here NewsOverview
is my response data class.
Eg:->
data class NewsOverview(
val status: String,
val totalResults: Int,
val articles:List<String>
)
- Create your repository like this.
class NewsRepository(
private val service: RetrogridService = RetroGridNetworkClient.buildService(RetrogridService::class.java)
) {
suspend fun getNewsOverView() = service.fetchNewsOverview()
}
Congratulations you are done with 90%
- Call the api from your viewmodel.
class RetrogridViewModel(private val repository: NewsRepository = NewsRepository()) : ViewModel() {
init {
viewModelScope.launch {
when (val result = repository.getNewsOverView()) {
is RetrogridResponse.Success -> println("SUCCESS ${result.responseBody}")
is RetrogridResponse.Failure -> {
println((result.errorResponseBody as NewsErrorResponse).toString())
}
is RetrogridResponse.NetworkError -> println("Network error : "+result.errorMessage)
is RetrogridResponse.UnknownError -> println("Unknown error : "+result.errorMessage)
}
}
}
}
Notice how you should handle success and error responses.
If you have to add static base url, Please follow the below steps.
- You should create a file called
retrofit.properties
in the src/main/resources directory of the app module. - In the app module's build.gradle file, add the following lines to make sure the properties file is included in the build:
android {
// ...
sourceSets {
main {
resources {
srcDirs = ['src/main/resources']
}
}
}
}
-
Add the property value with key
api.base.url
in yourretrofit.properties
file. Example:-api.base.url=https://example.org/v2/
-
Now you can use service without
@RetrofitServiceConfiguration
annotation.
@RetrofitServiceConfiguration(baseUrl = "https://example.com/v1/")
interface RetrogridService {
@GET(value = "method_url")
@ErrorResponseMap(errorClass = NewsErrorResponse::class)
suspend fun fetchNewsOverview():RetrogridResponse<NewsOverview>
}
Note : If you configure with both annotation and retrofit.properties
file, then annotation will override base url value. Which mean if you want to change the base url for any specific services you should use @RetrofitServiceConfiguration
annotation.