Skip to content

Commit

Permalink
Allow to define global headers included in every request
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Oct 26, 2023
1 parent 21271e9 commit c50d4da
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/src/generators/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class $clientException implements io.HttpException {
this.code,
this.body,
});
@override
final String message;
@override
Expand Down Expand Up @@ -188,23 +189,27 @@ class $clientException implements io.HttpException {
class $clientName {
$clientName({
$authInputCode
String? baseUrl,
this.baseUrl,
this.globalHeaders = const {},
http.Client? client,
}) : assert(
baseUrl == null || baseUrl.startsWith('http'),
'baseUrl must start with http',
) {
// Ensure trailing slash is removed from baseUrl
this.baseUrl = baseUrl?.replaceAll(RegExp(r'/\$'), '');
// Create a retry client
this.client = RetryClient(client ?? http.Client());
}
),
assert(
baseUrl == null || !baseUrl.endsWith('/'),
'baseUrl must not end with /',
),
client = RetryClient(client ?? http.Client());
/// User provided override for baseUrl URL
late final String? baseUrl;
final String? baseUrl;
/// Headers to be sent with every request
final Map<String, String> globalHeaders;
/// HTTP client for requests
late final http.Client client;
final http.Client client;
${authVariables.isEmpty ? '' : '/// Authentication related variables'}
${authVariables.join('\n')}
Expand Down Expand Up @@ -290,6 +295,9 @@ class $clientName {
if (responseType.isNotEmpty){
headers['${HttpHeaders.acceptHeader}'] = responseType;
}
// Add global headers
headers.addAll(globalHeaders);
// Build the request object
late http.Response response;
Expand Down

0 comments on commit c50d4da

Please sign in to comment.