Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to define coroutineContext for coroutine functions #387

Merged
merged 3 commits into from Jul 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,3 @@

import com.github.kittinunf.fuel.core.Deserializable
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.Request
Expand All @@ -8,91 +7,115 @@ import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.github.kittinunf.fuel.core.response
import com.github.kittinunf.result.Result
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
import kotlinx.coroutines.experimental.withContext
import java.nio.charset.Charset
import kotlin.coroutines.experimental.CoroutineContext

private suspend fun <T : Any, U : Deserializable<T>> Request.await(
deserializable: U
deserializable: U, scope: CoroutineContext
): Triple<Request, Response, Result<T, FuelError>> =
suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation { cancel() }
continuation.resume(response(deserializable))
withContext(scope) {
suspendCancellableCoroutine<Triple<Request, Response, Result<T, FuelError>>> { continuation ->
continuation.invokeOnCancellation { cancel() }
continuation.resume(response(deserializable))
}
}


suspend fun Request.awaitByteArrayResponse(): Triple<Request, Response, Result<ByteArray, FuelError>> =
await(byteArrayDeserializer())
suspend fun Request.awaitByteArrayResponse(
scope: CoroutineContext = CommonPool
): Triple<Request, Response, Result<ByteArray, FuelError>> =
await(byteArrayDeserializer(), scope)

suspend fun Request.awaitStringResponse(
charset: Charset = Charsets.UTF_8
): Triple<Request, Response, Result<String, FuelError>> = await(stringDeserializer(charset))
charset: Charset = Charsets.UTF_8,
scope: CoroutineContext = CommonPool
): Triple<Request, Response, Result<String, FuelError>> = await(stringDeserializer(charset), scope)

suspend fun <U : Any> Request.awaitObjectResponse(
deserializable: ResponseDeserializable<U>
): Triple<Request, Response, Result<U, FuelError>> = await(deserializable)
deserializable: ResponseDeserializable<U>,
scope: CoroutineContext = CommonPool
): Triple<Request, Response, Result<U, FuelError>> = await(deserializable, scope)

/***
*
* Response functions all these return a Type
*
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return ByteArray if no exceptions are thrown
*/
@Throws
suspend fun Request.awaitByteArray(): ByteArray = await(byteArrayDeserializer()).third.get()
suspend fun Request.awaitByteArray(
scope: CoroutineContext = CommonPool
): ByteArray = await(byteArrayDeserializer(), scope).third.get()

/**
* @note errors thrown in deserialization will not be caught
*
* @param charset this is defaults to UTF-8
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return ByteArray if no exceptions are thrown
*/
@Throws
suspend fun Request.awaitString(
charset: Charset = Charsets.UTF_8
): String = await(stringDeserializer(charset)).third.get()
charset: Charset = Charsets.UTF_8,
scope: CoroutineContext = CommonPool
): String = await(stringDeserializer(charset), scope).third.get()

/**
* @note This function will throw the an exception if an error is thrown either at the HTTP level
* or during deserialization
*
* @param deserializable
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return Result object
*/
@Throws
suspend fun <U : Any> Request.awaitObject(
deserializable: ResponseDeserializable<U>
): U = await(deserializable).third.get()
deserializable: ResponseDeserializable<U>,
scope: CoroutineContext = CommonPool
): U = await(deserializable, scope).third.get()

/***
*
* Response functions all these return a Result
*
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return Result<ByteArray,FuelError>
*/
suspend fun Request.awaitByteArrayResult(): Result<ByteArray, FuelError> = awaitByteArrayResponse().third
suspend fun Request.awaitByteArrayResult(
scope: CoroutineContext = CommonPool
): Result<ByteArray, FuelError> = awaitByteArrayResponse(scope).third

/**
*
* @param charset this is defaults to UTF-8
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return Result<String,FuelError>
*/
suspend fun Request.awaitStringResult(
charset: Charset = Charsets.UTF_8
): Result<String, FuelError> = awaitStringResponse(charset).third
charset: Charset = Charsets.UTF_8,
scope: CoroutineContext = CommonPool
): Result<String, FuelError> = awaitStringResponse(charset, scope).third


/**
* This function catches both server errors and Deserialization Errors
*
* @param deserializable
* @param scope : This is the coroutine context you want the call to be made on, the defaut is CommonPool
*
* @return Result object
*/
suspend fun <U : Any> Request.awaitObjectResult(
deserializable: ResponseDeserializable<U>
deserializable: ResponseDeserializable<U>,
scope: CoroutineContext = CommonPool
): Result<U, FuelError> = try {
await(deserializable).third
await(deserializable, scope).third
} catch (e: Exception) {
val fuelError = when (e) {
is FuelError -> e
Expand Down