Retrofit Adapters
Sandwich
If you're interested in a more specified and lightweight Monad sealed API library for modeling Retrofit responses and handling exceptions, check out Sandwich.
Kotlin's Result
This library allows you to model your Retrofit responses with Kotlin's Result class.
Add the dependency below to your module's build.gradle
file:
dependencies {
implementation "com.github.skydoves:retrofit-adapters-result:1.0.0"
}
ResultCallAdapterFactory
You can return Kotlin's Result class to the Retrofit's service methods by setting ResultCallAdapterFactory
like the below:
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(..)
.addCallAdapterFactory(ResultCallAdapterFactory.create())
.build()
Then you can return the Result
class with the suspend keyword.
interface PokemonService {
@GET("pokemon")
suspend fun fetchPokemonList(
@Query("limit") limit: Int = 20,
@Query("offset") offset: Int = 0
): Result<PokemonResponse>
}
Finally, you will get the network response, which is wrapped by the Result
class like the below:
viewModelScope.launch {
val result = pokemonService.fetchPokemonList()
if (result.isSuccess) {
val data = result.getOrNull()
// handle data
} else {
// handle error case
}
}
Unit Tests by Injecting TestScope
You can also inject your custom CoroutineScope
into the ResultCallAdapterFactory
and execute network requests on the scope.
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
val testScope = TestScope(testDispatcher)
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(..)
.addCallAdapterFactory(ResultCallAdapterFactory.create(testScope))
.build()
Note: For more information about the Testing coroutines, check out the Testing Kotlin coroutines on Android.
Jetpack's Paging
This library allows you to return the paging source, which is parts of the Jetpack's Paging library.
Add the dependency below to your module's build.gradle
file:
dependencies {
implementation "com.github.skydoves:retrofit-adapters-paging:1.0.0"
}
PagingCallAdapterFactory
You can return Jetpack's PagingSource class to the Retrofit's service methods by setting PagingCallAdapterFactory
like the below:
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(..)
.addCallAdapterFactory(PagingCallAdapterFactory.create())
.build()
Then you can return the NetworkPagingSource
class with the @PagingKeyConfig
and @PagingKey
annotations:
interface PokemonService {
@GET("pokemon")
@PagingKeyConfig(
keySize = 20,
mapper = PokemonPagingMapper::class
)
suspend fun fetchPokemonListAsPagingSource(
@Query("limit") limit: Int = 20,
@PagingKey @Query("offset") offset: Int = 0,
): NetworkPagingSource<PokemonResponse, Pokemon>
}
PagingKeyConfig and PagingKey
To return the NetworkPagingSource
class, you must attach the @PagingKeyConfig
and @PagingKey
annotations to your Retrofit's service methods.
- @PagingKeyConfig: Contains paging configurations for the network request and delivery them to the call adapter internally. You should set the
keySize
andmapper
parameters. - @PagingKey: Marks the parameter in the service interface method as the paging key. This parameter will be paged by incrementing the page values continuously.
PagingMapper
You should create a paging mapper class, which extends the PagingMapper<T, R>
interface like the below for transforming the original network response to the list of paging items. This class should be used in the @PagingKeyConfig
annotation.
class PokemonPagingMapper : PagingMapper<PokemonResponse, Pokemon> {
override fun map(value: PokemonResponse): List<Pokemon> {
return value.results
}
}
You will get the network response, which is wrapped by the NetworkPagingSource
class like the below:
viewModelScope.launch {
val pagingSource = pokemonService.fetchPokemonListAsPagingSource()
val pagerFlow = Pager(PagingConfig(pageSize = 20)) { pagingSource }.flow
stateFlow.emitAll(pagerFlow)
}
Finally, you should call the submitData
method by your PagingDataAdapter
to bind the paging data. If you want to learn more about the Jetpack's Paging, check out the Paging library.
Arrow's Either
This library allows you to model your Retrofit responses with arrow-kt's Either class.
Add the dependency below to your module's build.gradle
file:
dependencies {
implementation "com.github.skydoves:retrofit-adapters-arrow:1.0.0"
}
EitherCallAdapterFactory
You can return Arrow's Either class to the Retrofit's service methods by setting EitherCallAdapterFactory
like the below:
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(..)
.addCallAdapterFactory(EitherCallAdapterFactory.create())
.build()
Then you can return the Either
class with the suspend keyword.
interface PokemonService {
@GET("pokemon")
suspend fun fetchPokemonListAsEither(
@Query("limit") limit: Int = 20,
@Query("offset") offset: Int = 0
): Either<Throwable, PokemonResponse>
}
Finally, you will get the network response, which is wrapped by the Either
class like the below:
viewModelScope.launch {
val either = pokemonService.fetchPokemonListAsEither()
if (either.isRight()) {
val data = either.orNull()
// handle data
} else {
// handle error case
}
}
Unit Tests by Injecting TestScope
You can also inject your custom CoroutineScope
into the EitherCallAdapterFactory
and execute network requests on the scope.
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
val testScope = TestScope(testDispatcher)
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(..)
.addCallAdapterFactory(EitherCallAdapterFactory.create(testScope))
.build()
Note: For more information about the Testing coroutines, check out the Testing Kotlin coroutines on Android.
β€οΈ
Find this repository useful? Support it by joining stargazers for this repository.
Also, follow me on GitHub for my next creations!
License
Designed and developed by 2022 skydoves (Jaewoong Eum)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.