Skip to content

Commit

Permalink
generified retrofit request maker
Browse files Browse the repository at this point in the history
  • Loading branch information
iankang committed Mar 12, 2022
1 parent 570030e commit 0a9559d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.coinloreapi.api

import android.util.Log
import com.example.coinloreapi.models.CoinLoreResponse
import kotlinx.coroutines.*
import retrofit2.Response
import java.io.IOException


private val TAG: String = "RequestMaker"
suspend fun <T> coinLoreApiCall(
dispatcher: CoroutineDispatcher = Dispatchers.IO,
apiCall: suspend () -> Response<T>
): CoinLoreResponse<T> {
return withContext(dispatcher) {
try {
val responseDeffered: Deferred<Response<T>> = async { apiCall.invoke() }
val response = responseDeffered.await()
Log.d(TAG, "response: ${response.raw()}")
if (response.isSuccessful) {
Log.d(TAG, "success")
CoinLoreResponse(
data = response.body(),
message = "successful",
isOk = true,
httpStatus = response.code()
)
} else {
Log.e(TAG, "error: ${response}")
CoinLoreResponse(
data = response.body(),
message = response.errorBody().toString(),
isOk = false,
httpStatus = response.code()
)
}

} catch (error: IOException) {

CoinLoreResponse(message = error.localizedMessage)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.coinloreapi.models

data class CoinLoreResponse<T>(
val data: T? = null,
val message: String? = "Error",
val isOk:Boolean? = false,
val httpStatus:Int? = 500
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,54 @@ package com.example.coinloreapi.repository

import android.util.Log
import com.example.coinloreapi.api.CoinLoreAPI

import com.example.coinloreapi.api.coinLoreApiCall
import com.example.coinloreapi.models.CoinLoreResponse
import com.example.coinloreapi.models.GlobalCoinResponse
import com.example.coinloreapi.utils.NetworkState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.koin.java.KoinJavaComponent.inject

import retrofit2.Response
import java.io.IOException

class GlobalCoinRepository {

private val coinLoreAPI: CoinLoreAPI by inject(CoinLoreAPI::class.java)
private val TAG = GlobalCoinRepository::class.java.name

suspend fun getGlobalCoin(): NetworkState<GlobalCoinResponse> = withContext(Dispatchers.IO) {
try {
val responseDeffered = async { coinLoreAPI.getGlobalCoinData() }
val response = responseDeffered.await()
Log.d(TAG, "response: ${response.raw()}")
if (response.isSuccessful) {
Log.d(TAG, "success")
NetworkState.Success(response.body()!!)
} else {
Log.e(TAG, "error: ${response}")
when (response.code()) {
403 -> NetworkState.HttpErrors.ResourceForbidden(response.message())
404 -> NetworkState.HttpErrors.ResourceNotFound(response.message())
500 -> NetworkState.HttpErrors.InternalServerError(response.message())
502 -> NetworkState.HttpErrors.BadGateway(response.message())
301 -> NetworkState.HttpErrors.ResourceRemoved(response.message())
302 -> NetworkState.HttpErrors.RemovedResourceFound(response.message())
else -> NetworkState.Error(response.raw().toString())
suspend fun getGlobalCoin(): CoinLoreResponse<GlobalCoinResponse> =
withContext(Dispatchers.IO) {
try {
val responseDeffered = async { coinLoreAPI.getGlobalCoinData() }
val response: Response<GlobalCoinResponse> = responseDeffered.await()
Log.d(TAG, "response: ${response.raw()}")
if (response.isSuccessful) {
Log.d(TAG, "success")
CoinLoreResponse(
data = response.body(),
message = "successful",
isOk = true,
httpStatus = response.code()
)
} else {
Log.e(TAG, "error: ${response}")
CoinLoreResponse(
data = response.body(),
message = response.errorBody().toString(),
isOk = false,
httpStatus = response.code()
)
}

} catch (error: IOException) {

CoinLoreResponse(message = error.localizedMessage)
}

} catch (error: IOException) {
NetworkState.NetworkException(error.localizedMessage ?: "error")
}

suspend fun getGlobalCoinResponse():CoinLoreResponse<GlobalCoinResponse>{
return coinLoreApiCall(apiCall = {coinLoreAPI.getGlobalCoinData()})
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.example.coinloreapi.models.CoinLoreResponse
import com.example.coinloreapi.models.GlobalCoinResponse
import com.example.coinloreapi.repository.GlobalCoinRepository
import com.example.cryptknow.utils.stateManagement.NetworkUIState
import com.example.cryptknow.utils.stateManagement.NetworkingState.networkUiState

@Composable
fun HomeItems(coinRepository: GlobalCoinRepository){
Expand All @@ -22,17 +21,17 @@ fun HomeItems(coinRepository: GlobalCoinRepository){
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val coinState by produceState(initialValue = NetworkUIState<GlobalCoinResponse>(isSuccessful = false, loading = true) ){
val response = coinRepository.getGlobalCoin()
value = networkUiState(response)
val coinState by produceState(initialValue = CoinLoreResponse<GlobalCoinResponse>() ){
val response = coinRepository.getGlobalCoinResponse()
value = response
}
if(coinState.loading){
if(coinState.isOk == false){
Text(text = "loading")
}

if(coinState.isSuccessful){
if(coinState.isOk == true){
Text(
coinState.response?.toString()!!
coinState.data.toString()
)
}

Expand Down

0 comments on commit 0a9559d

Please sign in to comment.