Skip to content

Commit

Permalink
fix: Update RemoteDataSource for CTXSpend
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Feb 2, 2024
1 parent 8d2fdc3 commit 7da3ecd
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.dash.wallet.features.exploredash.network.service.DashDirectAuthApi
import org.dash.wallet.features.exploredash.network.service.DashDirectServicesApi
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendApi
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendDataSource
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendTokenApi
import org.dash.wallet.features.exploredash.repository.*
import org.dash.wallet.features.exploredash.services.UserLocationState
import org.dash.wallet.features.exploredash.services.UserLocationStateInt
Expand Down Expand Up @@ -70,7 +71,7 @@ abstract class ExploreDashModule {
return CTXSpendDataSource(userConfiguration, config)
}

fun provideRemoteDataSource(config: DashDirectConfig): RemoteDataSource {
fun provideRemoteDataSource(config: CTXSpendConfig): RemoteDataSource {
return RemoteDataSource(config)
}

Expand All @@ -87,6 +88,11 @@ abstract class ExploreDashModule {
fun provideApi(ctxSpendDataSource: CTXSpendDataSource): CTXSpendApi {
return ctxSpendDataSource.buildApi(CTXSpendApi::class.java)
}

@Provides
fun provideCTXAuthApi(remoteDataSource: RemoteDataSource): CTXSpendTokenApi {
return remoteDataSource.buildApi(CTXSpendTokenApi::class.java)
}
}

@Binds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,40 @@ import okhttp3.Authenticator
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.dash.wallet.features.exploredash.network.interceptor.HeadersInterceptor
import org.dash.wallet.features.exploredash.utils.DashDirectConfig
import org.dash.wallet.features.exploredash.utils.DashDirectConstants
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendTokenApi
import org.dash.wallet.features.exploredash.repository.remote.TokenAuthenticator
import org.dash.wallet.features.exploredash.utils.CTXSpendConfig
import org.dash.wallet.features.exploredash.utils.CTXSpendConstants
import org.slf4j.LoggerFactory
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration

class RemoteDataSource @Inject constructor(private val config: DashDirectConfig) {
class RemoteDataSource @Inject constructor(private val config: CTXSpendConfig) {
companion object {
private val log = LoggerFactory.getLogger(RemoteDataSource::class.java)
}

fun <Api> buildApi(api: Class<Api>): Api {
return Retrofit.Builder()
.baseUrl(DashDirectConstants.BASE_URL)
.client(getOkHttpClient())
.baseUrl(CTXSpendConstants.BASE_URL)
.client(getOkHttpClient(TokenAuthenticator(buildTokenApi(), config)))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(api)
}

private fun buildTokenApi(): CTXSpendTokenApi {
return Retrofit.Builder()
.baseUrl(CTXSpendConstants.BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(CTXSpendTokenApi::class.java)
}

private fun getOkHttpClient(authenticator: Authenticator? = null): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(HeadersInterceptor(config))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package org.dash.wallet.features.exploredash.network.interceptor
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import org.dash.wallet.features.exploredash.utils.DashDirectConfig
import org.dash.wallet.features.exploredash.utils.CTXSpendConfig
import org.dash.wallet.features.exploredash.utils.DashDirectConstants
import javax.inject.Inject

class HeadersInterceptor @Inject constructor(private val config: DashDirectConfig) : Interceptor {
class HeadersInterceptor @Inject constructor(private val config: CTXSpendConfig) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val original = chain.request()
Expand All @@ -32,7 +32,7 @@ class HeadersInterceptor @Inject constructor(private val config: DashDirectConfi
requestBuilder.header(DashDirectConstants.CLIENT_ID_PARAM_NAME, DashDirectConstants.CLIENT_ID)

val accessToken = runBlocking {
config.getSecuredData(DashDirectConfig.PREFS_KEY_ACCESS_TOKEN)
config.getSecuredData(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN)
}

if (accessToken?.isNotEmpty() == true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2021 Dash Core Group.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dash.wallet.features.exploredash.repository.remote

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.Authenticator
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.dash.wallet.common.data.ResponseResource
import org.dash.wallet.common.data.safeApiCall
import org.dash.wallet.features.exploredash.data.ctxspend.model.RefreshTokenResponse
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendTokenApi
import org.dash.wallet.features.exploredash.utils.CTXSpendConfig
import javax.inject.Inject

class TokenAuthenticator @Inject constructor(
private val tokenApi: CTXSpendTokenApi,
private val config: CTXSpendConfig
) : Authenticator {

// For multiple call to refresh token sync
private val tokenMutex = Mutex()

override fun authenticate(route: Route?, response: Response): Request? {
return runBlocking {
tokenMutex.withLock {
when (val tokenResponse = getUpdatedToken()) {
is ResponseResource.Success -> {
tokenResponse.value?.let {
config.set(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN, it.accessToken?: "")
config.set(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN, it.refreshToken?: "")
response.request.newBuilder()
.header("Authorization", "Bearer ${it.accessToken}")
.build()
}
}

else -> {
config.set(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN, "")
config.set(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN, "")
null
}
}
}
}
}

private suspend fun getUpdatedToken(): ResponseResource<RefreshTokenResponse?> {
val accessToken = config.get(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN) ?: ""
val refreshToken = config.get(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN) ?: ""
return safeApiCall { tokenApi.refreshToken(refreshToken = refreshToken) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:layout_height="match_parent"
android:orientation="vertical"
tools:background="@color/background_primary"
tools:context="org.dash.wallet.features.exploredash.ui.dashdirect.dialogs.GiftCardDetailsDialog">
tools:context="org.dash.wallet.features.exploredash.ui.ctxspend.dialogs.GiftCardDetailsDialog">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/header"
Expand Down

0 comments on commit 7da3ecd

Please sign in to comment.