Skip to content

Commit b015ac9

Browse files
authored
use 4-space instead of tab in perl templates (#1830)
1 parent bdf3277 commit b015ac9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1965
-1971
lines changed

modules/openapi-generator/src/main/resources/perl/ApiClient.mustache

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use Module::Runtime qw(use_module);
2626

2727
use {{moduleName}}::Configuration;
2828

29-
3029
sub new {
3130
my $class = shift;
3231
@@ -41,7 +40,7 @@ sub new {
4140
'ua' => LWP::UserAgent->new,
4241
'config' => $config,
4342
);
44-
43+
4544
return bless \%args, $class;
4645
}
4746

@@ -76,23 +75,21 @@ sub set_timeout {
7675
sub call_api {
7776
my $self = shift;
7877
my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_;
79-
78+
8079
# update parameters based on authentication settings
8180
$self->update_params_for_auth($header_params, $query_params, $auth_settings);
82-
83-
81+
8482
my $_url = $self->{config}{base_url} . $resource_path;
85-
83+
8684
# build query
8785
if (%$query_params) {
8886
$_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
8987
}
90-
91-
88+
9289
# body data
9390
$body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
9491
my $_body_data = %$post_params ? $post_params : $body_data;
95-
92+
9693
# Make the HTTP request
9794
my $_request;
9895
if ($method eq 'POST') {
@@ -101,15 +98,15 @@ sub call_api {
10198
'form-data' : $header_params->{'Content-Type'};
10299

103100
$_request = POST($_url, %$header_params, Content => $_body_data);
104-
101+
105102
}
106103
elsif ($method eq 'PUT') {
107104
# multipart
108105
$header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
109106
'form-data' : $header_params->{'Content-Type'};
110-
107+
111108
$_request = PUT($_url, %$header_params, Content => $_body_data);
112-
109+
113110
}
114111
elsif ($method eq 'GET') {
115112
my $headers = HTTP::Headers->new(%$header_params);
@@ -130,17 +127,17 @@ sub call_api {
130127

131128
$self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
132129
$self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});
133-
130+
134131
$log->debugf("REQUEST: %s", $_request->as_string);
135132
my $_response = $self->{ua}->request($_request);
136133
$log->debugf("RESPONSE: %s", $_response->as_string);
137-
134+
138135
unless ($_response->is_success) {
139136
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
140137
}
141138

142139
return $_response->content;
143-
140+
144141
}
145142

146143
# Take value and turn it into a string suitable for inclusion in
@@ -160,12 +157,12 @@ sub to_path_value {
160157
# @param object $object an object to be serialized to a string
161158
# @return string the serialized object
162159
sub to_query_value {
163-
my ($self, $object) = @_;
164-
if (ref($object) eq 'ARRAY') {
165-
return join(',', @$object);
166-
} else {
167-
return $self->to_string($object);
168-
}
160+
my ($self, $object) = @_;
161+
if (ref($object) eq 'ARRAY') {
162+
return join(',', @$object);
163+
} else {
164+
return $self->to_string($object);
165+
}
169166
}
170167

171168

@@ -213,7 +210,7 @@ sub deserialize
213210
{
214211
my ($self, $class, $data) = @_;
215212
$log->debugf("deserializing %s for %s", $data, $class);
216-
213+
217214
if (not defined $data) {
218215
return undef;
219216
} elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash
@@ -232,10 +229,9 @@ sub deserialize
232229
} else {
233230
#TODO log error
234231
}
235-
236232
} elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data
237233
return $data if $data eq '[]'; # return if empty array
238-
234+
239235
my $_sub_class = substr($class, 6, -1);
240236
my $_json_data = decode_json $data;
241237
my @_values = ();
@@ -259,7 +255,6 @@ sub deserialize
259255
return $_instance->from_hash(decode_json $data);
260256
}
261257
}
262-
263258
}
264259

265260
# return 'Accept' based on an array of accept provided
@@ -268,15 +263,15 @@ sub deserialize
268263
sub select_header_accept
269264
{
270265
my ($self, @header) = @_;
271-
266+
272267
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
273268
return undef;
274269
} elsif (grep(/^application\/json$/i, @header)) {
275270
return 'application/json';
276271
} else {
277272
return join(',', @header);
278273
}
279-
274+
280275
}
281276

282277
# return the content type based on an array of content-type provided
@@ -285,31 +280,31 @@ sub select_header_accept
285280
sub select_header_content_type
286281
{
287282
my ($self, @header) = @_;
288-
283+
289284
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
290285
return 'application/json'; # default to application/json
291286
} elsif (grep(/^application\/json$/i, @header)) {
292287
return 'application/json';
293288
} else {
294289
return join(',', @header);
295290
}
296-
291+
297292
}
298293

