TypedRestClient

open class TypedRestClient<E : ErrorSpec>(client: HttpClient = HttpClientFactory.create(), baseUrl: String = "", mapper: RestErrorMapper<E, *>)(source)

A REST client whose every call works in Raise of the caller's error type E.

A request that cannot be sent raises RestErrorMapper.networkError, a non-2xx status raises RestErrorMapper.errorOf, and a success body that does not decode raises RestErrorMapper.parseError. Success and error bodies both decode through client's content negotiation, so the codec lives in the HttpClient (see HttpClientFactory.create).

request is the general entry point: any method, any body Ktor can send. The verbs are sugar over it: get, post, put, patch, delete, head and options speak JSON; postMultipart and postForm send the two form encodings. Each takes a builder for headers, query parameters and anything else on the request.

Inheritors

Constructors

Link copied to clipboard
constructor(client: HttpClient = HttpClientFactory.create(), baseUrl: String = "", mapper: RestErrorMapper<E, *>)

Functions

Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> delete(uri: String, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> get(uri: String, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> head(uri: String, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> options(uri: String, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T, reified B> patch(uri: String, body: B? = null, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T, reified B> post(uri: String, body: B, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> postForm(uri: String, parameters: Parameters, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T

POSTs parameters as an application/x-www-form-urlencoded body.

Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T> postMultipart(uri: String, crossinline parts: FormBuilder.() -> Unit, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T

POSTs a multipart/form-data body assembled from parts (see FormBuilder.append).

Link copied to clipboard
context(_: Raise<E>)
inline suspend fun <reified T, reified B> put(uri: String, body: B? = null, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T
Link copied to clipboard
context(r: Raise<E>)
inline suspend fun <reified T> request(method: HttpMethod, uri: String, crossinline builder: HttpRequestBuilder.() -> Unit = {}): T

Sends method to uri with whatever builder sets, and decodes the body as T, raising the mapped E at each failure point.