Skip to content

Commit

Permalink
Refactor services and service responses
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar committed Nov 15, 2023
1 parent d0b5352 commit 70bfffc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,7 +118,7 @@ class GoogleAccountRepository @Inject constructor(
val moshi: Moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val jsonAdapter: JsonAdapter<GetGoogleAccountsResponseDto> = moshi.adapter()
val jsonAdapter: JsonAdapter<ListDirectoryPeopleResponse> = moshi.adapter()

val response = jsonAdapter.fromJson(jsonString)
return response?.people?.map { it.asDomainModel() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoogleAccountDto>,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<StreamTokenService>()
val instance = retrofit.create<StreamService>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,31 +76,31 @@ 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,
)

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))
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 70bfffc

Please sign in to comment.