299294
# Get API key (with prefix if set)
300295
# @param string key name
301296
# @return string API key with the prefix
302297
sub get_api_key_with_prefix
303298
{
304-
my ($self, $key_name) = @_;
299+
my ($self, $key_name) = @_;
305300
306-
my $api_key = $self->{config}{api_key}{$key_name};
307-
308-
return unless $api_key;
309-
310-
my $prefix = $self->{config}{api_key_prefix}{$key_name};
311-
return $prefix ? "$prefix $api_key" : $api_key;
312-
}
301+
my $api_key = $self->{config}{api_key}{$key_name};
302+
303+
return unless $api_key;
304+
305+
my $prefix = $self->{config}{api_key_prefix}{$key_name};
306+
return $prefix ? "$prefix $api_key" : $api_key;
307+
}
313308

314309
# update header and query param based on authentication setting
315310
#
@@ -318,36 +313,46 @@ sub get_api_key_with_prefix
318313
# @param array $authSettings array of authentication scheme (e.g ['api_key'])
319314
sub update_params_for_auth {
320315
my ($self, $header_params, $query_params, $auth_settings) = @_;
321-
316+
322317
return $self->_global_auth_setup($header_params, $query_params)
323-
unless $auth_settings && @$auth_settings;
324-
318+
unless $auth_settings && @$auth_settings;
319+
325320
# one endpoint can have more than 1 auth settings
326321
foreach my $auth (@$auth_settings) {
327322
# determine which one to use
328323
if (!defined($auth)) {
329324
# TODO show warning about auth setting not defined
330325
}
331-
{{#authMethods}}elsif ($auth eq '{{name}}') {
332-
{{#isApiKey}}{{#isKeyInHeader}}
326+
{{#authMethods}}
327+
elsif ($auth eq '{{name}}') {
328+
{{#isApiKey}}
329+
{{#isKeyInHeader}}
333330
my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}');
334331
if ($api_key) {
335332
$header_params->{'{{keyParamName}}'} = $api_key;
336-
}{{/isKeyInHeader}}{{#isKeyInQuery}}
333+
}
334+
{{/isKeyInHeader}}
335+
{{#isKeyInQuery}}
337336
my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}');
338337
if ($api_key) {
339338
$query_params->{'{{keyParamName}}'} = $api_key;
340-
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
339+
}
340+
{{/isKeyInQuery}}
341+
{{/isApiKey}}
342+
{{#isBasic}}
341343
if ($self->{config}{username} || $self->{config}{password}) {
342344
$header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
343-
}{{/isBasic}}{{#isOAuth}}
345+
}
346+
{{/isBasic}}
347+
{{#isOAuth}}
344348
if ($self->{config}{access_token}) {
345349
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
346-
}{{/isOAuth}}
350+
}
351+
{{/isOAuth}}
347352
}
348353
{{/authMethods}}
349354
else {
350-
# TODO show warning about security definition not found
355+
# TODO show warning about security definition not found
351356
}
352357
}
353358
}
@@ -357,37 +362,36 @@ sub update_params_for_auth {
357362
# OpenAPI Spec does not describe the intended authorization. So we check in the config for any
358363
# auth tokens and if we find any, we use them for all endpoints;
359364
sub _global_auth_setup {
360-
my ($self, $header_params, $query_params) = @_;
361-
362-
my $tokens = $self->{config}->get_tokens;
363-
return unless keys %$tokens;
364-
365-
# basic
366-
if (my $uname = delete $tokens->{username}) {
367-
my $pword = delete $tokens->{password};
368-
$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
369-
}
370-
371-
# oauth
372-
if (my $access_token = delete $tokens->{access_token}) {
373-
$header_params->{'Authorization'} = 'Bearer ' . $access_token;
374-
}
375-
376-
# other keys
377-
foreach my $token_name (keys %$tokens) {
378-
my $in = $tokens->{$token_name}->{in};
379-
my $token = $self->get_api_key_with_prefix($token_name);
380-
if ($in eq 'head') {
381-
$header_params->{$token_name} = $token;
382-
}
383-
elsif ($in eq 'query') {
384-
$query_params->{$token_name} = $token;
385-
}
386-
else {
387-
die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
388-
}
389-
}
390-
}
365+
my ($self, $header_params, $query_params) = @_;
366+
367+
my $tokens = $self->{config}->get_tokens;
368+
return unless keys %$tokens;
391369

370+
# basic
371+
if (my $uname = delete $tokens->{username}) {
372+
my $pword = delete $tokens->{password};
373+
$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
374+
}
375+
376+
# oauth
377+
if (my $access_token = delete $tokens->{access_token}) {
378+
$header_params->{'Authorization'} = 'Bearer ' . $access_token;
379+
}
380+
381+
# other keys
382+
foreach my $token_name (keys %$tokens) {
383+
my $in = $tokens->{$token_name}->{in};
384+
my $token = $self->get_api_key_with_prefix($token_name);
385+
if ($in eq 'head') {
386+
$header_params->{$token_name} = $token;
387+
}
388+
elsif ($in eq 'query') {
389+
$query_params->{$token_name} = $token;
390+
}
391+
else {
392+
die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
393+
}
394+
}
395+
}
392396

393397
1;

0 commit comments

Comments
 (0)