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 27, 2023
1 parent 22d171a commit 9202008
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 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 @@ -180,31 +181,38 @@ class $clientException implements io.HttpException {
// CLASS: $clientName
// ==========================================
/// Client for ${spec.info.title}
///
/// `baseUrl`: Override baseUrl URL - else defaults to server url defined in spec
/// Client for ${spec.info.title} (v.${spec.info.version})
///
/// `client`: Override HTTP client to use for requests
/// ${spec.info.description ?? 'No description provided'}
class $clientName {
/// Creates a new $clientName instance.
///
/// - [$clientName.baseUrl] Override base URL (default: server url defined in spec)
/// - [$clientName.headers] Global headers to be sent with every request
/// - [$clientName.client] Override HTTP client to use for requests
$clientName({
$authInputCode
String? baseUrl,
this.baseUrl,
this.headers = 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());
}
/// User provided override for baseUrl URL
late final String? baseUrl;
),
assert(
baseUrl == null || !baseUrl.endsWith('/'),
'baseUrl must not end with /',
),
client = RetryClient(client ?? http.Client());
/// Override base URL (default: server url defined in spec)
final String? baseUrl;
/// Global headers to be sent with every request
final Map<String, String> headers;
/// 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 +298,9 @@ class $clientName {
if (responseType.isNotEmpty){
headers['${HttpHeaders.acceptHeader}'] = responseType;
}
// Add global headers
headers.addAll(this.headers);
// Build the request object
late http.Response response;
Expand Down

0 comments on commit 9202008

Please sign in to comment.