From 70bfffc854d929b3e2f3cf6c95e73164569d7f1a Mon Sep 17 00:00:00 2001 From: Liviu Timar <65943217+liviu-timar@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:36:50 +0200 Subject: [PATCH] Refactor services and service responses --- .../repositories/GoogleAccountRepository.kt | 6 ++-- .../google/ListDirectoryPeopleResponse.kt} | 4 +-- .../services/stream/GetAuthDataResponse.kt} | 4 +-- .../services/stream/StreamService.kt} | 29 ++++++++++--------- .../video/android/ui/login/LoginViewModel.kt | 18 ++++++------ .../android/util/StreamVideoInitHelper.kt | 24 +++++++-------- 6 files changed, 43 insertions(+), 42 deletions(-) rename demo-app/src/main/kotlin/io/getstream/video/android/data/{dto/GetGoogleAccountsDto.kt => services/google/ListDirectoryPeopleResponse.kt} (93%) rename demo-app/src/main/kotlin/io/getstream/video/android/{token/TokenResponse.kt => data/services/stream/GetAuthDataResponse.kt} (80%) rename demo-app/src/main/kotlin/io/getstream/video/android/{token/StreamTokenService.kt => data/services/stream/StreamService.kt} (64%) diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/data/repositories/GoogleAccountRepository.kt b/demo-app/src/main/kotlin/io/getstream/video/android/data/repositories/GoogleAccountRepository.kt index 4d86f86bc3..00096df69a 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/data/repositories/GoogleAccountRepository.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/data/repositories/GoogleAccountRepository.kt @@ -26,8 +26,8 @@ import com.squareup.moshi.Moshi import com.squareup.moshi.adapter import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import dagger.hilt.android.qualifiers.ApplicationContext -import io.getstream.video.android.data.dto.GetGoogleAccountsResponseDto -import io.getstream.video.android.data.dto.asDomainModel +import io.getstream.video.android.data.services.google.ListDirectoryPeopleResponse +import io.getstream.video.android.data.services.google.asDomainModel import io.getstream.video.android.models.GoogleAccount import io.getstream.video.android.util.GoogleSignInHelper import kotlinx.coroutines.Dispatchers @@ -118,7 +118,7 @@ class GoogleAccountRepository @Inject constructor( val moshi: Moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() - val jsonAdapter: JsonAdapter = moshi.adapter() + val jsonAdapter: JsonAdapter = moshi.adapter() val response = jsonAdapter.fromJson(jsonString) return response?.people?.map { it.asDomainModel() } diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/data/dto/GetGoogleAccountsDto.kt b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/google/ListDirectoryPeopleResponse.kt similarity index 93% rename from demo-app/src/main/kotlin/io/getstream/video/android/data/dto/GetGoogleAccountsDto.kt rename to demo-app/src/main/kotlin/io/getstream/video/android/data/services/google/ListDirectoryPeopleResponse.kt index d0905c4064..f7584582dc 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/data/dto/GetGoogleAccountsDto.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/google/ListDirectoryPeopleResponse.kt @@ -14,12 +14,12 @@ * limitations under the License. */ -package io.getstream.video.android.data.dto +package io.getstream.video.android.data.services.google import io.getstream.video.android.models.GoogleAccount import io.getstream.video.android.util.UserHelper -data class GetGoogleAccountsResponseDto( +data class ListDirectoryPeopleResponse( val people: List, ) diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/token/TokenResponse.kt b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/GetAuthDataResponse.kt similarity index 80% rename from demo-app/src/main/kotlin/io/getstream/video/android/token/TokenResponse.kt rename to demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/GetAuthDataResponse.kt index b3070e60f3..385590d20a 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/token/TokenResponse.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/GetAuthDataResponse.kt @@ -14,8 +14,8 @@ * limitations under the License. */ -package io.getstream.video.android.token +package io.getstream.video.android.data.services.stream import kotlinx.serialization.Serializable -@Serializable data class TokenResponse(val userId: String, val apiKey: String, val token: String) +@Serializable data class GetAuthDataResponse(val userId: String, val apiKey: String, val token: String) diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/token/StreamTokenService.kt b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/StreamService.kt similarity index 64% rename from demo-app/src/main/kotlin/io/getstream/video/android/token/StreamTokenService.kt rename to demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/StreamService.kt index 3043028903..7854d9a732 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/token/StreamTokenService.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/data/services/stream/StreamService.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.getstream.video.android.token +package io.getstream.video.android.data.services.stream import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory import kotlinx.serialization.json.Json @@ -24,21 +24,22 @@ import retrofit2.create import retrofit2.http.GET import retrofit2.http.Query -interface StreamTokenService { +interface StreamService { @GET("api/auth/create-token") - suspend fun fetchToken( + suspend fun getAuthData( @Query("environment") environment: String, - @Query("user_id") userId: String? - ): TokenResponse -} + @Query("user_id") userId: String?, + ): GetAuthDataResponse + + companion object { + private const val BASE_URL = "https://pronto.getstream.io/" -object StreamVideoNetwork { - private val contentType = "application/json".toMediaType() - private val retrofit = - Retrofit.Builder() - .baseUrl("https://pronto.getstream.io/") - .addConverterFactory(Json.asConverterFactory(contentType)) - .build() + private val retrofit = + Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) + .build() - val tokenService = retrofit.create() + val instance = retrofit.create() + } } diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/ui/login/LoginViewModel.kt b/demo-app/src/main/kotlin/io/getstream/video/android/ui/login/LoginViewModel.kt index fe40b1ae18..a4eb4aaf5a 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/ui/login/LoginViewModel.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/ui/login/LoginViewModel.kt @@ -24,10 +24,10 @@ import io.getstream.video.android.BuildConfig import io.getstream.video.android.STREAM_SDK_ENVIRONMENT import io.getstream.video.android.core.StreamVideo import io.getstream.video.android.data.repositories.GoogleAccountRepository +import io.getstream.video.android.data.services.stream.GetAuthDataResponse +import io.getstream.video.android.data.services.stream.StreamService import io.getstream.video.android.datastore.delegate.StreamUserDataStore import io.getstream.video.android.model.User -import io.getstream.video.android.token.StreamVideoNetwork -import io.getstream.video.android.token.TokenResponse import io.getstream.video.android.util.StreamVideoInitHelper import io.getstream.video.android.util.UserHelper import kotlinx.coroutines.Dispatchers @@ -76,7 +76,7 @@ class LoginViewModel @Inject constructor( emit(LoginUiState.AlreadyLoggedIn) } else { try { - val tokenResponse = StreamVideoNetwork.tokenService.fetchToken( + val authData = StreamService.instance.getAuthData( environment = STREAM_SDK_ENVIRONMENT, userId = userId, ) @@ -84,23 +84,23 @@ class LoginViewModel @Inject constructor( val loggedInGoogleUser = if (autoLogIn) null else googleAccountRepository.getCurrentUser() val user = User( - id = tokenResponse.userId, + id = authData.userId, // if autoLogIn is true it means we have a random user name = if (autoLogIn) userId else loggedInGoogleUser?.name ?: "", image = if (autoLogIn) "" else loggedInGoogleUser?.photoUrl ?: "", role = "admin", - custom = mapOf("email" to tokenResponse.userId), + custom = mapOf("email" to authData.userId), ) // Store the data in the demo app - dataStore.updateApiKey(tokenResponse.apiKey) + dataStore.updateApiKey(authData.apiKey) dataStore.updateUser(user) - dataStore.updateUserToken(tokenResponse.token) + dataStore.updateUserToken(authData.token) // Init the Video SDK with the data StreamVideoInitHelper.loadSdk(dataStore) - emit(LoginUiState.SignInComplete(tokenResponse)) + emit(LoginUiState.SignInComplete(authData)) } catch (exception: Throwable) { val message = "Sign in failed: ${exception.message ?: "Generic error"}" emit(LoginUiState.SignInFailure(message)) @@ -143,7 +143,7 @@ sealed interface LoginUiState { object AlreadyLoggedIn : LoginUiState - data class SignInComplete(val tokenResponse: TokenResponse) : LoginUiState + data class SignInComplete(val authDataResponse: GetAuthDataResponse) : LoginUiState data class SignInFailure(val errorMessage: String) : LoginUiState } diff --git a/demo-app/src/main/kotlin/io/getstream/video/android/util/StreamVideoInitHelper.kt b/demo-app/src/main/kotlin/io/getstream/video/android/util/StreamVideoInitHelper.kt index 14dbfbcd50..12fbf57e11 100644 --- a/demo-app/src/main/kotlin/io/getstream/video/android/util/StreamVideoInitHelper.kt +++ b/demo-app/src/main/kotlin/io/getstream/video/android/util/StreamVideoInitHelper.kt @@ -32,10 +32,10 @@ import io.getstream.video.android.core.StreamVideo import io.getstream.video.android.core.StreamVideoBuilder import io.getstream.video.android.core.logging.LoggingLevel import io.getstream.video.android.core.notifications.NotificationConfig +import io.getstream.video.android.data.services.stream.StreamService import io.getstream.video.android.datastore.delegate.StreamUserDataStore import io.getstream.video.android.model.ApiKey import io.getstream.video.android.model.User -import io.getstream.video.android.token.StreamVideoNetwork import kotlinx.coroutines.flow.firstOrNull @SuppressLint("StaticFieldLeak") @@ -77,21 +77,21 @@ object StreamVideoInitHelper { if (loggedInUser == null && useRandomUserAsFallback) { val userId = UserHelper.generateRandomString() - val result = StreamVideoNetwork.tokenService.fetchToken( + val authData = StreamService.instance.getAuthData( environment = STREAM_SDK_ENVIRONMENT, - userId = userId + userId = userId, ) - val user = User(id = result.userId, role = "admin") + val user = User(id = authData.userId, role = "admin") // Store the data (note that this datastore belongs to the client - it's not // used by the SDK directly in any way) - dataStore.updateApiKey(result.apiKey) + dataStore.updateApiKey(authData.apiKey) dataStore.updateUser(user) - dataStore.updateUserToken(result.token) + dataStore.updateUserToken(authData.token) - apiKey = result.apiKey + apiKey = authData.apiKey loggedInUser = user - userToken = result.token + userToken = authData.token } if (loggedInUser != null) { @@ -141,12 +141,12 @@ object StreamVideoInitHelper { ), tokenProvider = { val email = user.custom["email"] - val response = StreamVideoNetwork.tokenService.fetchToken( + val authData = StreamService.instance.getAuthData( environment = STREAM_SDK_ENVIRONMENT, - userId = email + userId = email, ) - dataStore.updateUserToken(response.token) - response.token + dataStore.updateUserToken(authData.token) + authData.token }, ).build() }