ChopperClient constructor
- String baseUrl = '',
- Client? client,
- Iterable interceptors = const [],
- Authenticator? authenticator,
- Converter? converter,
- ErrorConverter? errorConverter,
- Iterable<
ChopperService> services = const [],
Creates and configures a ChopperClient.
The base URL of each request of the registered services can be defined with the baseUrl parameter. E.g., the hostname of your service.
A custom HTTP client can be passed as the client
parameter to be used
with the created ChopperClient.
If not provided, a default http.Client
will be used.
ChopperServices can be added to the client with the services
parameter.
See the ChopperApi annotation to learn more about creating services.
final chopper = ChopperClient(
baseUrl: 'localhost:8000',
services: [
// Add a generated service
TodosListService.create()
],
);
RequestInterceptors and ResponseInterceptors can be added to the client
with the interceptors
parameter.
See RequestInterceptor, ResponseInterceptor, HttpLoggingInterceptor, HeadersInterceptor, CurlInterceptor
final chopper = ChopperClient(
...
interceptors: [
HttpLoggingInterceptor(),
]
);
Converters can be added to the client with the converter parameter. Converters are used to convert the body of requests and responses to and from the HTTP format.
A different converter can be used to handle error responses (when Response.isSuccessful == false)) with tche errorConverter parameter.
See Converter, JsonConverter
final chopper = ChopperClient(
...
converter: JsonConverter(),
errorConverter: JsonConverter(),
);
Implementation
ChopperClient({
this.baseUrl = '',
http.Client? client,
Iterable interceptors = const [],
this.authenticator,
this.converter,
this.errorConverter,
Iterable<ChopperService> services = const [],
}) : httpClient = client ?? http.Client(),
_clientIsInternal = client == null {
if (interceptors.every(_isAnInterceptor) == false) {
throw ArgumentError(
'Unsupported type for interceptors, it only support the following types:\n'
'${allowedInterceptorsType.join('\n - ')}',
);
}
_requestInterceptors.addAll(interceptors.where(_isRequestInterceptor));
_responseInterceptors.addAll(interceptors.where(_isResponseInterceptor));
services.toSet().forEach((s) {
s.client = this;
_services[s.definitionType] = s;
});
}