send<D> method
Future<AgResponse<D> >
send<D>(
- AgRequest request, {
- D decode(
- Object? json
- AgCancelToken? cancelToken,
- void onSendProgress()?,
- void onReceiveProgress()?,
override
Sends request, returning a decoded AgResponse or throwing an
AgApiException. decode converts the raw JSON body to D; when
omitted, the raw body is used as-is (only safe when D is dynamic
or already matches the raw shape).
Implementation
@override
Future<AgResponse<D>> send<D>(
AgRequest request, {
D Function(Object? json)? decode,
AgCancelToken? cancelToken,
void Function(int, int)? onSendProgress,
void Function(int, int)? onReceiveProgress,
}) async {
requests.add(request);
final reply = _replies[_key(request.method, request.endpoint)];
if (reply == null) {
throw StateError(
'AgFakeApiProvider: no reply for ${request.method} ${request.endpoint.path}. '
'Set one up with reply() or fail() before sending it.',
);
}
if (reply.error case final error?) throw error;
final json = reply.json!(request);
// The real provider rejects a non-2xx status (dio's default
// validateStatus), so a `reply(statusCode: 404)` must fail here too.
if (reply.statusCode < 200 || reply.statusCode > 299) {
throw AgApiException(
statusCode: reply.statusCode,
message: 'Request failed with status ${reply.statusCode}',
responseBody: json,
type: AgApiErrorType.response,
);
}
return AgResponse<D>(
statusCode: reply.statusCode,
data: decode != null ? decode(json) : json as D,
headers: reply.headers,
duration: Duration.zero,
);
}