Skip to content

Commit

Permalink
use 4-space instead of tab in perl templates (OpenAPITools#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Jan 6, 2019
1 parent aae65d6 commit 5b1907d
Show file tree
Hide file tree
Showing 62 changed files with 1,965 additions and 1,971 deletions.
160 changes: 82 additions & 78 deletions modules/openapi-generator/src/main/resources/perl/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use Module::Runtime qw(use_module);

use {{moduleName}}::Configuration;


sub new {
my $class = shift;
Expand All @@ -41,7 +40,7 @@ sub new {
'ua' => LWP::UserAgent->new,
'config' => $config,
);

return bless \%args, $class;
}

Expand Down Expand Up @@ -76,23 +75,21 @@ sub set_timeout {
sub call_api {
my $self = shift;
my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_;
# update parameters based on authentication settings
$self->update_params_for_auth($header_params, $query_params, $auth_settings);
my $_url = $self->{config}{base_url} . $resource_path;

# build query
if (%$query_params) {
$_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
}



# body data
$body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
my $_body_data = %$post_params ? $post_params : $body_data;

# Make the HTTP request
my $_request;
if ($method eq 'POST') {
Expand All @@ -101,15 +98,15 @@ sub call_api {
'form-data' : $header_params->{'Content-Type'};

$_request = POST($_url, %$header_params, Content => $_body_data);

}
elsif ($method eq 'PUT') {
# multipart
$header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
'form-data' : $header_params->{'Content-Type'};

$_request = PUT($_url, %$header_params, Content => $_body_data);

}
elsif ($method eq 'GET') {
my $headers = HTTP::Headers->new(%$header_params);
Expand All @@ -130,17 +127,17 @@ sub call_api {

$self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
$self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});

$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
$log->debugf("RESPONSE: %s", $_response->as_string);

unless ($_response->is_success) {
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
}

return $_response->content;

}

# Take value and turn it into a string suitable for inclusion in
Expand All @@ -160,12 +157,12 @@ sub to_path_value {
# @param object $object an object to be serialized to a string
# @return string the serialized object
sub to_query_value {
my ($self, $object) = @_;
if (ref($object) eq 'ARRAY') {
return join(',', @$object);
} else {
return $self->to_string($object);
}
my ($self, $object) = @_;
if (ref($object) eq 'ARRAY') {
return join(',', @$object);
} else {
return $self->to_string($object);
}
}


Expand Down Expand Up @@ -213,7 +210,7 @@ sub deserialize
{
my ($self, $class, $data) = @_;
$log->debugf("deserializing %s for %s", $data, $class);
if (not defined $data) {
return undef;
} elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash
Expand All @@ -232,10 +229,9 @@ sub deserialize
} else {
#TODO log error
}

} elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data
return $data if $data eq '[]'; # return if empty array
my $_sub_class = substr($class, 6, -1);
my $_json_data = decode_json $data;
my @_values = ();
Expand All @@ -259,7 +255,6 @@ sub deserialize
return $_instance->from_hash(decode_json $data);
}
}

}

# return 'Accept' based on an array of accept provided
Expand All @@ -268,15 +263,15 @@ sub deserialize
sub select_header_accept
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return undef;
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}

}

# return the content type based on an array of content-type provided
Expand All @@ -285,31 +280,31 @@ sub select_header_accept
sub select_header_content_type
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return 'application/json'; # default to application/json
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}

}

# Get API key (with prefix if set)
# @param string key name
# @return string API key with the prefix
sub get_api_key_with_prefix
{
my ($self, $key_name) = @_;
my ($self, $key_name) = @_;
my $api_key = $self->{config}{api_key}{$key_name};
return unless $api_key;
my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key;
}
my $api_key = $self->{config}{api_key}{$key_name};

return unless $api_key;

my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key;
}

# update header and query param based on authentication setting
#
Expand All @@ -318,36 +313,46 @@ sub get_api_key_with_prefix
# @param array $authSettings array of authentication scheme (e.g ['api_key'])
sub update_params_for_auth {
my ($self, $header_params, $query_params, $auth_settings) = @_;
return $self->_global_auth_setup($header_params, $query_params)
unless $auth_settings && @$auth_settings;
unless $auth_settings && @$auth_settings;
# one endpoint can have more than 1 auth settings
foreach my $auth (@$auth_settings) {
# determine which one to use
if (!defined($auth)) {
# TODO show warning about auth setting not defined
}
{{#authMethods}}elsif ($auth eq '{{name}}') {
{{#isApiKey}}{{#isKeyInHeader}}
{{#authMethods}}
elsif ($auth eq '{{name}}') {
{{#isApiKey}}
{{#isKeyInHeader}}
my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}');
if ($api_key) {
$header_params->{'{{keyParamName}}'} = $api_key;
}{{/isKeyInHeader}}{{#isKeyInQuery}}
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
my $api_key = $self->get_api_key_with_prefix('{{keyParamName}}');
if ($api_key) {
$query_params->{'{{keyParamName}}'} = $api_key;
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
if ($self->{config}{username} || $self->{config}{password}) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
}{{/isBasic}}{{#isOAuth}}
}
{{/isBasic}}
{{#isOAuth}}
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}{{/isOAuth}}
}
{{/isOAuth}}
}
{{/authMethods}}
else {
# TODO show warning about security definition not found
# TODO show warning about security definition not found
}
}
}
Expand All @@ -357,37 +362,36 @@ sub update_params_for_auth {
# OpenAPI Spec does not describe the intended authorization. So we check in the config for any
# auth tokens and if we find any, we use them for all endpoints;
sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_;
my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens;

# basic
if (my $uname = delete $tokens->{username}) {
my $pword = delete $tokens->{password};
$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
}

# oauth
if (my $access_token = delete $tokens->{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $access_token;
}

# other keys
foreach my $token_name (keys %$tokens) {
my $in = $tokens->{$token_name}->{in};
my $token = $self->get_api_key_with_prefix($token_name);
if ($in eq 'head') {
$header_params->{$token_name} = $token;
}
elsif ($in eq 'query') {
$query_params->{$token_name} = $token;
}
else {
die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
}
}
}
my ($self, $header_params, $query_params) = @_;
my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens;

# basic
if (my $uname = delete $tokens->{username}) {
my $pword = delete $tokens->{password};
$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
}

# oauth
if (my $access_token = delete $tokens->{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $access_token;
}

# other keys
foreach my $token_name (keys %$tokens) {
my $in = $tokens->{$token_name}->{in};
my $token = $self->get_api_key_with_prefix($token_name);
if ($in eq 'head') {
$header_params->{$token_name} = $token;
}
elsif ($in eq 'query') {
$query_params->{$token_name} = $token;
}
else {
die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
}
}
}

1;
Loading

0 comments on commit 5b1907d

Please sign in to comment.