send<T> method

Future<T> send<T>(
  1. String method,
  2. String pathTemplate, {
  3. Map<String, String>? pathParameters,
  4. Map<String, String>? queryParameters,
  5. Map<String, String>? headers,
  6. dynamic body,
  7. MultipartFile? file,
  8. bool? followRedirects,
})

Implementation

Future<T> send<T>(
  String method,
  String pathTemplate, {
  Map<String, String>? pathParameters,
  Map<String, String>? queryParameters,
  Map<String, String>? headers,
  dynamic body,
  MultipartFile? file,
  bool? followRedirects,
}) async {
  followRedirects ??= true;
  var path = pathTemplate;

  if (pathParameters != null) {
    for (var pathParameter in pathParameters.entries) {
      path = path.replaceAll(
          '{${pathParameter.key}}', Uri.encodeComponent(pathParameter.value));
    }
  }
  assert(!path.contains('{'));

  if (path.startsWith('/')) {
    path = path.substring(1);
  }

  var uri = _baseUri.replace(path: p.url.join(_baseUri.path, path));
  if (queryParameters != null) {
    uri = uri.replace(queryParameters: {
      ...uri.queryParameters,
      ...queryParameters,
    });
  }

  if (body is MultipartFile) {
    file ??= body;
  }

  BaseRequest request;
  if (file != null) {
    request = MultipartRequest(method, uri)
      ..headers[_headerAtlassianToken] ??= 'no-check'
      ..headers['content-type'] = 'multipart/form-data'
      ..files.add(file);
  } else {
    var bodyRequest = Request(method, uri);
    request = bodyRequest;

    if (body != null) {
      bodyRequest
        ..headers['content-type'] = 'application/json'
        ..body = jsonEncode(body);
    }
  }
  if (headers != null) {
    request.headers.addAll(headers);
  }

  request
    ..headers[_headerExperimental] = 'opt-in'
    ..headers['User-Agent'] = 'Dart/atlassian_apis'
    ..followRedirects = followRedirects;

  var response = await Response.fromStream(await _client.send(request));
  ApiException.checkResponse(response);

  if (response.statusCode == 302) {
    assert(T == Uri);
    return (Uri.parse(response.headers['location'] ?? '')) as T;
  }

  var decoded = _decode(response);
  return decoded as T;
}