run<T> method

  1. @override
Future<T> run<T>(
  1. covariant PieFedApiQuery<T> query
)
override

Run an API query.

Implementation

@override
Future<T> run<T>(PieFedApiQuery<T> query) async {
  try {
    final queryJson = query.toJson();
    final res = await _makeRequest(query, queryJson).timeout(timeout);

    if (res.statusCode < 200 || res.statusCode >= 300) {
      String errorMessage;
      try {
        final Map<String, dynamic> json = jsonDecode(res.body);
        errorMessage = json['error'] ?? res.body;
      } catch (_) {
        errorMessage = res.body;
      }
      throw PieFedApiException(errorMessage);
    }

    final String responseBody = utf8.decode(res.bodyBytes);
    final Map<String, dynamic> json = responseBody.isEmpty ? <String, dynamic>{} : jsonDecode(responseBody);
    return query.responseFactory(json);
  } catch (e) {
    if (e is PieFedApiException) rethrow;
    throw PieFedApiException(e.toString());
  }
}