send method
Send a request resolving SDK URL / auth / default header rules.
Implementation
Future<HttpClientResponse> send({
required String method,
required String path,
Map<String, String>? headers,
Object? body,
bool requiresAuth = false,
int? timeoutMs,
bool followRedirects = true,
}) async {
final snapshot = _snapshot();
if (snapshot.baseURL.isEmpty) {
throw HttpClientException('HTTPClientAdapter not configured');
}
final url = _buildFullURL(path, snapshot.baseURL);
final resolvedHeaders = await _buildHeaders(
snapshot: snapshot,
path: path,
extra: headers,
requiresAuth: requiresAuth,
);
_requireCurrentConfiguration(snapshot.generation);
final encodedBody = _encodeBody(body);
var response = await rawRequest(
method: method,
url: _maybeAppendSupabaseUpsert(url, path, snapshot.environment),
headers: resolvedHeaders,
body: encodedBody,
timeoutMs: timeoutMs ?? snapshot.timeoutMs,
followRedirects: _redirectsAllowedForHeaders(
followRedirects,
resolvedHeaders,
),
);
_requireCurrentConfiguration(snapshot.generation);
// Retry-once on 401 after a token refresh.
if (response.statusCode == 401 &&
requiresAuth &&
snapshot.refreshTokenCallback != null) {
try {
_requireCurrentConfiguration(snapshot.generation);
final newToken = await snapshot.refreshTokenCallback!.call();
_requireCurrentConfiguration(snapshot.generation);
if (newToken != null && newToken.isNotEmpty) {
_accessToken = newToken;
final retryHeaders = Map<String, String>.from(resolvedHeaders);
retryHeaders['Authorization'] = 'Bearer $newToken';
response = await rawRequest(
method: method,
url: _maybeAppendSupabaseUpsert(url, path, snapshot.environment),
headers: retryHeaders,
body: encodedBody,
timeoutMs: timeoutMs ?? snapshot.timeoutMs,
followRedirects: _redirectsAllowedForHeaders(
followRedirects,
retryHeaders,
),
);
_requireCurrentConfiguration(snapshot.generation);
}
} catch (_) {
_requireCurrentConfiguration(snapshot.generation);
_logger.warning('Token refresh failed');
}
}
return response;
}