Files
Driver/app/src/androidTest/java/h_mal/appttude/com/WebUtils.kt
hmalik144 4b0db6cd19 - Firebase emulator added
- Update to Espresso tests
 - Updated gradle dependencies for espresso

Took 9 hours 56 minutes
2023-03-13 22:36:53 +00:00

62 lines
2.0 KiB
Kotlin

package h_mal.appttude.com
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
class WebUtils {
private val okHttpClient by lazy { OkHttpClient() }
suspend fun <T : Any> post(url: String, body: String): T? {
val requestBody = body.toRequestBody()
val request = Request.Builder()
.post(requestBody)
.url(url)
.build()
return okHttpClient.newCall(request).await()
}
suspend fun get(url: String): String? {
val request: Request = Request.Builder()
.url(url)
.build()
return okHttpClient.newCall(request).await()
}
private suspend fun <T> Call.await(): T? {
val objectMapper = Gson()
val typeToken: TypeToken<T> = object : TypeToken<T>() {}
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
continuation.resume(
objectMapper.fromJson(
response.body?.string(),
typeToken.type
)
)
}
override fun onFailure(call: Call, e: IOException) {
// Don't bother with resuming the continuation if it is already cancelled.
if (continuation.isCancelled) return
continuation.resumeWithException(e)
}
})
continuation.invokeOnCancellation {
try {
cancel()
} catch (ex: Throwable) {
//Ignore cancel exception
}
}
}
}
}