request method
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;
}