invokeAPI method
Implementation
Future<Response> invokeAPI(
String path,
String method,
Iterable<QueryParam> queryParams,
Object? body,
Map<String, String> headerParams,
Map<String, String> formParams,
String? nullableContentType,
List<String> authNames,
) async {
_updateParamsForAuth(authNames, queryParams as List<QueryParam>, headerParams);
headerParams.addAll(_defaultHeaderMap);
final urlEncodedQueryParams =
queryParams.where((param) => param.value != null).map((param) => '$param');
final queryString =
urlEncodedQueryParams.isNotEmpty ? '?${urlEncodedQueryParams.join('&')}' : '';
final url = Uri.parse('$basePath$path$queryString');
if (nullableContentType != null) {
headerParams['Content-Type'] = nullableContentType;
}
try {
// Special case for uploading a single file which isn't a 'multipart/form-data'.
if (body is MultipartFile &&
(nullableContentType == null ||
!nullableContentType.toLowerCase().startsWith('multipart/form-data'))) {
final request = StreamedRequest(method, url);
request.headers.addAll(headerParams);
request.contentLength = body.length;
body.finalize().listen(
request.sink.add,
onDone: request.sink.close,
onError: (error, trace) => request.sink.close(),
cancelOnError: true,
);
final response = await _client.send(request);
return Response.fromStream(response);
}
if (body is MultipartRequest) {
final request = MultipartRequest(method, url);
request.fields.addAll(body.fields);
request.files.addAll(body.files);
request.headers.addAll(body.headers);
request.headers.addAll(headerParams);
final response = await _client.send(request);
return Response.fromStream(response);
}
final msgBody = nullableContentType == 'application/x-www-form-urlencoded'
? formParams
: await serializeAsync(body);
final nullableHeaderParams = headerParams.isEmpty ? null : headerParams;
switch (method) {
case 'POST':
return await _client.post(
url,
headers: nullableHeaderParams,
body: msgBody,
);
case 'PUT':
return await _client.put(
url,
headers: nullableHeaderParams,
body: msgBody,
);
case 'DELETE':
return await _client.delete(
url,
headers: nullableHeaderParams,
);
case 'PATCH':
return await _client.patch(
url,
headers: nullableHeaderParams,
body: msgBody,
);
case 'HEAD':
return await _client.head(
url,
headers: nullableHeaderParams,
);
case 'GET':
return await _client.get(
url,
headers: nullableHeaderParams,
);
}
} on SocketException catch (e, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'Socket operation failed: $method $path',
e,
trace,
);
} on TlsException catch (e, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'TLS/SSL communication failed: $method $path',
e,
trace,
);
} on IOException catch (e, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'I/O operation failed: $method $path',
e,
trace,
);
} on ClientException catch (e, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'HTTP connection failed: $method $path',
e,
trace,
);
} on Exception catch (e, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'Exception occurred: $method $path',
e,
trace,
);
}
throw ApiException(
HttpStatus.badRequest,
'Invalid HTTP operation: $method $path',
);
}