RestAPI class abstract

Base class for all REST API implementations.

Subclasses must override host, headers, and shortname.

Example:

class MyAPI extends RestAPI {
  @override
  String get host => 'api.example.com';

  @override
  Map<String, String> get headers => {
    'Authorization': 'Bearer $token',
    'Content-Type': 'application/json',
  };

  @override
  String get shortname => 'my_api';
}

Testing

Use useMockClient in test setup to mock all API calls:

setUp(() {
  useMockClient();
});

tearDown(() {
  useRealClient();
});

Constructors

RestAPI()

Properties

client → Client
HTTP client for making requests.
no setter
hashCode int
The hash code for this object.
no setterinherited
headers Map<String, String>
Headers to include with every request.
no setter
host String
The base host for API requests (e.g., "api.example.com").
no setter
logger Logger
Logger instance. Override to use a custom logger.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shortname String
Short name for logging purposes.
no setter

Methods

delete(String endpoint, {Object? body, Map<String, String>? extraHeaders}) Future<Response?>
Sends a DELETE request to endpoint.
get(String endpoint, {Map<String, String>? queryParams, Map<String, String>? extraHeaders}) Future<Response?>
Sends a GET request to endpoint.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch(String endpoint, {Object? body, Map<String, String>? extraHeaders}) Future<Response?>
Sends a PATCH request to endpoint.
post(String endpoint, {Object? body, Map<String, String>? extraHeaders}) Future<Response?>
Sends a POST request to endpoint.
put(String endpoint, {Object? body, Map<String, String>? extraHeaders}) Future<Response?>
Sends a PUT request to endpoint.
sendRaw(String url, {Map<String, String>? extraHeaders}) Future<Response?>
Send a GET request to a raw URL (bypasses host).
toString() String
A string representation of this object.
inherited

Operators

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