Skip to content

FluxbaseHttpClient

//fluxbase-kotlin/io.github.nimbleflux.fluxbase.core/FluxbaseHttpClient

class FluxbaseHttpClient(baseUrl: String, transport: HttpTransport, json: Json = defaultJson)

The shared HTTP client that every Fluxbase module (auth, postgrest, realtime, storage, jobs, …) uses to talk to the Fluxbase server. This is the Kotlin port of FluxbaseFetch (sdk/src/fetch.ts).

Responsibilities:

  • Hold the base URL (trailing slash stripped) and default headers.
  • Manage the auth token: setAuthToken(token) sets Authorization: Bearer; setAuthToken(null) restores the anon key fallback (does NOT delete the header — matches TS fetch.ts:93-102).
  • Provide convenience methods (get, post, put, patch, delete) that return reified typed results via kotlinx.serialization.
  • Delegate the actual I/O to an HttpTransport SPI, which makes the client trivially testable with a recording fake.

401 auto-refresh-retry (port of fetch.ts’s single-retry-after-refresh): When setRefreshTokenCallback has been wired (the client wires it to auth.refreshSession()), any request that fails with HTTP 401 triggers a single token refresh, then the request is retried exactly once with the new token. Concurrent 401s are deduped via refreshMutex so only one refresh fires even when many requests fail simultaneously. A second 401 (refresh didn’t help) is propagated to the caller — there is no retry loop.

jvm

baseUrl the Fluxbase server URL (trailing slash stripped).
transport the I/O SPI. If null, a Ktor-backed transport is used at runtime; tests inject an io.github.nimbleflux.fluxbase.core.test.RecordingHttp.
json the JSON decoder used for typed get/post responses.
FluxbaseHttpClient [jvm]
constructor(baseUrl: String, transport: HttpTransport, json: Json = defaultJson)
Name Summary
Companion [jvm]
object Companion
Name Summary
baseUrl [jvm]
val baseUrl: String
The base URL with any trailing slash removed.
defaultHeaders [jvm]
val defaultHeaders: MutableMap<String, String>
Default headers applied to every request. Content-Type: application/json is always present; Authorization is managed via setAuthToken.
Name Summary
delete [jvm]
suspend fun delete(path: String, headers: Map<String, String> = emptyMap())
DELETE path. No deserialization (TS returns void).
deleteWithHeaders [jvm]
suspend fun deleteWithHeaders(path: String, body: Any? = null, headers: Map<String, String> = emptyMap()): HttpResponse
DELETE that can carry a JSON body and returns response headers — used by account deletion (DELETE /api/v1/auth/account accepts an optional {password}). Mirrors fetch.delete(path, { body }) in fetch.ts.
get [jvm]
inline suspend fun <reified T> get(path: String, headers: Map<String, String> = emptyMap()): T
GET path, deserialize the JSON body to T.
getBytes [jvm]
suspend fun getBytes(path: String, headers: Map<String, String> = emptyMap()): ByteArray
GET path and return the response body as raw bytes — the binary-safe path (no charset decode). Used by storage downloads and any other non-text payload. Mirrors getBlob in sdk/src/fetch.ts.
getWithHeaders [jvm]
suspend fun getWithHeaders(path: String, headers: Map<String, String> = emptyMap()): HttpResponse
GET that also returns response headers — used by the query builder to parse Content-Range for count queries. Mirrors getWithHeaders in fetch.ts.
patch [jvm]
inline suspend fun <reified T> patch(path: String, body: Any? = null, headers: Map<String, String> = emptyMap()): T
PATCH body to path, deserialize the JSON response to T.
post [jvm]
inline suspend fun <reified T> post(path: String, body: Any? = null, headers: Map<String, String> = emptyMap()): T
POST body to path, deserialize the JSON response to T.
postWithHeaders [jvm]
suspend fun postWithHeaders(path: String, body: Any? = null, headers: Map<String, String> = emptyMap()): HttpResponse
POST that also returns response headers. Mirrors postWithHeaders in fetch.ts.
put [jvm]
inline suspend fun <reified T> put(path: String, body: Any? = null, headers: Map<String, String> = emptyMap()): T
PUT body to path, deserialize the JSON response to T.
removeHeader [jvm]
fun removeHeader(name: String)
Remove a custom header.
setAnonKey [jvm]
fun setAnonKey(key: String)
Set the anon key used as the Authorization fallback on sign-out.
setAuthToken [jvm]
fun setAuthToken(token: String?)
Update the authorization token.
setHeader [jvm]
fun setHeader(name: String, value: String)
Set a custom header on all subsequent requests (e.g. X-FB-Tenant).
setRefreshTokenCallback [jvm]
fun setRefreshTokenCallback(callback: suspend () -> String?)
Register the callback that refreshes the access token. On a 401 the client invokes this once (deduped across concurrent requests), applies the returned token via setAuthToken, and retries the original request a single time. Mirrors TS setRefreshTokenCallback in fetch.ts.