HttpClient class

A robust HTTP Client with built-in support for Server-Sent Events (SSE) and structured error handling.

This client automatically handles JSON encoding/decoding, request timeouts, and wraps results in an AppResponse for consistent data handling across the app.

Constructors

HttpClient({required String baseUrl, Map<String, String>? defaultHeaders, Duration timeout = const Duration(seconds: 50), Client? client})
Creates an HttpClient.

Properties

baseUrl String
The base URL for all relative endpoints (e.g., 'https://api.tuul.ai/v1').
final
defaultHeaders Map<String, String>
Headers sent with every request (e.g., Authorization, User-Agent).
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
timeout Duration
The maximum duration to wait for a standard HTTP response. Defaults to 50 seconds.
final

Methods

close() → void
Closes the underlying HTTP client.
delete<T>(String endpoint, {Map<String, String>? headers, Map<String, dynamic>? queryParameters, T dataParser(dynamic json)?}) Future<AppResponse<T>>
Performs a DELETE request.
get<T>(String endpoint, {Map<String, String>? headers, Map<String, dynamic>? queryParameters, T dataParser(dynamic json)?}) Future<AppResponse<T>>
Performs a GET request.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch<T>(String endpoint, {Map<String, String>? headers, dynamic body, Map<String, dynamic>? queryParameters, T dataParser(dynamic json)?}) Future<AppResponse<T>>
Performs a PATCH request.
post<T>(String endpoint, {Map<String, String>? headers, dynamic body, Map<String, dynamic>? queryParameters, T dataParser(dynamic json)?}) Future<AppResponse<T>>
Performs a POST request.
put<T>(String endpoint, {Map<String, String>? headers, dynamic body, Map<String, dynamic>? queryParameters, T dataParser(dynamic json)?}) Future<AppResponse<T>>
Performs a PUT request.
sse(String endpoint, {String method = 'GET', Map<String, String>? headers, dynamic body, Map<String, dynamic>? queryParameters, required void onChunk(String chunk), void onDone()?, void onError(Object error, StackTrace stackTrace)?}) Future<void>
Initiates a Server-Sent Events (SSE) connection using a callback mechanism.
sseStream(String endpoint, {String method = 'GET', Map<String, String>? headers, dynamic body, Map<String, dynamic>? queryParameters}) Stream<String>
Connects to a Server-Sent Events (SSE) source and returns a Stream.
toString() String
A string representation of this object.
inherited

Operators

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