Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Warren Smith naturalwarren

🎯
Focusing
View GitHub Profile
@naturalwarren
naturalwarren / KotlinRxJava2CallAdapterFactory.kt
Last active Jan 28, 2019
A CallAdapterFactory capable of creating NetworkResponse instances.
View KotlinRxJava2CallAdapterFactory.kt
/**
* A [CallAdapter.Factory] which allows [NetworkResponse] objects to be
* returned from RxJava streams created by Retrofit.
*
* Adding this class to [Retrofit] allows you to write service methods like:
*
* fun getTokens(): Single<NetworkResponse<AccessToken,Error>>
*/
class KotlinRxJava2CallAdapterFactory : CallAdapter.Factory() {
@naturalwarren
naturalwarren / ApiKotlin.kt
Last active Jan 24, 2019
Making a Retrofit API call with RxJava with a Kotlin-esque API
View ApiKotlin.kt
authApi.getTokens()
.subscribe { response : NetworkResponse<AccessToken, Error> ->
when (response) {
is NetworkResponse.Success<AccessToken> -> {
// A 2XX response that's guaranteed to have a body of type AccessToken.
}
is NetworkResponse.ServerError<Error> -> {
// A non-2XX response that may have an Error as its error body.
}
is NetworkResponse.NetworkError -> {
View CoinbaseRxJava2CallAdapterFactory.kt
/**
* Copyright 2019 Coinbase, Inc.
*
* 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,
View CoinbaseRxJava2CallAdapter.kt
/**
* Decorates stream emissions from [delegateAdapter] in [CoinbaseResponse].
*/
internal class CoinbaseRxJava2CallAdapter(
private val successBodyType: Type,
private val delegateAdapter: CallAdapter<Any, Any>,
private val errorConverter: Converter<ResponseBody, Any?>,
private val supportedType: SupportedType<Any, Any>
) : CallAdapter<Any, Any> {
View CoinbaseRxJava2CallAdapterFactory.kt
/**
* A [CallAdapter.Factory] which allows [CoinbaseResponse] objects to be
* returned from RxJava streams. For the type
* [Observable<CoinbaseResponse<SuccessBody, ErrorBody>>], the following
* semantics are provided:
*
* 1. 2xx responses call onNext with the deserialized body set as
* [CoinbaseResponse.body]
* 2. non-2xx responses call onNext with the deserialized error body set as
* [CoinbaseResponse.errorBody]
View CoinbaseResponse.kt
/**
* A response from the Coinbase API.
*
* In the event that a 2xx is returned [isSuccessful] will be set to true
* and [body] will be set.
* In the event that a non-2xx response is returned [isSuccessful] will be
* set to false and [errorBody] will be set.
* In the event that a request did not result in a response from the monorail
* [networkError] will be set.
*/
View CoinbaseRxJavaCallAdapter.kt
internal class CoinbaseRxJavaCallAdapter(
private val successBodyType: Type,
private val delegateAdapter: CallAdapter<Any, Any>,
private val errorConverterFactory: Converter<ResponseBody, Any?>,
private val isObservable: Boolean,
private val isFlowable: Boolean,
private val isSingle: Boolean,
private val isMaybe: Boolean
) : CallAdapter<Any, Any> {
View CoinbaseRxJavaCallAdapterFactory.kt
/**
* A [CallAdapter.Factory] which allows [CoinbaseResponse] objects to
* be returned from RxJava streams.
*
* Adding this class to [Retrofit] allows you to return [Observable],
* [Flowable], [Single], or [Maybe] types parameterized with [CoinbaseResponse]
* from service methods. This adapter must be registered before an adapter
* that is capable of adapting RxJava streams.
*
* For the type [Observable<CoinbaseResponse<SuccessBody, ErrorBody>>],
@naturalwarren
naturalwarren / CoinbaseResponse.kt
Last active Dec 2, 2018
Custom response class CoinbaseResponse.
View CoinbaseResponse.kt
/**
* A response class that holds a success and error body type as well
* as a throwable for network errors.
*
* @param T success response body type.
* @param U error response body type.
* @param body set when a call succeeds with a 2xx status code.
* @param errorBody set when a call fails with a non-2xx calls status code.
* @param networkError set when a network error occurs.
*/
@naturalwarren
naturalwarren / CoinbaseRxJavaCallAdapterFactory.kt
Created Dec 2, 2018
Call Adapter that provides type safe error bodies.
View CoinbaseRxJavaCallAdapterFactory.kt
/**
* A [CallAdapter.Factory] which allows [CoinbaseResponse] objects to be returned from RxJava
* streams.
*
* Adding this class to [Retrofit] allows you to return [Observable], [Flowable], [Single], or
* [Maybe] types parameterized with [CoinbaseResponse] from service methods. This adapter must be
* registered before an adapter that is capable of adapting RxJava streams.
*
* For the type [Observable<CoinbaseResponse<SuccessBody, ErrorBody>>], the following semantics are
* provided: