BloomHttpClient class

High-level HTTP client with base URL resolution, JSON encoding/decoding, Bearer auth token injection, request/response interceptors, and timeout handling.

BloomHttpClient wraps Dart's package:http with framework conventions for Bloom JS Native apps:

  • Base URL Resolution: Relative paths (e.g. '/tasks') are resolved against baseUrl. If baseUrl is not explicitly passed, it automatically falls back to BloomEnv.getOrNull('API_BASE_URL') or 'API_URL'. Absolute URLs (starting with http:// or https://) bypass baseUrl.
  • Authentication: Injects Authorization: Bearer <token> automatically on every request if authTokenProvider or authToken is configured.
  • JSON Codecs: Automatically sets Content-Type: application/json and Accept: application/json, encodes Map/List request bodies to JSON strings, and decodes JSON response payloads into Dart objects.
  • Interceptors: Supports pre-request and post-response processing pipelines via requestInterceptors and responseInterceptors.
  • Error Handling: Non-2xx responses throw an http.ClientException containing the HTTP status code and response body.

Backend Behavior

  • SSR & Dart VM: Fully functional. Requests execute using Dart's native IO client.
  • Browser: Fully functional. Requests execute using browser Fetch / XMLHttpRequest under the hood.

Example

final client = BloomHttpClient(
  baseUrl: 'https://api.example.com/v1',
  authTokenProvider: () => authStore.token.value,
  timeout: Duration(seconds: 10),
);

// Fetch JSON list
final List<dynamic> users = await client.get('/users');

// Post JSON payload
final Map<String, dynamic> newUser = await client.post(
  '/users',
  body: {'name': 'Alice', 'role': 'Admin'},
);

See also:

  • BloomQuery, for caching and deduplicating HTTP GET requests.
  • BloomMutation, for executing HTTP POST/PUT/DELETE mutations with optimistic updates.

Constructors

BloomHttpClient({String? baseUrl, Client? innerClient, Duration timeout = const Duration(seconds: 15), String? authToken, String? authTokenProvider()?})
Creates a BloomHttpClient configured with an optional baseUrl, timeout, and auth tokens.

Properties

authToken String?
Static Bearer authentication token attached to outgoing requests.
getter/setter pair
authTokenProvider String? Function()?
Dynamic provider callback resolving the current Bearer token at request time.
getter/setter pair
baseUrl String?
Base URL prefix prepended to relative request paths.
final
hashCode int
The hash code for this object.
no setterinherited
requestInterceptors List<RequestInterceptor>
Ordered list of interceptor callbacks executed sequentially prior to dispatching requests.
final
responseInterceptors List<ResponseInterceptor>
Ordered list of interceptor callbacks executed sequentially upon receiving responses.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeout Duration
Network timeout duration applied to requests before throwing a TimeoutException.
final

Methods

close() → void
Closes the underlying HTTP client and releases active socket connections.
delete<T>(String path, {dynamic body, Map<String, String>? headers, Map<String, dynamic>? queryParameters}) Future<T>
Sends an HTTP DELETE request to path with optional body and decodes the JSON response as T.
get<T>(String path, {Map<String, String>? headers, Map<String, dynamic>? queryParameters}) Future<T>
Sends an HTTP GET request to path and decodes the JSON response as T.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch<T>(String path, {dynamic body, Map<String, String>? headers, Map<String, dynamic>? queryParameters}) Future<T>
Sends an HTTP PATCH request to path with optional body and decodes the JSON response as T.
post<T>(String path, {dynamic body, Map<String, String>? headers, Map<String, dynamic>? queryParameters}) Future<T>
Sends an HTTP POST request to path with optional body and decodes the JSON response as T.
put<T>(String path, {dynamic body, Map<String, String>? headers, Map<String, dynamic>? queryParameters}) Future<T>
Sends an HTTP PUT request to path with optional body and decodes the JSON response as T.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited