Skip to content

Commit

Permalink
Merge pull request #4 from egeniq-forks/feature/exceptions
Browse files Browse the repository at this point in the history
Catch exceptions as errors on iOS
  • Loading branch information
johankool committed Sep 30, 2020
2 parents 994d408 + e27bbb8 commit 8ec0131
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Expand Up @@ -14,6 +14,7 @@ import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor

{{#operations}}
@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}} constructor(
baseUrl: kotlin.String = "{{{basePath}}}",
httpClientEngine: HttpClientEngine? = null,
Expand All @@ -39,6 +40,7 @@ import kotlinx.serialization.internal.StringDescriptor
{{#returnType}}
@Suppress("UNCHECKED_CAST")
{{/returnType}}
@Throws(Exception::class)
suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): HttpResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> {
val localVariableAuthNames = listOf<String>({{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}})
Expand Down
Expand Up @@ -5,11 +5,16 @@ import io.ktor.client.call.typeInfo
import io.ktor.http.Headers
import io.ktor.http.isSuccess

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}open class HttpResponse<T : Any>(val response: io.ktor.client.statement.HttpResponse, val provider: BodyProvider<T>) {
val status: Int = response.status.value
val success: Boolean = response.status.isSuccess()
val headers: Map<String, List<String>> = response.headers.mapEntries()
@Throws(Exception::class)
suspend fun body(): T = provider.body(response)
@Throws(Exception::class)
suspend fun <V : Any> typedBody(type: TypeInfo): V = provider.typedBody(response, type)
{{#nonPublicApi}}internal {{/nonPublicApi}}companion object {
Expand All @@ -21,31 +26,43 @@ import io.ktor.http.isSuccess
}
}

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}interface BodyProvider<T : Any> {
@Throws(Exception::class)
suspend fun body(response: io.ktor.client.statement.HttpResponse): T
@Throws(Exception::class)
suspend fun <V : Any> typedBody(response: io.ktor.client.statement.HttpResponse, type: TypeInfo): V
}

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}class TypedBodyProvider<T : Any>(private val type: TypeInfo) : BodyProvider<T> {
@Suppress("UNCHECKED_CAST")
@Throws(Exception::class)
override suspend fun body(response: io.ktor.client.statement.HttpResponse): T =
response.call.receive(type) as T
@Suppress("UNCHECKED_CAST")
@Throws(Exception::class)
override suspend fun <V : Any> typedBody(response: io.ktor.client.statement.HttpResponse, type: TypeInfo): V =
response.call.receive(type) as V
}

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}class MappedBodyProvider<S : Any, T : Any>(private val provider: BodyProvider<S>, private val block: S.() -> T) : BodyProvider<T> {
@Throws(Exception::class)
override suspend fun body(response: io.ktor.client.statement.HttpResponse): T =
block(provider.body(response))
@Throws(Exception::class)
override suspend fun <V : Any> typedBody(response: io.ktor.client.statement.HttpResponse, type: TypeInfo): V =
provider.typedBody(response, type)
}

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}inline fun <reified T : Any> io.ktor.client.statement.HttpResponse.wrap(): HttpResponse<T> =
HttpResponse(this, TypedBodyProvider(typeInfo<T>()))

@ExperimentalStdlibApi
{{#nonPublicApi}}internal {{/nonPublicApi}}fun <T : Any, V : Any> HttpResponse<T>.map(block: T.() -> V): HttpResponse<V> =
HttpResponse(response, MappedBodyProvider(provider, block))

0 comments on commit 8ec0131

Please sign in to comment.