send method

Future<Object?> send(
  1. String method,
  2. dynamic url, {
  3. dynamic json,
})

Creates a request with a HTTP method, url and optional data. The url can be either a String or Uri.

Implementation

Future<Object?> send(String method, url, {json}) async {
  final uri = url is String ? Uri.parse(url) : url as Uri;

  final request = Request(method, uri);
  if (credential != null) {
    request.headers['Authorization'] = 'Bearer $credential';
  }

  if (json != null) {
    request.headers['Content-Type'] = 'application/json';
    request.body = jsonEncode(json);
  }

  final streamedResponse = await _client.send(request);
  final response = await Response.fromStream(streamedResponse);

  Object? bodyJson;
  try {
    bodyJson = jsonDecode(response.body);
  } on FormatException {
    final contentType = response.headers['content-type'];
    if (contentType != null && !contentType.contains('application/json')) {
      throw Exception(
          "Returned value was not JSON. Did the uri end with '.json'?");
    }
    rethrow;
  }

  if (response.statusCode != 200) {
    if (bodyJson is Map) {
      final error = bodyJson['error'];
      if (error != null) {
        throw FirebaseClientException(response.statusCode, error.toString());
      }
    }

    throw FirebaseClientException(response.statusCode, bodyJson.toString());
  }

  return bodyJson;
}