copyWithAuthorization method

HttpRequest copyWithAuthorization(
  1. HttpClient client, [
  2. Authorization? authorization
])

Copies this instance with a different client and authorization if provided.

If authorization is null returns this.

Implementation

HttpRequest copyWithAuthorization(
  HttpClient client, [
  Authorization? authorization,
]) {
  if (authorization == null || authorization == this.authorization) {
    return this;
  }

  // Without the `Authorization` header of the previous credential: the
  // headers passed are applied last, and it would replace the new one.
  var headers = this.requestHeaders != null
      ? Map<String, String>.from(this.requestHeaders!)
      : null;
  var authorizationKey = _getMapKeyIgnoreCase(headers, 'Authorization');
  if (authorizationKey != null) headers!.remove(authorizationKey);

  var requestHeaders = client.clientRequester.buildRequestHeaders(
    client,
    method,
    url,
    headers: headers,
    authorization: authorization,
    contentType: headerContentType,
    accept: headerAccept,
  );

  var queryParameters = this.queryParameters != null
      ? Map<String, String?>.from(this.queryParameters!)
      : null;

  var requestURL = client.clientRequester.buildRequestURL(
    client,
    url,
    authorization: authorization,
    queryParameters: _queryParametersAsBody ? null : queryParameters,
    noQueryString: noQueryString,
  );

  var copy = HttpRequest(
    method,
    url,
    requestURL,
    queryParameters: queryParameters,
    noQueryString: noQueryString,
    authorization: authorization,
    withCredentials: withCredentials,
    responseType: responseType,
    mimeType: mimeType,
    requestHeaders: requestHeaders,
    sendData: sendData,
  );

  copy._retries = _retries;
  copy._queryParametersAsBody = _queryParametersAsBody;

  return copy;
}