This file contains hidden or 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
val authApi: OAuthApi = Retrofit.newBuilder() | |
.baseUrl(COINBASE_URL) | |
.addConverterFactory(MoshiConverterFactory.create(moshi)) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) | |
.build() | |
.create() | |
interface OAuthApi { | |
/** |
This file contains hidden or 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
/** | |
* 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: |
This file contains hidden or 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
/** | |
* 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. | |
*/ |
This file contains hidden or 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
/** | |
* 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>>], |
This file contains hidden or 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
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> { |
This file contains hidden or 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
/** | |
* 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. | |
*/ |
This file contains hidden or 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
/** | |
* 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] |
This file contains hidden or 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
/** | |
* 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> { |
This file contains hidden or 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
/** | |
* 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, |
This file contains hidden or 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
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 -> { |