diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PerlClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PerlClientCodegen.java index b25e4490e85..b96b012abba 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PerlClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PerlClientCodegen.java @@ -27,8 +27,8 @@ public String getHelp() { public PerlClientCodegen() { super(); - modelPackage = "Object"; - outputFolder = "generated-code/perl"; + modelPackage = File.separatorChar + "Object"; + outputFolder = "generated-code" + File.separatorChar + "perl"; modelTemplateFiles.put("object.mustache", ".pm"); apiTemplateFiles.put("api.mustache", ".pm"); templateDir = "perl"; @@ -63,6 +63,7 @@ public PerlClientCodegen() { languageSpecificPrimitives.add("DateTime"); languageSpecificPrimitives.add("ARRAY"); languageSpecificPrimitives.add("HASH"); + languageSpecificPrimitives.add("object"); typeMapping.put("integer", "int"); typeMapping.put("long", "int"); @@ -75,9 +76,11 @@ public PerlClientCodegen() { typeMapping.put("password", "string"); typeMapping.put("array", "ARRAY"); typeMapping.put("map", "HASH"); + typeMapping.put("object", "object"); - supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm")); - supportingFiles.add(new SupportingFile("BaseObject.mustache", "lib/WWW/" + invokerPackage, "Object/BaseObject.pm")); + supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "ApiClient.pm")); + supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "Configuration.pm")); + supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "Object/BaseObject.pm")); } @Override @@ -87,11 +90,11 @@ public String escapeReservedWord(String name) { @Override public String apiFileFolder() { - return outputFolder + "/lib/WWW/" + invokerPackage + apiPackage().replace('.', File.separatorChar); + return (outputFolder + "/lib/WWW/" + invokerPackage + apiPackage()).replace('/', File.separatorChar); } public String modelFileFolder() { - return outputFolder + "/lib/WWW/" + invokerPackage + "/" + modelPackage().replace('.', File.separatorChar); + return (outputFolder + "/lib/WWW/" + invokerPackage + modelPackage()).replace('/', File.separatorChar); } @Override diff --git a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache new file mode 100644 index 00000000000..38eb2ad8dae --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -0,0 +1,22 @@ +package WWW::{{invokerPackage}}::Configuration; + +use strict; +use warnings; +use utf8; + +use Log::Any qw($log); +use Carp; + +# class/static variables +our $api_client; +our $http_timeout = 180; +our $http_user_agent = 'Perl-Swagger'; + +# authenticaiton setting +our $api_key = {}; +our $api_key_prefix = {}; +our $username; +our $password; + + +1; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 131eb3a3934..5adb50344de 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -27,14 +27,10 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; +use WWW::{{invokerPackage}}::ApiClient; +use WWW::{{invokerPackage}}::Configuration; {{#operations}} - -use WWW::{{invokerPackage}}::APIClient; - our @EXPORT_OK = qw( {{#operation}}{{{nickname}}} {{/operation}} @@ -42,7 +38,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = WWW::{{invokerPackage}}::APIClient->new; + my $default_api_client = $WWW::{{invokerPackage}}::Configuration::api_client ? $WWW::{{invokerPackage}}::Configuration::api_client : WWW::{{invokerPackage}}::ApiClient->new; my (%self) = ( 'api_client' => $default_api_client, @_ @@ -92,49 +88,49 @@ sub new { } $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type({{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}); - {{#queryParams}} # query params + {{#queryParams}}# query params if ( exists $args{'{{paramName}}'}) { - $query_params->{'{{baseName}}'} = WWW::{{invokerPacakge}}::APIClient::to_query_value($args{'{{paramName}}'}); + $query_params->{'{{baseName}}'} = $self->{api_client}->to_query_value($args{'{{paramName}}'}); }{{/queryParams}} - {{#headerParams}} # header params + {{#headerParams}}# header params if ( exists $args{'{{paramName}}'}) { - $header_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_header_value($args{'{{paramName}}'}); + $header_params->{'{{baseName}}'} = $self->{api_client}->to_header_value($args{'{{paramName}}'}); }{{/headerParams}} - {{#pathParams}} # path params + {{#pathParams}}# path params if ( exists $args{'{{paramName}}'}) { my $_base_variable = "{" . "{{baseName}}" . "}"; - my $_base_value = WWW::{{invokerPackage}}::APIClient::to_path_value($args{'{{paramName}}'}); + my $_base_value = $self->{api_client}->to_path_value($args{'{{paramName}}'}); $_resource_path =~ s/$_base_variable/$_base_value/g; }{{/pathParams}} - {{#formParams}} # form params + {{#formParams}}# form params if ( exists $args{'{{paramName}}'} ) { {{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'}; push $form_params->{'{{baseName}}'}, $args{'{{paramName}}'}; {{/isFile}} - {{^isFile}}$form_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_form_value($args{'{{paramName}}'}); + {{^isFile}}$form_params->{'{{baseName}}'} = $self->{api_client}->to_form_value($args{'{{paramName}}'}); {{/isFile}} }{{/formParams}} my $_body_data; - {{#bodyParams}} # body params + {{#bodyParams}}# body params if ( exists $args{'{{paramName}}'}) { $_body_data = $args{'{{paramName}}'}; }{{/bodyParams}} - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]; # make the API Call {{#returnType}}my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('{{returnType}}', $response); - return $_response_object;{{/returnType}} + my $_response_object = $self->{api_client}->deserialize('{{returnType}}', $response); + return $_response_object;{{/returnType}} {{^returnType}}$self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; {{/returnType}} } diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perlApiClient.mustache similarity index 58% rename from modules/swagger-codegen/src/main/resources/perl/APIClient.mustache rename to modules/swagger-codegen/src/main/resources/perlApiClient.mustache index 19079a07922..2bcdd6da690 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perlApiClient.mustache @@ -1,9 +1,10 @@ -package WWW::{{invokerPackage}}::APIClient; +package WWW::{{invokerPackage}}::ApiClient; use strict; use warnings; use utf8; +use MIME::Base64; use LWP::UserAgent; use HTTP::Headers; use HTTP::Response; @@ -15,20 +16,18 @@ use URI::Escape; use Scalar::Util; use Log::Any qw($log); use Carp; -use Switch; use Module::Runtime qw(use_module); -# class variables -my $ua = LWP::UserAgent->new; -my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent -my $http_timeout; #timeout -my $base_url = "{{basePath}}"; - +use WWW::{{invokerPackage}}::Configuration; sub new { my $class = shift; - my %args = @_; + my (%args) = ( + 'ua' => LWP::UserAgent->new, + 'base_url' => '{{basePath}}', + @_ + ); return bless \%args, $class; } @@ -38,8 +37,8 @@ sub new # @param string $user_agent The user agent of the API client # sub set_user_agent { - my $user_agent = shift; - $http_user_agent= $user_agent; + my ($self, $user_agent) = @_; + $self->{http_user_agent}= $user_agent; } # Set timeout @@ -47,11 +46,11 @@ sub set_user_agent { # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] # sub set_timeout { - my $seconds = shift; + my ($self, $seconds) = @_; if (!looks_like_number($seconds)) { croak('Timeout variable must be numeric.'); } - $http_timeout = $seconds; + $self->{http_timeout} = $seconds; } # make the HTTP request @@ -63,60 +62,63 @@ sub set_timeout { # @return mixed sub call_api { my $self = shift; - my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data) = @_; + 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 $headers = HTTP::Headers->new(%$header_params); - my $_url = $base_url . $resource_path; + my $_url = $self->{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; - switch ($method) { - case 'POST' { + if ($method eq 'POST') { # multipart - my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? + $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; + + $_request = POST($_url, %$header_params, Content => $_body_data); - $_request = POST($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); - } - case 'PUT' { + } + elsif ($method eq 'PUT') { # multipart - my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? + $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; - $_request = PUT($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); - } - case 'GET' { - $_request = GET($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - case 'HEAD' { - $_request = HEAD($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - case 'DELETE' { #TODO support form data - $_request = DELETE($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - } - case 'PATCH' { #TODO - } + $_request = PUT($_url, %$header_params, Content => $_body_data); + + } + elsif ($method eq 'GET') { + my $headers = HTTP::Headers->new(%$header_params); + $_request = GET($_url, %$header_params); + } + elsif ($method eq 'HEAD') { + my $headers = HTTP::Headers->new(%$header_params); + $_request = HEAD($_url,%$header_params); + } + elsif ($method eq 'DELETE') { #TODO support form data + my $headers = HTTP::Headers->new(%$header_params); + $_request = DELETE($_url, %$headers); + } + elsif ($method eq 'PATCH') { #TODO + } + else { } - $ua->timeout($http_timeout); - $ua->agent($http_user_agent); + $self->{ua}->timeout($self->{http_timeout} || $WWW::{{invokerPackage}}::Configuration::http_timeout); + $self->{ua}->agent($self->{http_user_agent} || $WWW::{{invokerPackage}}::Configuration::http_user_agent); - my $_response = $ua->request($_request); + my $_response = $self->{ua}->request($_request); unless ($_response->is_success) { croak("API Exception(".$_response->code."): ".$_response->message); @@ -126,41 +128,13 @@ sub call_api { } - -# Build a JSON POST object -sub sanitize_for_serialization -{ -# my $data = shift; -# if (is_scalar($data) || null === $data) { -# $sanitized = $data; -# } else if ($data instanceof \DateTime) { -# $sanitized = $data->format(\DateTime::ISO8601); -# } else if (is_array($data)) { -# foreach ($data as $property => $value) { -# $data[$property] = $this->sanitizeForSerialization($value); -# } -# $sanitized = $data; -# } else if (is_object($data)) { -# $values = array(); -# foreach (array_keys($data::$swaggerTypes) as $property) { -# $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); -# } -# $sanitized = $values; -# } else { -# $sanitized = (string)$data; -# } -# -# return $sanitized; -} - - # Take value and turn it into a string suitable for inclusion in # the path, by url-encoding. # @param string $value a string which will be part of the path # @return string the serialized object sub to_path_value { - my $value = shift; - return uri_escape(to_string($value)); + my ($self, $value) = @_; + return uri_escape($self->to_string($value)); } @@ -171,11 +145,11 @@ 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 $object = shift; + my ($self, $object) = @_; if (is_array($object)) { return implode(',', $object); } else { - return toString($object); + return $self->to_string($object); } } @@ -186,8 +160,8 @@ sub to_query_value { # @param string $value a string which will be part of the header # @return string the header string sub to_header_value { - my $value = shift; - return to_string($value); + my ($self, $value) = @_; + return $self->to_string($value); } # Take value and turn it into a string suitable for inclusion in @@ -196,8 +170,8 @@ sub to_header_value { # @param string $value the value of the form parameter # @return string the form string sub to_form_value { - my $value = shift; - return to_string($value); + my ($self, $value) = @_; + return $self->to_string($value); } # Take value and turn it into a string suitable for inclusion in @@ -206,7 +180,7 @@ sub to_form_value { # @param string $value the value of the parameter # @return string the header string sub to_string { - my $value = shift; + my ($self, $value) = @_; if (ref($value) eq "DateTime") { # datetime in ISO8601 format return $value->datetime(); } @@ -242,7 +216,7 @@ sub deserialize $_result = \@_values; } elsif ($class eq 'DateTime') { $_result = DateTime->from_epoch(epoch => str2time($data)); - } elsif (grep /^$data$/, ('string', 'int', 'float', 'bool')) { #TODO revise the primitive type + } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { #TODO revise the primitive type $_result= $data; } else { # model my $_instance = use_module("WWW::{{invokerPackage}}::Object::$class")->new; @@ -287,5 +261,44 @@ sub select_header_content_type } +# 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, $api_key) = @_; + if ($WWW::{{invokerPackage}}::Configuration::api_key_prefix->{$api_key}) { + return $WWW::{{invokerPackage}}::Configuration::api_key_prefix->{$api_key}." ".$WWW::{{invokerPackage}}::Configuration::api_key->{$api_key}; + } else { + return $WWW::{{invokerPackage}}::Configuration::api_key->{$api_key}; + } +} + +# update hearder and query param based on authentication setting +# +# @param array $headerParams header parameters (by ref) +# @param array $queryParams query parameters (by ref) +# @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 if (!defined($auth_settings) || scalar(@$auth_settings) == 0); + + # one endpoint can have more than 1 auth settings + foreach my $auth (@$auth_settings) { + # determine which one to use + if (!defined($auth)) { + } + {{#authMethods}}elsif ($auth eq '{{name}}') { + {{#isApiKey}}{{#isKeyInHeader}}$header_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$query_params->{'{{keyParamName}}'} = $self->get_api_key_with_prefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$header_params->{'Authorization'} = 'Basic '.encode_base64($WWW::{{invokerPackage}}::Configuration::username.":".$WWW::{{invokerPackage}}::Configuration::password);{{/isBasic}} + {{#isOAuth}}# TODO support oauth{{/isOAuth}} + } + {{/authMethods}} + else { + # TODO show warning about security definition not found + } + } +} + 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 0f0fea793e3..da29b3e246a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -1,9 +1,10 @@ -package WWW::SwaggerClient::APIClient; +package WWW::SwaggerClient::ApiClient; use strict; use warnings; use utf8; +use MIME::Base64; use LWP::UserAgent; use HTTP::Headers; use HTTP::Response; @@ -15,20 +16,18 @@ use URI::Escape; use Scalar::Util; use Log::Any qw($log); use Carp; -use Switch; use Module::Runtime qw(use_module); -# class variables -my $ua = LWP::UserAgent->new; -my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent -my $http_timeout; #timeout -my $base_url = "http://petstore.swagger.io/v2"; - +use WWW::SwaggerClient::Configuration; sub new { my $class = shift; - my %args = @_; + my (%args) = ( + 'ua' => LWP::UserAgent->new, + 'base_url' => 'http://petstore.swagger.io/v2', + @_ + ); return bless \%args, $class; } @@ -38,8 +37,8 @@ sub new # @param string $user_agent The user agent of the API client # sub set_user_agent { - my $user_agent = shift; - $http_user_agent= $user_agent; + my ($self, $user_agent) = @_; + $self->{http_user_agent}= $user_agent; } # Set timeout @@ -47,11 +46,11 @@ sub set_user_agent { # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] # sub set_timeout { - my $seconds = shift; + my ($self, $seconds) = @_; if (!looks_like_number($seconds)) { croak('Timeout variable must be numeric.'); } - $http_timeout = $seconds; + $self->{http_timeout} = $seconds; } # make the HTTP request @@ -63,60 +62,63 @@ sub set_timeout { # @return mixed sub call_api { my $self = shift; - my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data) = @_; + 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 $headers = HTTP::Headers->new(%$header_params); - my $_url = $base_url . $resource_path; + my $_url = $self->{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; - switch ($method) { - case 'POST' { + if ($method eq 'POST') { # multipart - my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? + $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; + + $_request = POST($_url, %$header_params, Content => $_body_data); - $_request = POST($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); - } - case 'PUT' { + } + elsif ($method eq 'PUT') { # multipart - my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? + $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; - $_request = PUT($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); - } - case 'GET' { - $_request = GET($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - case 'HEAD' { - $_request = HEAD($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - case 'DELETE' { #TODO support form data - $_request = DELETE($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - } - case 'PATCH' { #TODO - } + $_request = PUT($_url, %$header_params, Content => $_body_data); + + } + elsif ($method eq 'GET') { + my $headers = HTTP::Headers->new(%$header_params); + $_request = GET($_url, %$header_params); + } + elsif ($method eq 'HEAD') { + my $headers = HTTP::Headers->new(%$header_params); + $_request = HEAD($_url,%$header_params); + } + elsif ($method eq 'DELETE') { #TODO support form data + my $headers = HTTP::Headers->new(%$header_params); + $_request = DELETE($_url, %$headers); + } + elsif ($method eq 'PATCH') { #TODO + } + else { } - $ua->timeout($http_timeout); - $ua->agent($http_user_agent); + $self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout); + $self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent); - my $_response = $ua->request($_request); + my $_response = $self->{ua}->request($_request); unless ($_response->is_success) { croak("API Exception(".$_response->code."): ".$_response->message); @@ -126,41 +128,13 @@ sub call_api { } - -# Build a JSON POST object -sub sanitize_for_serialization -{ -# my $data = shift; -# if (is_scalar($data) || null === $data) { -# $sanitized = $data; -# } else if ($data instanceof \DateTime) { -# $sanitized = $data->format(\DateTime::ISO8601); -# } else if (is_array($data)) { -# foreach ($data as $property => $value) { -# $data[$property] = $this->sanitizeForSerialization($value); -# } -# $sanitized = $data; -# } else if (is_object($data)) { -# $values = array(); -# foreach (array_keys($data::$swaggerTypes) as $property) { -# $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); -# } -# $sanitized = $values; -# } else { -# $sanitized = (string)$data; -# } -# -# return $sanitized; -} - - # Take value and turn it into a string suitable for inclusion in # the path, by url-encoding. # @param string $value a string which will be part of the path # @return string the serialized object sub to_path_value { - my $value = shift; - return uri_escape(to_string($value)); + my ($self, $value) = @_; + return uri_escape($self->to_string($value)); } @@ -171,11 +145,11 @@ 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 $object = shift; + my ($self, $object) = @_; if (is_array($object)) { return implode(',', $object); } else { - return toString($object); + return $self->to_string($object); } } @@ -186,8 +160,8 @@ sub to_query_value { # @param string $value a string which will be part of the header # @return string the header string sub to_header_value { - my $value = shift; - return to_string($value); + my ($self, $value) = @_; + return $self->to_string($value); } # Take value and turn it into a string suitable for inclusion in @@ -196,8 +170,8 @@ sub to_header_value { # @param string $value the value of the form parameter # @return string the form string sub to_form_value { - my $value = shift; - return to_string($value); + my ($self, $value) = @_; + return $self->to_string($value); } # Take value and turn it into a string suitable for inclusion in @@ -206,7 +180,7 @@ sub to_form_value { # @param string $value the value of the parameter # @return string the header string sub to_string { - my $value = shift; + my ($self, $value) = @_; if (ref($value) eq "DateTime") { # datetime in ISO8601 format return $value->datetime(); } @@ -242,7 +216,7 @@ sub deserialize $_result = \@_values; } elsif ($class eq 'DateTime') { $_result = DateTime->from_epoch(epoch => str2time($data)); - } elsif (grep /^$data$/, ('string', 'int', 'float', 'bool')) { #TODO revise the primitive type + } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { #TODO revise the primitive type $_result= $data; } else { # model my $_instance = use_module("WWW::SwaggerClient::Object::$class")->new; @@ -287,5 +261,48 @@ sub select_header_content_type } +# 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, $api_key) = @_; + if ($WWW::SwaggerClient::Configuration::api_key_prefix->{$api_key}) { + return $WWW::SwaggerClient::Configuration::api_key_prefix->{$api_key}." ".$WWW::SwaggerClient::Configuration::api_key->{$api_key}; + } else { + return $WWW::SwaggerClient::Configuration::api_key->{$api_key}; + } +} + +# update hearder and query param based on authentication setting +# +# @param array $headerParams header parameters (by ref) +# @param array $queryParams query parameters (by ref) +# @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 if (!defined($auth_settings) || scalar(@$auth_settings) == 0); + + # one endpoint can have more than 1 auth settings + foreach my $auth (@$auth_settings) { + # determine which one to use + if (!defined($auth)) { + } + elsif ($auth eq 'api_key') { + $header_params->{'api_key'} = $self->get_api_key_with_prefix('api_key'); + + } + elsif ($auth eq 'petstore_auth') { + + # TODO support oauth + } + + else { + # TODO show warning about security definition not found + } + } +} + 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm new file mode 100644 index 00000000000..bbce9d74759 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -0,0 +1,22 @@ +package WWW::SwaggerClient::Configuration; + +use strict; +use warnings; +use utf8; + +use Log::Any qw($log); +use Carp; + +# class/static variables +our $api_client; +our $http_timeout = 180; +our $http_user_agent = 'Perl-Swagger'; + +# authenticaiton setting +our $api_key = {}; +our $api_key_prefix = {}; +our $username; +our $password; + + +1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 57237a96974..44bc1567a3d 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -27,12 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - - -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( update_pet @@ -48,7 +44,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = WWW::SwaggerClient::APIClient->new; + my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( 'api_client' => $default_api_client, @_ @@ -91,26 +87,26 @@ sub new { if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', ); + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml'); my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -142,26 +138,26 @@ sub new { if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', ); + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml'); my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -195,9 +191,9 @@ sub new { } $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); - # query params + # query params if ( exists $args{'status'}) { - $query_params->{'status'} = WWW::::APIClient::to_query_value($args{'status'}); + $query_params->{'status'} = $self->{api_client}->to_query_value($args{'status'}); } @@ -205,18 +201,18 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('ARRAY[Pet]', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('ARRAY[Pet]', $response); + return $_response_object; } @@ -249,9 +245,9 @@ sub new { } $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); - # query params + # query params if ( exists $args{'tags'}) { - $query_params->{'tags'} = WWW::::APIClient::to_query_value($args{'tags'}); + $query_params->{'tags'} = $self->{api_client}->to_query_value($args{'tags'}); } @@ -259,18 +255,18 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('ARRAY[Pet]', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('ARRAY[Pet]', $response); + return $_response_object; } @@ -310,28 +306,28 @@ sub new { - # path params + # path params if ( exists $args{'pet_id'}) { my $_base_variable = "{" . "petId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['api_key', 'petstore_auth']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('Pet', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('Pet', $response); + return $_response_object; } @@ -369,38 +365,38 @@ sub new { if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded', ); + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded'); - # path params + # path params if ( exists $args{'pet_id'}) { my $_base_variable = "{" . "petId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } - # form params + # form params if ( exists $args{'name'} ) { - $form_params->{'name'} = WWW::SwaggerClient::APIClient::to_form_value($args{'name'}); + $form_params->{'name'} = $self->{api_client}->to_form_value($args{'name'}); - } # form params + }# form params if ( exists $args{'status'} ) { - $form_params->{'status'} = WWW::SwaggerClient::APIClient::to_form_value($args{'status'}); + $form_params->{'status'} = $self->{api_client}->to_form_value($args{'status'}); } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -441,28 +437,28 @@ sub new { $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); - # header params + # header params if ( exists $args{'api_key'}) { - $header_params->{'api_key'} = WWW::SwaggerClient::APIClient::to_header_value($args{'api_key'}); + $header_params->{'api_key'} = $self->{api_client}->to_header_value($args{'api_key'}); } - # path params + # path params if ( exists $args{'pet_id'}) { my $_base_variable = "{" . "petId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -501,22 +497,22 @@ sub new { if ($_header_accept) { $header_params->{'Accept'} = $_header_accept; } - $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data', ); + $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data'); - # path params + # path params if ( exists $args{'pet_id'}) { my $_base_variable = "{" . "petId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'pet_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'pet_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } - # form params + # form params if ( exists $args{'additional_metadata'} ) { - $form_params->{'additionalMetadata'} = WWW::SwaggerClient::APIClient::to_form_value($args{'additional_metadata'}); + $form_params->{'additionalMetadata'} = $self->{api_client}->to_form_value($args{'additional_metadata'}); - } # form params + }# form params if ( exists $args{'file'} ) { $form_params->{'file'} = [] unless defined $form_params->{'file'}; push $form_params->{'file'}, $args{'file'}; @@ -526,14 +522,14 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['petstore_auth']; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index d4261c6bbec..6e22d4dbcc0 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -27,12 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - - -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( get_inventory @@ -44,7 +40,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = WWW::SwaggerClient::APIClient->new; + my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( 'api_client' => $default_api_client, @_ @@ -95,18 +91,18 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = ['api_key']; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('HASH[string,int]', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('HASH[string,int]', $response); + return $_response_object; } @@ -144,23 +140,23 @@ sub new { my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('Order', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('Order', $response); + return $_response_object; } @@ -200,28 +196,28 @@ sub new { - # path params + # path params if ( exists $args{'order_id'}) { my $_base_variable = "{" . "orderId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'order_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'order_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('Order', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('Order', $response); + return $_response_object; } @@ -261,24 +257,24 @@ sub new { - # path params + # path params if ( exists $args{'order_id'}) { my $_base_variable = "{" . "orderId" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'order_id'}); + my $_base_value = $self->{api_client}->to_path_value($args{'order_id'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index 2e5889ba39e..5b2fc0a0000 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -27,12 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - - -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( create_user @@ -48,7 +44,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = WWW::SwaggerClient::APIClient->new; + my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( 'api_client' => $default_api_client, @_ @@ -98,19 +94,19 @@ sub new { my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -149,19 +145,19 @@ sub new { my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -200,19 +196,19 @@ sub new { my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -247,12 +243,12 @@ sub new { } $header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type(); - # query params + # query params if ( exists $args{'username'}) { - $query_params->{'username'} = WWW::::APIClient::to_query_value($args{'username'}); - } # query params + $query_params->{'username'} = $self->{api_client}->to_query_value($args{'username'}); + }# query params if ( exists $args{'password'}) { - $query_params->{'password'} = WWW::::APIClient::to_query_value($args{'password'}); + $query_params->{'password'} = $self->{api_client}->to_query_value($args{'password'}); } @@ -260,18 +256,18 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('string', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('string', $response); + return $_response_object; } @@ -310,14 +306,14 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -358,28 +354,28 @@ sub new { - # path params + # path params if ( exists $args{'username'}) { my $_base_variable = "{" . "username" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'}); + my $_base_value = $self->{api_client}->to_path_value($args{'username'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); if (!$response) { return; } - my $_response_object = $self->{api_client}->deserialize('User', $response); - return $_response_object; + my $_response_object = $self->{api_client}->deserialize('User', $response); + return $_response_object; } @@ -420,27 +416,27 @@ sub new { - # path params + # path params if ( exists $args{'username'}) { my $_base_variable = "{" . "username" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'}); + my $_base_value = $self->{api_client}->to_path_value($args{'username'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # body params + # body params if ( exists $args{'body'}) { $_body_data = $args{'body'}; } - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } @@ -481,24 +477,24 @@ sub new { - # path params + # path params if ( exists $args{'username'}) { my $_base_variable = "{" . "username" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args{'username'}); + my $_base_value = $self->{api_client}->to_path_value($args{'username'}); $_resource_path =~ s/$_base_variable/$_base_value/g; } my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; + # authentication setting, if any + my $auth_settings = []; # make the API Call $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, - $header_params, $_body_data); + $header_params, $_body_data, $auth_settings); return; } diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 7855254e657..6b731b846fe 100644 --- a/samples/client/petstore/perl/t/01_pet_api.t +++ b/samples/client/petstore/perl/t/01_pet_api.t @@ -1,4 +1,4 @@ -use Test::More tests => 31; +use Test::More tests => 33; use Test::Exception; use lib 'lib'; @@ -6,12 +6,19 @@ use strict; use warnings; use_ok('WWW::SwaggerClient::PetApi'); -use_ok('WWW::SwaggerClient::APIClient'); +use_ok('WWW::SwaggerClient::ApiClient'); use_ok('WWW::SwaggerClient::Object::Pet'); use_ok('WWW::SwaggerClient::Object::Tag'); use_ok('WWW::SwaggerClient::Object::Category'); + +my $api_client = WWW::SwaggerClient::ApiClient->new('base_url' => 'http://testing'); +my $pet_api = WWW::SwaggerClient::PetApi->new('api_client' => $api_client); +is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client'; + my $api = WWW::SwaggerClient::PetApi->new(); +is $api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; + # test select_header_content_type is $api->{api_client}->select_header_content_type('application/xml', 'Application/JSON'), 'application/json', 'get the proper content type application/json but not application/xml'; is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper content type application/json'; diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index eb06f786f98..d4f3fbb6f4b 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -5,7 +5,8 @@ use strict; use warnings; use WWW::SwaggerClient::PetApi; -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; +use WWW::SwaggerClient::Configuration; use WWW::SwaggerClient::Object::Pet; use WWW::SwaggerClient::Object::Tag; use WWW::SwaggerClient::Object::Category; @@ -13,6 +14,14 @@ use Data::Dumper; use DateTime; +$WWW::SwaggerClient::Configuration::http_user_agent = 'Perl-Swagger-Test'; +$WWW::SwaggerClient::Configuration::api_key->{'api_key'} = 'ZZZZZZZZZZZZZZ'; +$WWW::SwaggerClient::Configuration::api_key_prefix->{'api_key'} = 'BEARER'; + +$WWW::SwaggerClient::Configuration::username = 'username'; +$WWW::SwaggerClient::Configuration::password = 'password'; + + my $api = WWW::SwaggerClient::PetApi->new(); my $pet_id = 10008;