Skip to content

Instantly share code, notes, and snippets.

View naturalwarren's full-sized avatar
🎯
Focusing

Warren Smith naturalwarren

🎯
Focusing
  • Waterloo Canada
View GitHub Profile
@naturalwarren
naturalwarren / OAuthApi.kt
Last active December 2, 2018 19:25
Demonstrates
val authApi: OAuthApi = Retrofit.newBuilder()
.baseUrl(COINBASE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
.create()
interface OAuthApi {
/**
@naturalwarren
naturalwarren / CoinbaseRxJavaCallAdapterFactory.kt
Created December 2, 2018 19:33
Call Adapter that provides type safe error bodies.
/**
* 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:
@naturalwarren
naturalwarren / CoinbaseResponse.kt
Last active December 2, 2018 21:17
Custom response class CoinbaseResponse.
/**
* 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.
*/
/**
* 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 / CoinbaseRxJavaCallAdapter.kt
Created December 2, 2018 21:42
A custom CallAdapter.
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> {
/**
* 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.
*/
/**
* 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]
/**
* 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> {
@naturalwarren
naturalwarren / CoinbaseRxJava2CallAdapterFactory.kt
Last active March 16, 2022 04:58
A Kotlin-esque API for Retrofit.
/**
* 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,
@naturalwarren
naturalwarren / ApiKotlin.kt
Last active January 24, 2019 00:58
Making a Retrofit API call with RxJava with a Kotlin-esque API
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 -> {