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
/** | |
* 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>>], 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
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
authApi.getTokens() | |
.subscribe({ response: Response<AccessToken> -> | |
when { | |
response.isSuccessful && response.body() != null -> { | |
// Case 1: Success. We got a response with a body. | |
} | |
response.errorBody() != null -> { | |
// Case 2: Failure. We got an error from the backend, deserialize it. | |
val error = moshi.adapter(Error::class.java).fromJson(errorBody.source()) | |
} |
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
class AccessTokenInterceptor( | |
private val tokenProvider: AccessTokenProvider | |
) : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val token = tokenProvider.token() | |
return if (token == null) { | |
chain.proceed(chain.request()) | |
} else { |
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
/** | |
* Provides an access token for request authorization. | |
*/ | |
interface AccessTokenProvider { | |
/** | |
* Returns an access token. In the event that you don't have a token return null. | |
*/ | |
fun token(): String? |
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
/** | |
* Authenticator that attempts to refresh the client's access token. | |
* In the event that a refresh fails and a new token can't be issued an error | |
* is delivered to the caller. This authenticator blocks all requests while a token | |
* refresh is being performed. In-flight requests that fail with a 401 are | |
* automatically retried. | |
*/ | |
class AccessTokenAuthenticator( | |
private val tokenProvider: AccessTokenProvider | |
) : Authenticator { |
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
@Module | |
abstract class AppModule { | |
@Module | |
companion object { | |
private const val HTTP_RESPONSE_CACHE = (10 * 1024 * 1024).toLong() | |
@AppScope @Provides @JvmStatic | |
fun httpClient(cache: Cache): OkHttpClient { |
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
@Provides | |
fun retrofit(okHttpClient: Lazy<OkHttpClient>) = Retrofit.Builder() | |
.baseUrl("https://sandbox.tradier.com/v1/") | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) | |
.callFactory { okHttpClient.get().newCall(it) } | |
.build() |