I created client libraries in a flutter project using the DioNext generator from openapi-generator-dart.
The api I am trying to communicate with uses an apikey as the authentication method and it uses it in the url, i.e when I am accessing the api through my Google Chrome browser:
https://api.hotelsexample.com/hotelfinder/v1/countries/en_US/hotels/A000293?apikey=9dhsu8d8-an9c-d40e1-a11c-5s84ddewda5
Now the method generated to get, i.e. a hotel, by DioNext looks like the following, and there are many endpoints like this, so I do not want to change the generated code:
Future<Response<Hotel>> hotelGET({
required String countryId,
required String hotelId,
BuiltList<String>? fieldsFilter,
CancelToken? cancelToken,
Map<String, dynamic>? headers,
Map<String, dynamic>? extra,
ValidateStatus? validateStatus,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
final _path = r'/countries/{countryId}/hotels/{hotelId}'.replaceAll('{' r'countryId' '}', countryId.toString()).replaceAll('{' r'hotelId' '}', hotelId.toString());
final _options = Options(
method: r'GET',
headers: <String, dynamic>{
...?headers,
},
extra: <String, dynamic>{
'secure': <Map<String, String>>[
{
'type': 'apiKey',
'name': 'apikey',
'keyName': 'apikey',
'where': 'query',
},
],
...?extra,
},
validateStatus: validateStatus,
);
final _queryParameters = <String, dynamic>{
if (fieldsFilter != null) r'fieldsFilter': encodeCollectionQueryParameter<String>(_serializers, fieldsFilter, const FullType(BuiltList, [FullType(String)]), format: ListFormat.multi,),
};
final _response = await _dio.request<Object>(
_path,
options: _options,
queryParameters: _queryParameters,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
Hotel _responseData;
try {
const _responseType = FullType(Hotel);
_responseData = _serializers.deserialize(
_response.data!,
specifiedType: _responseType,
) as Hotel;
} catch (error, stackTrace) {
throw DioError(
requestOptions: _response.requestOptions,
response: _response,
type: DioErrorType.other,
error: error,
)..stackTrace = stackTrace;
}
return Response<Hotel>(
data: _responseData,
headers: _response.headers,
isRedirect: _response.isRedirect,
requestOptions: _response.requestOptions,
redirects: _response.redirects,
statusCode: _response.statusCode,
statusMessage: _response.statusMessage,
extra: _response.extra,
);
}
Now, when I try to call the api using the following method:
void getmyhotel() async {
final api = Hotelsexample().getReferencesApi();
final countryId = "en_US";
final hotelId = "A000293";
try {
final response = await api.hotelGET(
countryId: countryId,
hotelId: hotelId,
headers: {"apikey": "9dhsu8d8-an9c-d40e1-a11c-5s84ddewda5"});
print(answer);
} catch (e) {
print(
"Exception: $e\n");
}
}
I get the error of an invalid apikey, so the apikey is not recognized by the api server. Where do I have to place the apikey in my code?
Thank you very much!
CodePudding user response:
You have to pass it as query instead of header.
CodePudding user response:
All right, I solved the problem. This time I downloaded the latest version of https://github.com/OpenAPITools/openapi-generator. It is a .jar file. I used the instructions on the github page to generate the files out of the .jar file and now I have client libraries, which are upgraded to null safety and do not need the use of dio. With the code
defaultApiClient.getAuthentication<ApiKeyAuth>("apikey")!.apiKey =
"9dhsu8d8-an9c-d40e1-a11c-5s84ddewda5";
I can now pass the apikey!
