NetworkClient

class NetworkClient(configuration: NetworkClientConfiguration = NetworkClientConfiguration()) : NetworkDataSource

Coroutine-based HTTP client built on Ktor + OkHttp. The Kotlin counterpart of SwiftyNetwork's NetworkClient.

Responsibilities, per request:

  1. Resolve authorization (endpoint-level, else the configured AuthorizationProvider).

  2. Execute the call.

  3. On 401, refresh the credential and retry up to NetworkClientConfiguration.maxAuthRefreshAttempts.

  4. Map non-2xx and transport failures to NetworkError.

  5. Decode the success body into the requested type.

Thread-safe: the mutable configuration/client pair is guarded by a Mutex, and the active client is reference-counted so updateConfiguration never closes a client mid-request.

Constructors

Link copied to clipboard
constructor(configuration: NetworkClientConfiguration = NetworkClientConfiguration())

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
suspend fun close()

Closes the underlying HTTP client after in-flight requests complete. Once closed, this client cannot make requests or accept configuration updates.

Link copied to clipboard
suspend fun ApiClient.execute(endpoint: NetworkEndpoint)

Executes endpoint, discarding any response body.

Link copied to clipboard
open suspend override fun <T> request(endpoint: NetworkEndpoint, body: Any?, bodyType: TypeInfo?, responseType: TypeInfo): T

Executes endpoint, optionally sending body (described by bodyType), and decodes the response into responseType.

Link copied to clipboard
inline suspend fun <T> ApiClient.request(endpoint: NetworkEndpoint): T

Executes endpoint and decodes the response into T.

inline suspend fun <B, T> ApiClient.request(endpoint: NetworkEndpoint, body: B): T

Executes endpoint sending body of type B, and decodes the response into T.

Link copied to clipboard
suspend fun updateConfiguration(newConfiguration: NetworkClientConfiguration)

Swaps in a new NetworkClientConfiguration, rebuilding the underlying engine. The previous client is closed only once its in-flight requests complete, so reconfiguring mid-request is safe.