send method Null safety

  1. @override
Future<StreamedResponse> send(
  1. BaseRequest request

Sends an HTTP request with OAuth2 authorization credentials attached.

This will also automatically refresh this client's Credentials before sending the request if necessary.


Future<http.StreamedResponse> send(http.BaseRequest request) async {
  if (credentials.isExpired) {
    if (!credentials.canRefresh) throw ExpirationException(credentials);
    await refreshCredentials();

  request.headers['authorization'] = 'Bearer ${credentials.accessToken}';
  var response = await _httpClient!.send(request);

  if (response.statusCode != 401) return response;
  if (!response.headers.containsKey('www-authenticate')) return response;

  List<AuthenticationChallenge> challenges;
  try {
    challenges = AuthenticationChallenge.parseHeader(
  } on FormatException {
    return response;

  var challenge = challenges
      .firstWhereOrNull((challenge) => challenge.scheme == 'bearer');
  if (challenge == null) return response;

  var params = challenge.parameters;
  if (!params.containsKey('error')) return response;

  throw AuthorizationException(params['error']!, params['error_description'],
      params['error_uri'] == null ? null : Uri.parse(params['error_uri']!));