request method

Future<HyperDeckRestResponse> request(
  1. String method,
  2. String path, {
  3. Object? body,
  4. Map<String, String>? headers,
})

Implementation

Future<HyperDeckRestResponse> request(String method, String path,
    {Object? body, Map<String, String>? headers}) async {
  final uri =
      baseUri.resolve(path.startsWith('/') ? path.substring(1) : path);
  final request = await _http.openUrl(method, uri);
  request.headers.set(HttpHeaders.acceptHeader, 'application/json');
  headers?.forEach(request.headers.set);
  if (body != null) {
    request.headers.contentType = ContentType.json;
    request.write(jsonEncode(body));
  }
  final response = await request.close();
  final text = await utf8.decoder.bind(response).join();
  Object? value;
  if (text.isNotEmpty) {
    try {
      value = jsonDecode(text);
    } on FormatException {
      value = text;
    }
  }
  final result =
      HyperDeckRestResponse(response.statusCode, value, response.headers);
  if (response.statusCode < 200 || response.statusCode >= 300) {
    throw HyperDeckRestException(result);
  }
  return result;
}