Skip to main content

Network

sinew-network / sinew_network: an HTTP client that handles what every app rewrites.

What it holds​

  • SinewHttp: a client (Ktor on Kotlin, Dio on Flutter) with timeouts by method, JSON and language headers, auth, error bodies and logging, from one SinewConfig. Use the preset, or add only the pieces you need to your own client.
  • The auth layer: the token goes only to baseUrl's host, outside the anonymous paths, with a single refresh for any number of concurrent 401s.
  • processApiCall and processOptionalApiCall: map an envelope to a Result, and every status to a typed exception.
  • retryWithBackoff and poll, NetworkChecker, and certificate pinning.

Create the client​

// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Network.kt#client
fun ordersClient(tokenSource: TokenSource): HttpClient {
val config = SinewConfig(
baseUrl = "https://api.example.com/v1/",
locale = { "en" },
anonymousPaths = setOf("/auth/login"),
)
return SinewHttp.client(config, tokenSource)
}

locale and the default headers are read on every request, so a language switch or a new device id applies to the next request without rebuilding the client.

Call an API​

// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Network.kt#api-call
// Paths are relative to baseUrl, and every failure comes back as a typed SinewException.
suspend fun cancel(client: HttpClient, id: String): Result<Order> =
processApiCall("ORD", "CANCEL") { client.post("orders/$id/cancel").body<OrderEnvelope>() }

How a status becomes an exception​

ResponseBecomes
a request, connect or socket timeoutRequestTimeOutException
no connection, DNS failure, connection refused or resetNoInternetConnectionException
400 or 422 with field errorsValidationException
401, 403, 404, 409UnauthorizedException, ForbiddenException, NotFoundException, ConflictException
429RateLimitedException, with the Retry-After wait
503, 504 / any other 5xxServiceUnavailableException / ServerException
any other status, with a readable message / withoutApiErrorException / UndefinedErrorResponseException
JSON that doesn't fit the model, or an envelope with no payloadParseException, reported

A readable error-body message becomes the exception's server text; otherwise its type's local message is used. A TLS or certificate failure matches nothing and becomes a reported UnexpectedException: a security event, never "offline".

Good to know​

  • API classes use paths relative to baseUrl (get("orders")): a leading / would replace its own path.
  • On sign-out, reset the auth layer and clear the token source, so nothing carries into the next session.
  • On Flutter with Riverpod, sinew_riverpod_providers provides the configured client, the token source and the stores, so you override only what differs.