Skip to content

Commit

Permalink
#122 Add TraktCode RemoteSource
Browse files Browse the repository at this point in the history
  • Loading branch information
hadi-norouzi committed Sep 13, 2024
1 parent 1536615 commit 24b108a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package io.filmtime.data.api.trakt

import io.filmtime.data.model.GeneralError
import io.filmtime.data.model.Result
import io.filmtime.data.model.TraktCodeLogin
import io.filmtime.data.model.TraktTokens

interface TraktAuthRemoteSource {

suspend fun getAccessToken(code: String): Result<TraktTokens, GeneralError>

suspend fun getLoginCode(): Result<TraktCodeLogin, GeneralError>

suspend fun getTokenByCode()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.filmtime.data.api.trakt

import io.filmtime.data.api.trakt.model.toAccessToken
import io.filmtime.data.api.trakt.model.toDeviceCode
import io.filmtime.data.model.GeneralError
import io.filmtime.data.model.Result
import io.filmtime.data.model.TraktCodeLogin
import io.filmtime.data.model.TraktTokens
import io.filmtime.data.network.BuildConfig
import io.filmtime.data.network.adapter.NetworkResponse
import io.filmtime.data.network.trakt.TraktAuthService
import io.filmtime.data.network.trakt.TraktClientIDRequest
import io.filmtime.data.network.trakt.TraktGetTokenRequest
import javax.inject.Inject

Expand Down Expand Up @@ -38,4 +41,28 @@ class TraktAuthRemoteSourceImpl @Inject constructor(
is NetworkResponse.UnknownError -> Result.Failure(GeneralError.UnknownError(result.error))
}
}

override suspend fun getLoginCode(): Result<TraktCodeLogin, GeneralError> {
return when (
val result = traktAuthService.getDeviceCode(
body = TraktClientIDRequest(BuildConfig.TRAKT_CLIENT_ID),
)
) {
is NetworkResponse.ApiError -> Result.Failure(GeneralError.ApiError(result.body.error, result.code))
is NetworkResponse.NetworkError -> Result.Failure(GeneralError.NetworkError)
is NetworkResponse.Success -> {
val response = result.body
if (response == null) {
Result.Failure(GeneralError.UnknownError(Throwable("Code response is null")))
} else {
Result.Success(response.toDeviceCode())
}
}
is NetworkResponse.UnknownError -> Result.Failure(GeneralError.UnknownError(result.error))
}
}

override suspend fun getTokenByCode() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.filmtime.data.api.trakt.model

import io.filmtime.data.model.TraktCodeLogin
import io.filmtime.data.network.trakt.TraktDeviceCodeResponse

fun TraktDeviceCodeResponse.toDeviceCode() =
TraktCodeLogin(
deviceCode = deviceCode,
userCode = userCode,
verificationURL = verificationURL,
expiresIn = expiresIn,
interval = interval,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.filmtime.data.model

data class TraktCodeLogin(
val deviceCode: String,
val userCode: String,
val verificationURL: String,
val expiresIn: Long,
val interval: Long,
)

0 comments on commit 24b108a

Please sign in to comment.