Loading modules/src/androidTest/java/app/lounge/NetworkFetchingAPITest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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()) Loading @@ -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()) Loading @@ -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()) Loading modules/src/main/java/app/lounge/networking/NetworkFetchingRetrofitImpl.kt→modules/src/main/java/app/lounge/networking/LoginDataSource.kt +19 −14 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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> Loading @@ -50,7 +51,7 @@ interface NetworkFetchingRetrofitAPI { } @Singleton class NetworkFetchingRetrofitImpl @Inject constructor( class LoginDataSource @Inject constructor( retrofit: Retrofit ) : NetworkFetching { Loading @@ -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( Loading @@ -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 modules/src/main/java/app/lounge/networking/NetworkFetching.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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> } modules/src/main/java/app/lounge/networking/RetrofitHandler.kt 0 → 100644 +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) } } Loading
modules/src/androidTest/java/app/lounge/NetworkFetchingAPITest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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()) Loading @@ -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()) Loading @@ -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()) Loading
modules/src/main/java/app/lounge/networking/NetworkFetchingRetrofitImpl.kt→modules/src/main/java/app/lounge/networking/LoginDataSource.kt +19 −14 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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> Loading @@ -50,7 +51,7 @@ interface NetworkFetchingRetrofitAPI { } @Singleton class NetworkFetchingRetrofitImpl @Inject constructor( class LoginDataSource @Inject constructor( retrofit: Retrofit ) : NetworkFetching { Loading @@ -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( Loading @@ -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
modules/src/main/java/app/lounge/networking/NetworkFetching.kt +2 −2 Original line number Diff line number Diff line Loading @@ -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> }
modules/src/main/java/app/lounge/networking/RetrofitHandler.kt 0 → 100644 +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) } }