call method

Future call(
  1. String verb,
  2. String path, {
  3. Map<String, dynamic>? params,
  4. Object? data,
  5. String? httpAccept,
})
inherited

call will use the response content-type to automatically determine if the response should be decoded from json or returned as Uint8List directly.

path should be a relative path according to the api. e.g. /v1/status/updates

Implementation

Future call(String verb, String path, {Map<String, dynamic>? params, Object? data, String? httpAccept}) async {
  httpAccept ??= 'application/json';
  var uri = _makeUri(path, params: params);
  var headers = {'content-type': 'application/json', 'accept': httpAccept};
  late http.Response r;
  switch (verb.toLowerCase()) {
    case 'get':
      r = await http.get(uri, headers: headers);
      break;
    case 'put':
      r = await http.put(uri, headers: headers, body: data);
      break;
    case 'delete':
      r = await http.delete(uri, headers: headers, body: data);
      break;
    case 'post':
      r = await http.post(uri, headers: headers, body: data);
      break;
    case 'patch':
      r = await http.patch(uri, headers: headers, body: data);
      break;
  }
  if (r.statusCode > 199 && r.statusCode < 300) {
    if (r.statusCode == 204) return true;
    if (r.headers['content-type'] == 'application/json') {
      return json.decode(r.body);
    } else {
      return r.bodyBytes;
    }
  } else {
    throw http.ClientException(r.body);
  }
}