Skip to content

Instantly share code, notes, and snippets.

@arulwastaken
Created July 7, 2025 06:38
Show Gist options
  • Save arulwastaken/c6e27d1a46105992c6031cf2ae015534 to your computer and use it in GitHub Desktop.
Save arulwastaken/c6e27d1a46105992c6031cf2ae015534 to your computer and use it in GitHub Desktop.
package com.codingwitharul.bookmyslot.data.repo
import com.codingwitharul.bookmyslot.data.db.DatabaseHelper
import com.codingwitharul.bookmyslot.data.networking.ApiClientHelper
import com.codingwitharul.bookmyslot.data.networking.EndPoints
import com.codingwitharul.bookmyslot.data.networking.apiEndPoint
import com.codingwitharul.bookmyslot.data.networking.models.ApiResponse
import com.codingwitharul.bookmyslot.data.networking.models.ErrorType
import com.codingwitharul.bookmyslot.data.networking.models.getErrorData
import com.codingwitharul.bookmyslot.data.networking.safeRequest
import com.codingwitharul.bookmyslot.data.toUserInfo
import com.codingwitharul.bookmyslot.db.UserInfo
import com.codingwitharul.bookmyslot.domain.repo.AuthRepo
import com.codingwitharul.bookmyslot.presentation.components.GoogleUser
import com.codingwitharul.bookmyslot.utils.toThrowable
import io.ktor.client.request.setBody
import io.ktor.http.HttpMethod
import kotlinx.datetime.Clock
class AuthRepoImpl(val db: DatabaseHelper, val apiClientHelper: ApiClientHelper) : AuthRepo {
override suspend fun loginWithOAuthOnServer(user: GoogleUser): Result<UserInfo> {
val resp = apiClientHelper.client.safeRequest<GoogleUser, GoogleUser> {
apiEndPoint(EndPoints.Auth)
method = HttpMethod.Post
setBody(user)
}
return when (resp) {
is ApiResponse.Error -> {
Result.failure("Error: ${resp.getErrorData(ErrorType.LOGIN)}".toThrowable())
}
is ApiResponse.Success<GoogleUser> -> {
val userInfo = resp.body.toUserInfo()
db.saveUserInfo(userInfo)
Result.success(userInfo)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment