dart2: Fix authentication (#11360)

* dart2: Fix authentication

* dart2: Update samples
This commit is contained in:
jld3103
2022-04-26 20:13:19 +02:00
committed by GitHub
parent 73a534d54c
commit 37905e8bfa
14 changed files with 15 additions and 218 deletions

View File

@@ -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}}
@@ -129,7 +128,6 @@ class {{{classname}}} {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -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;
@@ -38,7 +18,7 @@ class ApiClient {
}
final _defaultHeaderMap = <String, String>{};
final _authentications = <String, Authentication>{};
final Authentication? authentication;
void addDefaultHeader(String key, String value) {
_defaultHeaderMap[key] = value;
@@ -46,15 +26,6 @@ class ApiClient {
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(
@@ -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) {
@@ -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);
}
}

View File

@@ -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'];
@@ -49,7 +48,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -104,7 +102,6 @@ class PetApi {
headerParams[r'api_key'] = parameterToString(apiKey);
}
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -116,7 +113,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -160,7 +156,6 @@ class PetApi {
queryParams.addAll(_queryParams('csv', 'status', status));
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -172,7 +167,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -225,7 +219,6 @@ class PetApi {
queryParams.addAll(_queryParams('csv', 'tags', tags));
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -237,7 +230,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -289,7 +281,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -301,7 +292,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -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'];
@@ -361,7 +350,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -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) {
@@ -434,7 +421,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -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;
@@ -513,7 +498,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -38,7 +38,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -50,7 +49,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -85,7 +83,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -97,7 +94,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -141,7 +137,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -153,7 +148,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -201,7 +195,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -213,7 +206,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -37,7 +37,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>['application/json'];
@@ -49,7 +48,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -89,7 +87,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>['application/json'];
@@ -101,7 +98,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -141,7 +137,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>['application/json'];
@@ -153,7 +148,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -194,7 +188,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -206,7 +199,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -247,7 +239,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -259,7 +250,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -313,7 +303,6 @@ class UserApi {
queryParams.addAll(_queryParams('', 'username', username));
queryParams.addAll(_queryParams('', 'password', password));
const authNames = <String>[];
const contentTypes = <String>[];
@@ -325,7 +314,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -371,7 +359,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -383,7 +370,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -422,7 +408,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>['application/json'];
@@ -434,7 +419,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -11,11 +11,7 @@
part of openapi.api;
class ApiClient {
ApiClient({this.basePath = 'http://petstore.swagger.io/v2'}) {
// Setup authentications (key: authentication name, value: authentication).
_authentications[r'api_key'] = ApiKeyAuth('header', 'api_key');
_authentications[r'petstore_auth'] = OAuth();
}
ApiClient({this.basePath = 'http://petstore.swagger.io/v2', this.authentication});
final String basePath;
@@ -32,7 +28,7 @@ class ApiClient {
}
final _defaultHeaderMap = <String, String>{};
final _authentications = <String, Authentication>{};
final Authentication? authentication;
void addDefaultHeader(String key, String value) {
_defaultHeaderMap[key] = value;
@@ -40,15 +36,6 @@ class ApiClient {
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(
@@ -59,9 +46,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) {
@@ -180,18 +166,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);
}
}

View File

@@ -37,7 +37,6 @@ class AnotherFakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -49,7 +48,6 @@ class AnotherFakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -28,7 +28,6 @@ class DefaultApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -40,7 +39,6 @@ class DefaultApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -30,7 +30,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -42,7 +41,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -95,7 +93,6 @@ class FakeApi {
headerParams[r'header_1'] = parameterToString(header1);
}
const authNames = <String>['http_signature_test'];
const contentTypes = <String>['application/json', 'application/xml'];
@@ -107,7 +104,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -149,7 +145,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -161,7 +156,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -205,7 +199,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -217,7 +210,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -261,7 +253,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -273,7 +264,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -317,7 +307,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -329,7 +318,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -373,7 +361,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -385,7 +372,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -429,7 +415,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['image/png'];
@@ -441,7 +426,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -476,7 +460,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -488,7 +471,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -523,7 +505,6 @@ class FakeApi {
queryParams.addAll(_queryParams('', 'query', query));
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -535,7 +516,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -572,7 +552,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -584,7 +563,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -671,7 +649,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['http_basic_test'];
const contentTypes = <String>['application/x-www-form-urlencoded'];
if (integer != null) {
@@ -722,7 +699,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -847,7 +823,6 @@ class FakeApi {
headerParams[r'enum_header_string'] = parameterToString(enumHeaderString);
}
const authNames = <String>[];
const contentTypes = <String>['application/x-www-form-urlencoded'];
if (enumFormStringArray != null) {
@@ -865,7 +840,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -957,7 +931,6 @@ class FakeApi {
headerParams[r'boolean_group'] = parameterToString(booleanGroup);
}
const authNames = <String>['bearer_test'];
const contentTypes = <String>[];
@@ -969,7 +942,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -1024,7 +996,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -1036,7 +1007,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -1079,7 +1049,6 @@ class FakeApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/x-www-form-urlencoded'];
if (param != null) {
@@ -1097,7 +1066,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -1159,7 +1127,6 @@ class FakeApi {
}
queryParams.addAll(_queryParams('', 'allowEmpty', allowEmpty));
const authNames = <String>[];
const contentTypes = <String>[];
@@ -1171,7 +1138,6 @@ class FakeApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -37,7 +37,6 @@ class FakeClassnameTags123Api {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key_query'];
const contentTypes = <String>['application/json'];
@@ -49,7 +48,6 @@ class FakeClassnameTags123Api {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -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'];
@@ -49,7 +48,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -96,7 +94,6 @@ class PetApi {
headerParams[r'api_key'] = parameterToString(apiKey);
}
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -108,7 +105,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -152,7 +148,6 @@ class PetApi {
queryParams.addAll(_queryParams('csv', 'status', status));
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -164,7 +159,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -217,7 +211,6 @@ class PetApi {
queryParams.addAll(_queryParams('csv', 'tags', tags));
const authNames = <String>['petstore_auth'];
const contentTypes = <String>[];
@@ -229,7 +222,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -281,7 +273,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -293,7 +284,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -341,7 +331,6 @@ class PetApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['petstore_auth'];
const contentTypes = <String>['application/json', 'application/xml'];
@@ -353,7 +342,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -400,7 +388,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) {
@@ -418,7 +405,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -471,7 +457,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;
@@ -497,7 +482,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -558,7 +542,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;
@@ -584,7 +567,6 @@ class PetApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -38,7 +38,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -50,7 +49,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -85,7 +83,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>['api_key'];
const contentTypes = <String>[];
@@ -97,7 +94,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -141,7 +137,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -153,7 +148,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -201,7 +195,6 @@ class StoreApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -213,7 +206,6 @@ class StoreApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -37,7 +37,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -49,7 +48,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -89,7 +87,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -101,7 +98,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -141,7 +137,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -153,7 +148,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -194,7 +188,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -206,7 +199,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -247,7 +239,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -259,7 +250,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -313,7 +303,6 @@ class UserApi {
queryParams.addAll(_queryParams('', 'username', username));
queryParams.addAll(_queryParams('', 'password', password));
const authNames = <String>[];
const contentTypes = <String>[];
@@ -325,7 +314,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -371,7 +359,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>[];
@@ -383,7 +370,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}
@@ -422,7 +408,6 @@ class UserApi {
final headerParams = <String, String>{};
final formParams = <String, String>{};
const authNames = <String>[];
const contentTypes = <String>['application/json'];
@@ -434,7 +419,6 @@ class UserApi {
headerParams,
formParams,
contentTypes.isEmpty ? null : contentTypes.first,
authNames,
);
}

View File

@@ -11,14 +11,7 @@
part of openapi.api;
class ApiClient {
ApiClient({this.basePath = 'http://petstore.swagger.io:80/v2'}) {
// Setup authentications (key: authentication name, value: authentication).
_authentications[r'api_key'] = ApiKeyAuth('header', 'api_key');
_authentications[r'api_key_query'] = ApiKeyAuth('query', 'api_key_query');
_authentications[r'bearer_test'] = HttpBearerAuth();
_authentications[r'http_basic_test'] = HttpBasicAuth();
_authentications[r'petstore_auth'] = OAuth();
}
ApiClient({this.basePath = 'http://petstore.swagger.io:80/v2', this.authentication});
final String basePath;
@@ -35,7 +28,7 @@ class ApiClient {
}
final _defaultHeaderMap = <String, String>{};
final _authentications = <String, Authentication>{};
final Authentication? authentication;
void addDefaultHeader(String key, String value) {
_defaultHeaderMap[key] = value;
@@ -43,15 +36,6 @@ class ApiClient {
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(
@@ -62,9 +46,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) {
@@ -183,18 +166,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);
}
}