get<T> method
Executes HTTP GET request
path
- relative to baseUrl path
decoder
- function which will be called if http request was successful
to convert http.Response
to T
parameters
- collection of query parameters
headers
- collection of headers which should be applied to this request
Implementation
Future<ApiResult<T>> get<T>({
required String path,
required T Function(http.Response) decoder,
Map<String, Object> parameters = const {},
Map<String, String> headers = const {},
}) async {
final requestUrl = _buildRequestUrl(path, parameters: parameters);
logger?.info('HTTP GET $requestUrl');
try {
final requestHeaders = _buildHeaders(requestHeaders: headers);
final httpResponse = await http.get(
Uri.parse(requestUrl),
headers: requestHeaders,
);
if (httpResponse.statusCode != 200) {
throw Exception('${httpResponse.statusCode} ${httpResponse.body}');
}
final data = decoder.call(httpResponse);
return ApiResult.success(data);
} on Exception catch (error) {
logger?.error('HTTP GET FAILED', error);
return ApiResult.failed(error);
}
}