Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class {{{classname}}} {
{{/headerParams}}
{{/hasHeaderParams}}

const authNames = <String>[{{#authMethods}}'{{{name}}}'{{^-last}}, {{/-last}}{{/authMethods}}];
const contentTypes = <String>[{{#prioritizedContentTypes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/prioritizedContentTypes}}];

{{#isMultipart}}
Expand Down Expand Up @@ -129,7 +128,6 @@ class {{{classname}}} {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
{{>header}}
{{>part_of}}
class ApiClient {
ApiClient({this.basePath = '{{{basePath}}}'}) {
{{#hasAuthMethods}}
// Setup authentications (key: authentication name, value: authentication).
{{#authMethods}}
{{#isBasic}}
{{#isBasicBasic}}
_authentications[r'{{{name}}}'] = HttpBasicAuth();
{{/isBasicBasic}}
{{#isBasicBearer}}
_authentications[r'{{{name}}}'] = HttpBearerAuth();
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
_authentications[r'{{{name}}}'] = ApiKeyAuth({{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{^isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{^isKeyInHeader}}'query'{{/isKeyInHeader}}{{/isKeyInCookie}}, '{{{keyParamName}}}');
{{/isApiKey}}
{{#isOAuth}}
_authentications[r'{{{name}}}'] = OAuth();
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
}
ApiClient({this.basePath = '{{{basePath}}}', this.authentication});

final String basePath;

Expand All @@ -38,23 +18,14 @@ class ApiClient {
}

final _defaultHeaderMap = <String, String>{};
final _authentications = <String, Authentication>{};
final Authentication? authentication;

void addDefaultHeader(String key, String value) {
_defaultHeaderMap[key] = value;
}

Map<String,String> get defaultHeaderMap => _defaultHeaderMap;

/// Returns an unmodifiable [Map] of the authentications, since none should be added
/// or deleted.
Map<String, Authentication> get authentications => Map.unmodifiable(_authentications);

T? getAuthentication<T extends Authentication>(String name) {
final authentication = _authentications[name];
return authentication is T ? authentication : null;
}

// We don't use a Map<String, String> for queryParams.
// If collectionFormat is 'multi', a key might appear multiple times.
Future<Response> invokeAPI(
Expand All @@ -65,9 +36,8 @@ class ApiClient {
Map<String, String> headerParams,
Map<String, String> formParams,
String? contentType,
List<String> authNames,
) async {
_updateParamsForAuth(authNames, queryParams, headerParams);
_updateParamsForAuth(queryParams, headerParams);

headerParams.addAll(_defaultHeaderMap);
if (contentType != null) {
Expand Down Expand Up @@ -188,18 +158,12 @@ class ApiClient {
String serialize(Object? value) => value == null ? '' : json.encode(value);

/// Update query and header parameters based on authentication settings.
/// @param authNames The authentications to apply
void _updateParamsForAuth(
List<String> authNames,
List<QueryParam> queryParams,
Map<String, String> headerParams,
) {
for(final authName in authNames) {
final auth = _authentications[authName];
if (auth == null) {
throw ArgumentError('Authentication undefined: $authName');
}
auth.applyToParams(queryParams, headerParams);
if (authentication != null) {
authentication!.applyToParams(queryParams, headerParams);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['petstore_auth'];
const contentTypes = <String>['application/json', 'application/xml'];


Expand All @@ -49,7 +48,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -104,7 +102,6 @@ class PetApi {
headerParams[r'api_key'] = parameterToString(apiKey);
}

const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];


Expand All @@ -116,7 +113,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -160,7 +156,6 @@ class PetApi {

queryParams.addAll(_queryParams('csv', 'status', status));

const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];


Expand All @@ -172,7 +167,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -225,7 +219,6 @@ class PetApi {

queryParams.addAll(_queryParams('csv', 'tags', tags));

const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];


Expand All @@ -237,7 +230,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -289,7 +281,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['api_key'];
const contentTypes = <String>[];


Expand All @@ -301,7 +292,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -349,7 +339,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['petstore_auth'];
const contentTypes = <String>['application/json', 'application/xml'];


Expand All @@ -361,7 +350,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -416,7 +404,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['petstore_auth'];
const contentTypes = <String>['application/x-www-form-urlencoded'];

if (name != null) {
Expand All @@ -434,7 +421,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -487,7 +473,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['petstore_auth'];
const contentTypes = <String>['multipart/form-data'];

bool hasFields = false;
Expand All @@ -513,7 +498,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>[];
const contentTypes = <String>[];


Expand All @@ -50,7 +49,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -85,7 +83,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>['api_key'];
const contentTypes = <String>[];


Expand All @@ -97,7 +94,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -141,7 +137,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>[];
const contentTypes = <String>[];


Expand All @@ -153,7 +148,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down Expand Up @@ -201,7 +195,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};

const authNames = <String>[];
const contentTypes = <String>['application/json'];


Expand All @@ -213,7 +206,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

Expand Down
Loading