Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6e9f56bb authored by Hasib Prince's avatar Hasib Prince
Browse files

refactored

parent dc40dc7f
Loading
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import app.lounge.model.AuthDataResponse
import app.lounge.model.AuthDataValidationResponse
import app.lounge.networking.NetworkFetching
import app.lounge.networking.NetworkFetchingRetrofitAPI
import app.lounge.networking.NetworkFetchingRetrofitImpl
import app.lounge.networking.LoginDataSource
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import org.junit.Test
@@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit

class NetworkFetchingAPITest {

    private val networkFetchingToken: NetworkFetching = NetworkFetchingRetrofitImpl(
    private val networkFetchingToken: NetworkFetching = LoginDataSource(
        Retrofit.Builder()
            .baseUrl(NetworkFetchingRetrofitAPI.tokenBaseURL)
            .addConverterFactory(GsonConverterFactory.create())
@@ -30,7 +30,7 @@ class NetworkFetchingAPITest {
            .build()
    )

    private val networkFetchingGoogle: NetworkFetching = NetworkFetchingRetrofitImpl(
    private val networkFetchingGoogle: NetworkFetching = LoginDataSource(
        Retrofit.Builder()
            .baseUrl(NetworkFetchingRetrofitAPI.googlePlayBaseURL)
            .addConverterFactory(GsonConverterFactory.create())
@@ -42,7 +42,7 @@ class NetworkFetchingAPITest {
            .build()
    )

    private val networkFetchingTimeoutGoogle: NetworkFetching = NetworkFetchingRetrofitImpl(
    private val networkFetchingTimeoutGoogle: NetworkFetching = LoginDataSource(
        Retrofit.Builder()
            .baseUrl(NetworkFetchingRetrofitAPI.googlePlayBaseURL)
            .addConverterFactory(GsonConverterFactory.create())
+19 −14
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import com.aurora.gplayapi.GooglePlayApi
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.HeaderMap
@@ -27,12 +28,12 @@ interface NetworkFetchingRetrofitAPI {
    suspend fun authDataRequest(
        @HeaderMap headers: Map<String, String>,
        @Body requestBody: RequestBody
    ): AuthDataResponse
    ): Response<AuthDataResponse>

    @POST(Path.sync)
    suspend fun validateAuthentication(
        @HeaderMap headers: Map<String, String>
    ): AuthDataValidationResponse
    ): Response<AuthDataValidationResponse>



@@ -50,7 +51,7 @@ interface NetworkFetchingRetrofitAPI {
}

@Singleton
class NetworkFetchingRetrofitImpl @Inject constructor(
class LoginDataSource @Inject constructor(
    retrofit: Retrofit
) : NetworkFetching  {

@@ -60,7 +61,7 @@ class NetworkFetchingRetrofitImpl @Inject constructor(

    override suspend fun requestAuthData(
        anonymousAuthDataRequestBody: AnonymousAuthDataRequestBody
    ): AuthDataResponse {
    ): NetworkResult<AuthDataResponse> {
        val requestBody: RequestBody =
            anonymousAuthDataRequestBody.properties.toByteArray().let { result ->
                result.toRequestBody(
@@ -69,20 +70,24 @@ class NetworkFetchingRetrofitImpl @Inject constructor(
                    byteCount = result.size
                )
            }
        return networkFetchingRetrofitAPI.authDataRequest(
        return getResult {
            networkFetchingRetrofitAPI.authDataRequest(
                requestBody = requestBody,
                headers = NetworkFetchingRetrofitAPI.Header.authData {
                    anonymousAuthDataRequestBody.userAgent
                }
            )
        }
    }

    override suspend fun requestAuthDataValidation(
        anonymousAuthDataValidationRequestBody: AnonymousAuthDataValidationRequestBody
    ): AuthDataValidationResponse {
        return networkFetchingRetrofitAPI.validateAuthentication(
    ): NetworkResult<AuthDataValidationResponse> {
        return getResult {
            networkFetchingRetrofitAPI.validateAuthentication(
                headers = anonymousAuthDataValidationRequestBody.header
            )
        }
    }

}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -8,10 +8,10 @@ import app.lounge.model.AuthDataValidationResponse
interface NetworkFetching {
    suspend  fun requestAuthData(
        anonymousAuthDataRequestBody: AnonymousAuthDataRequestBody
    ) : AuthDataResponse
    ) : NetworkResult<AuthDataResponse>

    suspend fun requestAuthDataValidation(
        anonymousAuthDataValidationRequestBody: AnonymousAuthDataValidationRequestBody
    ) : AuthDataValidationResponse
    ) : NetworkResult<AuthDataValidationResponse>
}
+23 −0
Original line number Diff line number Diff line
package app.lounge.networking

import retrofit2.Response

sealed class NetworkResult<T> {
    class Success<T>(val data: T) : NetworkResult<T>()
    class Error<T>(val code: Int, val message: String?) : NetworkResult<T>()
    class Exception<T>(val e: Throwable) : NetworkResult<T>()
}

suspend fun <T> getResult(call: suspend () -> Response<T>): NetworkResult<T> {
    try {
        val response = call()
        if (response.isSuccessful) {
            val body = response.body()
            if (body != null) return NetworkResult.Success(body)
        }

        return NetworkResult.Error(response.code(), " ${response.code()} ${response.message()}")
    } catch (e: Exception) {
        return NetworkResult.Exception(e)
    }
}