From 866b546cfde78a9d86fdda65264d0685f26b697b Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 27 May 2015 17:56:39 +0800 Subject: [PATCH 01/10] make api client pluggable --- .../codegen/languages/PerlClientCodegen.java | 1 + .../main/resources/perl/APIClient.mustache | 103 ++++++------------ .../resources/perl/Configuration.mustache | 16 +++ .../src/main/resources/perl/api.mustache | 24 ++-- .../perl/lib/WWW/SwaggerClient/APIClient.pm | 103 ++++++------------ .../lib/WWW/SwaggerClient/Configuration.pm | 16 +++ .../perl/lib/WWW/SwaggerClient/PetApi.pm | 81 +++++--------- .../perl/lib/WWW/SwaggerClient/StoreApi.pm | 25 +---- .../perl/lib/WWW/SwaggerClient/UserApi.pm | 55 +++------- samples/client/petstore/perl/t/01_pet_api.t | 9 +- 10 files changed, 166 insertions(+), 267 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/perl/Configuration.mustache create mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm 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 b25e4490e856..8f8ff21bc58a 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 @@ -77,6 +77,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("map", "HASH"); supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm")); + supportingFiles.add(new SupportingFile("Configuration.mustache", "lib/WWW/" + invokerPackage, "Configuration.pm")); supportingFiles.add(new SupportingFile("BaseObject.mustache", "lib/WWW/" + invokerPackage, "Object/BaseObject.pm")); } diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 19079a079223..5d5dc0cdb968 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -15,20 +15,16 @@ 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}}"; - - sub new { my $class = shift; - my %args = @_; + my (%args) = ( + 'ua' => LWP::UserAgent->new, + 'base_url' => '{{basePath}}', + @_ + ); return bless \%args, $class; } @@ -38,8 +34,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 +43,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 @@ -67,7 +63,7 @@ sub call_api { 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) { @@ -80,43 +76,42 @@ sub call_api { # 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' ? 'form-data' : $header_params->{'Content-Type'}; $_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' ? 'form-data' : $header_params->{'Content-Type'}; $_request = PUT($_url, Accept => $header_params->{Accept}, Content_Type => $_content_type, Content => $_body_data); - } - case 'GET' { + } + elsif ($method eq 'GET') { $_request = GET($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}); - } - case 'HEAD' { + } + elsif ($method eq 'HEAD') { $_request = HEAD($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}); - } - case 'DELETE' { #TODO support form data + } + elsif ($method eq 'DELETE') { #TODO support form data $_request = DELETE($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - } - case 'PATCH' { #TODO - } - + } + 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 +121,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 +138,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 +153,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 +163,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 +173,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(); } 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 000000000000..0967dc136c32 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -0,0 +1,16 @@ +package WWW::{{invokerPackage}}::Configuration; + +use strict; +use warnings; +use utf8; + +use Log::Any qw($log); +use Carp; + +# class/static variables +my $api_client = WWW::SwaggerClient::APIClient->new; +my $http_timeout = 180; +my $http_user_agent = 'Perl-Swagger'; + + +1; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 131eb3a39347..4996534c35e6 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -28,9 +28,6 @@ use Carp qw( croak ); use Log::Any qw($log); -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - {{#operations}} use WWW::{{invokerPackage}}::APIClient; @@ -92,37 +89,34 @@ 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; - # make the API Call {{#returnType}}my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 0f0fea793e36..cf4ec0db4f39 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -15,20 +15,16 @@ 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"; - - 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 +34,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 +43,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 @@ -67,7 +63,7 @@ sub call_api { 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) { @@ -80,43 +76,42 @@ sub call_api { # 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' ? 'form-data' : $header_params->{'Content-Type'}; $_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' ? 'form-data' : $header_params->{'Content-Type'}; $_request = PUT($_url, Accept => $header_params->{Accept}, Content_Type => $_content_type, Content => $_body_data); - } - case 'GET' { + } + elsif ($method eq 'GET') { $_request = GET($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}); - } - case 'HEAD' { + } + elsif ($method eq 'HEAD') { $_request = HEAD($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}); - } - case 'DELETE' { #TODO support form data + } + elsif ($method eq 'DELETE') { #TODO support form data $_request = DELETE($_url, Accept => $header_params->{'Accept'}, Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - } - case 'PATCH' { #TODO - } - + } + 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 +121,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 +138,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 +153,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 +163,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 +173,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(); } 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 000000000000..b8c57e668436 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -0,0 +1,16 @@ +package WWW::SwaggerClient::Configuration; + +use strict; +use warnings; +use utf8; + +use Log::Any qw($log); +use Carp; + +# class/static variables +my $api_client = WWW::SwaggerClient::APIClient->new; +my $http_timeout = 180; +my $http_user_agent = 'Perl-Swagger'; + + +1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 57237a96974d..e796022bad1a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -28,9 +28,6 @@ use Carp qw( croak ); use Log::Any qw($log); -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - use WWW::SwaggerClient::APIClient; @@ -91,21 +88,18 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -142,21 +136,18 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -195,9 +186,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,9 +196,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -249,9 +237,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,9 +247,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -310,19 +295,16 @@ 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; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -369,33 +351,30 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -441,23 +420,20 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -501,22 +477,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,9 +502,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index d4261c6bbec4..e457c99cac28 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -28,9 +28,6 @@ use Carp qw( croak ); use Log::Any qw($log); -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - use WWW::SwaggerClient::APIClient; @@ -95,9 +92,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -144,14 +138,11 @@ 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; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -200,19 +191,16 @@ 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; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -261,19 +249,16 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index 2e5889ba39e0..ece7f3e544b4 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -28,9 +28,6 @@ use Carp qw( croak ); use Log::Any qw($log); -#use WWW::Swagger::Model::Category; -#use WWW::Swagger::Model::Pet; - use WWW::SwaggerClient::APIClient; @@ -98,14 +95,11 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -149,14 +143,11 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -200,14 +191,11 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -247,12 +235,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,9 +248,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -310,9 +295,6 @@ sub new { my $_body_data; - # for HTTP post (form) - #$_body_data = $_body ? undef : $form_params; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -358,19 +340,16 @@ 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; - # make the API Call my $response = $self->{api_client}->call_api($_resource_path, $_method, $query_params, $form_params, @@ -420,22 +399,19 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, @@ -481,19 +457,16 @@ 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; - # make the API Call $self->{api_client}->call_api($_resource_path, $_method, diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 7855254e6578..4a2ef58ee91c 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'; @@ -10,8 +10,15 @@ 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'; From 2c12312b9778643d0c94d9114b86e6d427a00aed Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 27 May 2015 22:44:56 +0800 Subject: [PATCH 02/10] update user agent and add more test cases --- .../src/main/resources/perl/APIClient.mustache | 2 ++ .../src/main/resources/perl/Configuration.mustache | 8 +++++--- .../swagger-codegen/src/main/resources/perl/api.mustache | 7 +++---- .../petstore/perl/lib/WWW/SwaggerClient/APIClient.pm | 2 ++ .../petstore/perl/lib/WWW/SwaggerClient/Configuration.pm | 8 +++++--- .../client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm | 5 ++--- .../petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm | 5 ++--- .../client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm | 5 ++--- samples/client/petstore/perl/test.pl | 3 +++ 9 files changed, 26 insertions(+), 19 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 5d5dc0cdb968..79528dad23b4 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -17,6 +17,8 @@ use Log::Any qw($log); use Carp; use Module::Runtime qw(use_module); +use WWW::{{invokerPackage}}::Configuration; + sub new { my $class = shift; diff --git a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache index 0967dc136c32..07ff9b87785a 100644 --- a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -7,10 +7,12 @@ use utf8; use Log::Any qw($log); use Carp; +use WWW::{{invokerPackage}}::APIClient; + # class/static variables -my $api_client = WWW::SwaggerClient::APIClient->new; -my $http_timeout = 180; -my $http_user_agent = 'Perl-Swagger'; +our $api_client; +our $http_timeout = 180; +our $http_user_agent = 'Perl-Swagger'; 1; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 4996534c35e6..54647ca4b8f0 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -27,11 +27,10 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); +use WWW::{{invokerPackage}}::APIClient; +use WWW::{{invokerPackage}}::Configuration; {{#operations}} - -use WWW::{{invokerPackage}}::APIClient; - our @EXPORT_OK = qw( {{#operation}}{{{nickname}}} {{/operation}} @@ -39,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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index cf4ec0db4f39..3f6aaf9ff87f 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -17,6 +17,8 @@ use Log::Any qw($log); use Carp; use Module::Runtime qw(use_module); +use WWW::SwaggerClient::Configuration; + sub new { my $class = shift; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm index b8c57e668436..e3da7f3332d5 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -7,10 +7,12 @@ use utf8; use Log::Any qw($log); use Carp; +use WWW::SwaggerClient::APIClient; + # class/static variables -my $api_client = WWW::SwaggerClient::APIClient->new; -my $http_timeout = 180; -my $http_user_agent = 'Perl-Swagger'; +our $api_client; +our $http_timeout = 180; +our $http_user_agent = 'Perl-Swagger'; 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index e796022bad1a..077e7cbc45f0 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -27,9 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - - use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( update_pet @@ -45,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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index e457c99cac28..0c2d25d02361 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -27,9 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - - use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( get_inventory @@ -41,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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index ece7f3e544b4..ecdd8eab9c28 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -27,9 +27,8 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); - - use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( create_user @@ -45,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, @_ diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index eb06f786f98e..f1e819be5ced 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -6,6 +6,7 @@ use strict; use warnings; use WWW::SwaggerClient::PetApi; 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,8 @@ use JSON; use Data::Dumper; use DateTime; +$WWW::SwaggerClient::Configuration::http_user_agent = 'Perl-Swagger-Test'; + my $api = WWW::SwaggerClient::PetApi->new(); my $pet_id = 10008; From 1a868abdfa57dfccd4370d10187b72eaac580c08 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 28 May 2015 10:51:36 +0800 Subject: [PATCH 03/10] rename APIClient to ApiClient --- .../wordnik/swagger/codegen/languages/PerlClientCodegen.java | 2 +- .../src/main/resources/perl/APIClient.mustache | 2 +- .../src/main/resources/perl/Configuration.mustache | 2 -- modules/swagger-codegen/src/main/resources/perl/api.mustache | 4 ++-- .../client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm | 2 +- .../petstore/perl/lib/WWW/SwaggerClient/Configuration.pm | 2 -- samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm | 4 ++-- .../client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm | 4 ++-- samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm | 4 ++-- samples/client/petstore/perl/t/01_pet_api.t | 4 ++-- samples/client/petstore/perl/test.pl | 2 +- 11 files changed, 14 insertions(+), 18 deletions(-) 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 8f8ff21bc58a..24a14135225b 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 @@ -76,7 +76,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("array", "ARRAY"); typeMapping.put("map", "HASH"); - supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm")); + supportingFiles.add(new SupportingFile("ApiClient.mustache", "lib/WWW/" + invokerPackage, "ApiClient.pm")); supportingFiles.add(new SupportingFile("Configuration.mustache", "lib/WWW/" + invokerPackage, "Configuration.pm")); supportingFiles.add(new SupportingFile("BaseObject.mustache", "lib/WWW/" + invokerPackage, "Object/BaseObject.pm")); } diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 79528dad23b4..d5fa48ac6d96 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -1,4 +1,4 @@ -package WWW::{{invokerPackage}}::APIClient; +package WWW::{{invokerPackage}}::ApiClient; use strict; use warnings; diff --git a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache index 07ff9b87785a..90ad999c8a94 100644 --- a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -7,8 +7,6 @@ use utf8; use Log::Any qw($log); use Carp; -use WWW::{{invokerPackage}}::APIClient; - # class/static variables our $api_client; our $http_timeout = 180; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 54647ca4b8f0..f1168d18ce78 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -27,7 +27,7 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); -use WWW::{{invokerPackage}}::APIClient; +use WWW::{{invokerPackage}}::ApiClient; use WWW::{{invokerPackage}}::Configuration; {{#operations}} @@ -38,7 +38,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = $WWW::{{invokerPackage}}::Configuration::api_client ? $WWW::{{invokerPackage}}::Configuration::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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 3f6aaf9ff87f..c80677ca5182 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::APIClient; +package WWW::SwaggerClient::ApiClient; use strict; use warnings; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm index e3da7f3332d5..08982c445e16 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -7,8 +7,6 @@ use utf8; use Log::Any qw($log); use Carp; -use WWW::SwaggerClient::APIClient; - # class/static variables our $api_client; our $http_timeout = 180; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 077e7cbc45f0..92fdd50e5efc 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -27,7 +27,7 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( @@ -44,7 +44,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index 0c2d25d02361..b6d864a6be35 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -27,7 +27,7 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( @@ -40,7 +40,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::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, @_ diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index ecdd8eab9c28..fa0d9ed5c4f1 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -27,7 +27,7 @@ use Exporter; use Carp qw( croak ); use Log::Any qw($log); -use WWW::SwaggerClient::APIClient; +use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::Configuration; our @EXPORT_OK = qw( @@ -44,7 +44,7 @@ our @EXPORT_OK = qw( sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::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, @_ diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 4a2ef58ee91c..6b731b846fec 100644 --- a/samples/client/petstore/perl/t/01_pet_api.t +++ b/samples/client/petstore/perl/t/01_pet_api.t @@ -6,12 +6,12 @@ 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 $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'; diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index f1e819be5ced..3479f6bd3a98 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -5,7 +5,7 @@ use lib 'lib'; 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; From 699f6fff4411672a6ce05bffe35d54293d8ba37f Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 28 May 2015 14:18:05 +0800 Subject: [PATCH 04/10] update file path --- .../swagger/codegen/languages/PerlClientCodegen.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 24a14135225b..74670f27e090 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,7 +27,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { public PerlClientCodegen() { super(); - modelPackage = "Object"; + modelPackage = "/Object"; outputFolder = "generated-code/perl"; modelTemplateFiles.put("object.mustache", ".pm"); apiTemplateFiles.put("api.mustache", ".pm"); @@ -76,9 +76,9 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("array", "ARRAY"); typeMapping.put("map", "HASH"); - supportingFiles.add(new SupportingFile("ApiClient.mustache", "lib/WWW/" + invokerPackage, "ApiClient.pm")); - supportingFiles.add(new SupportingFile("Configuration.mustache", "lib/WWW/" + invokerPackage, "Configuration.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 @@ -88,11 +88,11 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { @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 From 3d16d8b2b6aecc5d281335428803012905958a43 Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 3 Jun 2015 08:34:45 +0800 Subject: [PATCH 05/10] fix header --- :w | 290 ++++++++++++++++++ .../main/resources/perl/APIClient.mustache | 82 ++++- .../resources/perl/Configuration.mustache | 6 + .../src/main/resources/perl/api.mustache | 11 +- .../perl/lib/WWW/SwaggerClient/APIClient.pm | 88 +++++- .../lib/WWW/SwaggerClient/Configuration.pm | 6 + .../perl/lib/WWW/SwaggerClient/PetApi.pm | 52 +++- .../perl/lib/WWW/SwaggerClient/StoreApi.pm | 32 +- .../perl/lib/WWW/SwaggerClient/UserApi.pm | 48 ++- samples/client/petstore/perl/test.pl | 2 + 10 files changed, 553 insertions(+), 64 deletions(-) create mode 100644 :w diff --git a/:w b/:w new file mode 100644 index 000000000000..b34a3de344bd --- /dev/null +++ b/:w @@ -0,0 +1,290 @@ +package WWW::{{invokerPackage}}::ApiClient; + +use strict; +use warnings; +use utf8; + +use MIME::Base64; +use LWP::UserAgent; +use HTTP::Headers; +use HTTP::Response; +use HTTP::Request::Common qw(DELETE POST GET HEAD PUT); +use HTTP::Status; +use URI::Query; +use JSON; +use URI::Escape; +use Scalar::Util; +use Log::Any qw($log); +use Carp; +use Module::Runtime qw(use_module); + +use WWW::{{invokerPackage}}::Configuration; + +sub new +{ + my $class = shift; + my (%args) = ( + 'ua' => LWP::UserAgent->new, + 'base_url' => '{{basePath}}', + @_ + ); + + return bless \%args, $class; +} + +# Set the user agent of the API client +# +# @param string $user_agent The user agent of the API client +# +sub set_user_agent { + my ($self, $user_agent) = @_; + $self->{http_user_agent}= $user_agent; +} + +# Set timeout +# +# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] +# +sub set_timeout { + my ($self, $seconds) = @_; + if (!looks_like_number($seconds)) { + croak('Timeout variable must be numeric.'); + } + $self->{http_timeout} = $seconds; +} + +# make the HTTP request +# @param string $resourcePath path to method endpoint +# @param string $method method to call +# @param array $queryParams parameters to be place in query URL +# @param array $postData parameters to be placed in POST body +# @param array $headerParams parameters to be place in request header +# @return mixed +sub call_api { + my $self = shift; + my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_; + + my $headers = HTTP::Headers->new(%$header_params); + + my $_url = $self->{base_url} . $resource_path; + + # build query + if (%$query_params) { + $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); + } + + # update parameters based on authentication settings + update_params_for_auth(\$query_params, \$header_params, \$auth_settings); + + # 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') { + # multipart + my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? + 'form-data' : $header_params->{'Content-Type'}; + + $_request = POST($_url, Accept => $header_params->{Accept}, + Content_Type => $_content_type, Content => $_body_data); + } + elsif ($method eq 'PUT') { + # multipart + my $_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); + } + elsif ($method eq 'GET') { + $_request = GET($_url, Accept => $header_params->{'Accept'}, + Content_Type => $header_params->{'Content-Type'}); + } + elsif ($method eq 'HEAD') { + $_request = HEAD($_url, Accept => $header_params->{'Accept'}, + Content_Type => $header_params->{'Content-Type'}); + } + elsif ($method eq 'DELETE') { #TODO support form data + $_request = DELETE($_url, Accept => $header_params->{'Accept'}, + Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + } + elsif ($method eq 'PATCH') { #TODO + } + else { + } + + $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 = $self->{ua}->request($_request); + + unless ($_response->is_success) { + croak("API Exception(".$_response->code."): ".$_response->message); + } + + return $_response->content; + +} + +# 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 ($self, $value) = @_; + return uri_escape($self->to_string($value)); +} + + +# Take value and turn it into a string suitable for inclusion in +# the query, by imploding comma-separated if it's an object. +# If it's a string, pass through unchanged. It will be url-encoded +# later. +# @param object $object an object to be serialized to a string +# @return string the serialized object +sub to_query_value { + my ($self, $object) = @_; + if (is_array($object)) { + return implode(',', $object); + } else { + return $self->to_string($object); + } +} + + +# Take value and turn it into a string suitable for inclusion in +# the header. If it's a string, pass through unchanged +# If it's a datetime object, format it in ISO8601 +# @param string $value a string which will be part of the header +# @return string the header string +sub to_header_value { + my ($self, $value) = @_; + return $self->to_string($value); +} + +# Take value and turn it into a string suitable for inclusion in +# the http body (form parameter). If it's a string, pass through unchanged +# If it's a datetime object, format it in ISO8601 +# @param string $value the value of the form parameter +# @return string the form string +sub to_form_value { + my ($self, $value) = @_; + return $self->to_string($value); +} + +# Take value and turn it into a string suitable for inclusion in +# the parameter. If it's a string, pass through unchanged +# If it's a datetime object, format it in ISO8601 +# @param string $value the value of the parameter +# @return string the header string +sub to_string { + my ($self, $value) = @_; + if (ref($value) eq "DateTime") { # datetime in ISO8601 format + return $value->datetime(); + } + else { + return $value; + } +} + +# Deserialize a JSON string into an object +# +# @param string $class class name is passed as a string +# @param string $data data of the body +# @return object an instance of $class +sub deserialize +{ + my ($self, $class, $data) = @_; + $log->debugf("deserializing %s for %s", $data, $class); + my $_result; + + if (not defined $data) { + return undef; + } elsif ( lc(substr($class, 0, 4)) eq 'map[') { #hash + $_result = \(json_decode $data); + } elsif ( lc(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 = json_decode $data; + my @_values = (); + foreach my $_value (@_json_data) { + push @_values, $self->deserialize($_sub_class, $_value); + } + $_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 + $_result= $data; + } else { # model + my $_instance = use_module("WWW::{{invokerPackage}}::Object::$class")->new; + $_result = $_instance->from_hash(decode_json $data); + } + + return $_result; + +} + +# return 'Accept' based on an array of accept provided +# @param [Array] header_accept_array Array fo 'Accept' +# @return String Accept (e.g. application/json) +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 +# @param [Array] content_type_array Array fo content-type +# @return String Content-Type (e.g. application/json) +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); + } + +} + +# 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 (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(Configuration::username.":".Configuration::password);{{/isBasic}} + {{#isOAuth}}//TODO support oauth{{/isOAuth}} + }{{/authMethods}} + else { + //TODO show warning about security definition not found + } + } +} + + +1; diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index d5fa48ac6d96..2b0f39a41cdc 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -4,6 +4,7 @@ use strict; use warnings; use utf8; +use MIME::Base64; use LWP::UserAgent; use HTTP::Headers; use HTTP::Response; @@ -61,9 +62,11 @@ 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 = $self->{base_url} . $resource_path; @@ -72,6 +75,7 @@ sub call_api { $_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; @@ -82,28 +86,43 @@ sub call_api { # multipart my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; + $header_params->{'Content-Type'} = $_content_type; + my $headers = HTTP::Headers->new(%$header_params); + + #$_request = POST($_url, Accept => $header_params->{Accept}, + # Content_Type => $_content_type, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) - $_request = POST($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); } elsif ($method eq 'PUT') { # multipart my $_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); + $header_params->{'Content-Type'} = $_content_type; + my $headers = HTTP::Headers->new(%$header_params); + + #$_request = PUT($_url, Accept => $header_params->{Accept}, + # Content_Type => $_content_type, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + } elsif ($method eq 'GET') { - $_request = GET($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = GET($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}); + $_request = HTTP::Request->new( $method, $_url, $headers) } elsif ($method eq 'HEAD') { - $_request = HEAD($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = HEAD($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}); + $_request = HTTP::Request->new( $method, $_url, $headers) } elsif ($method eq 'DELETE') { #TODO support form data - $_request = DELETE($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = DELETE($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) } elsif ($method eq 'PATCH') { #TODO } @@ -256,5 +275,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/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache index 90ad999c8a94..38eb2ad8dae4 100644 --- a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -12,5 +12,11 @@ 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 f1168d18ce78..5adb50344de0 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -116,18 +116,21 @@ sub new { $_body_data = $args{'{{paramName}}'}; }{{/bodyParams}} + # 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/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index c80677ca5182..427c09324b8a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -4,6 +4,7 @@ use strict; use warnings; use utf8; +use MIME::Base64; use LWP::UserAgent; use HTTP::Headers; use HTTP::Response; @@ -61,9 +62,11 @@ 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 = $self->{base_url} . $resource_path; @@ -72,38 +75,56 @@ sub call_api { $_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; + use Data::Dumper; if ($method eq 'POST') { # multipart my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 'form-data' : $header_params->{'Content-Type'}; + $header_params->{'Content-Type'} = $_content_type; + my $headers = HTTP::Headers->new(%$header_params); + + #$_request = POST($_url, Accept => $header_params->{Accept}, + # Content_Type => $_content_type, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ); + print Dumper($_request); - $_request = POST($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); } elsif ($method eq 'PUT') { # multipart my $_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); + $header_params->{'Content-Type'} = $_content_type; + my $headers = HTTP::Headers->new(%$header_params); + + #$_request = PUT($_url, Accept => $header_params->{Accept}, + # Content_Type => $_content_type, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + } elsif ($method eq 'GET') { - $_request = GET($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = GET($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}); + $_request = HTTP::Request->new( $method, $_url, $headers) } elsif ($method eq 'HEAD') { - $_request = HEAD($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = HEAD($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}); + $_request = HTTP::Request->new( $method, $_url, $headers) } elsif ($method eq 'DELETE') { #TODO support form data - $_request = DELETE($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + my $headers = HTTP::Headers->new(%$header_params); + #$_request = DELETE($_url, Accept => $header_params->{'Accept'}, + # Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) } elsif ($method eq 'PATCH') { #TODO } @@ -256,5 +277,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 index 08982c445e16..bbce9d74759e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -12,5 +12,11 @@ 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 92fdd50e5efc..44bc1567a3d4 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -99,11 +99,14 @@ sub new { $_body_data = $args{'body'}; } + # 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; } @@ -147,11 +150,14 @@ sub new { $_body_data = $args{'body'}; } + # 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,15 +201,18 @@ sub new { my $_body_data; + # 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; } @@ -246,15 +255,18 @@ sub new { my $_body_data; + # 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; } @@ -304,15 +316,18 @@ sub new { my $_body_data; + # 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; } @@ -374,11 +389,14 @@ sub new { my $_body_data; + # 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; } @@ -433,11 +451,14 @@ sub new { my $_body_data; + # 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,11 +522,14 @@ sub new { my $_body_data; + # 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 b6d864a6be35..6e22d4dbcc0c 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -91,15 +91,18 @@ sub new { my $_body_data; + # 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; } @@ -142,15 +145,18 @@ sub new { $_body_data = $args{'body'}; } + # 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,15 +206,18 @@ sub new { my $_body_data; + # 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; } @@ -258,11 +267,14 @@ sub new { my $_body_data; + # 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 fa0d9ed5c4f1..5b2fc0a0000e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -99,11 +99,14 @@ sub new { $_body_data = $args{'body'}; } + # 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; } @@ -147,11 +150,14 @@ sub new { $_body_data = $args{'body'}; } + # 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; } @@ -195,11 +201,14 @@ sub new { $_body_data = $args{'body'}; } + # 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,15 +256,18 @@ sub new { my $_body_data; + # 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; } @@ -294,11 +306,14 @@ sub new { my $_body_data; + # 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; } @@ -349,15 +364,18 @@ sub new { my $_body_data; + # 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; } @@ -411,11 +429,14 @@ sub new { $_body_data = $args{'body'}; } + # 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; } @@ -466,11 +487,14 @@ sub new { my $_body_data; + # 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/test.pl b/samples/client/petstore/perl/test.pl index 3479f6bd3a98..bce4da39adf7 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -15,6 +15,8 @@ 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'; my $api = WWW::SwaggerClient::PetApi->new(); From fceaed34e47573c3fbaa9cb7dfe5180a3caa04cd Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 3 Jun 2015 10:19:07 +0800 Subject: [PATCH 06/10] rename apiclient --- .../client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 427c09324b8a..c9e9d82df104 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -82,7 +82,6 @@ sub call_api { # Make the HTTP request my $_request; - use Data::Dumper; if ($method eq 'POST') { # multipart my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? @@ -92,8 +91,7 @@ sub call_api { #$_request = POST($_url, Accept => $header_params->{Accept}, # Content_Type => $_content_type, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ); - print Dumper($_request); + $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) } elsif ($method eq 'PUT') { From 9b919c54dde59711a1378c836940832ebe77dabb Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 3 Jun 2015 11:21:10 +0800 Subject: [PATCH 07/10] added http basic auth test --- .../main/resources/perl/APIClient.mustache | 28 +++++-------------- .../perl/lib/WWW/SwaggerClient/APIClient.pm | 28 +++++-------------- samples/client/petstore/perl/test.pl | 4 +++ 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 2b0f39a41cdc..1d71ca3f99a3 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -84,45 +84,31 @@ sub call_api { my $_request; 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'}; - $header_params->{'Content-Type'} = $_content_type; - my $headers = HTTP::Headers->new(%$header_params); - #$_request = POST($_url, Accept => $header_params->{Accept}, - # Content_Type => $_content_type, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = POST($_url, %$header_params, Content => $_body_data); } 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'}; - $header_params->{'Content-Type'} = $_content_type; - my $headers = HTTP::Headers->new(%$header_params); - #$_request = PUT($_url, Accept => $header_params->{Accept}, - # Content_Type => $_content_type, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = PUT($_url, %$header_params, Content => $_body_data); } elsif ($method eq 'GET') { my $headers = HTTP::Headers->new(%$header_params); - #$_request = GET($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}); - $_request = HTTP::Request->new( $method, $_url, $headers) + $_request = GET($_url, %$header_params); } elsif ($method eq 'HEAD') { my $headers = HTTP::Headers->new(%$header_params); - #$_request = HEAD($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}); - $_request = HTTP::Request->new( $method, $_url, $headers) + $_request = HEAD($_url,%$header_params); } elsif ($method eq 'DELETE') { #TODO support form data my $headers = HTTP::Headers->new(%$header_params); - #$_request = DELETE($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = DELETE($_url, %$headers); } elsif ($method eq 'PATCH') { #TODO } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index c9e9d82df104..0ac34355c4c7 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -84,45 +84,31 @@ sub call_api { my $_request; 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'}; - $header_params->{'Content-Type'} = $_content_type; - my $headers = HTTP::Headers->new(%$header_params); - #$_request = POST($_url, Accept => $header_params->{Accept}, - # Content_Type => $_content_type, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = POST($_url, %$header_params, Content => $_body_data); } 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'}; - $header_params->{'Content-Type'} = $_content_type; - my $headers = HTTP::Headers->new(%$header_params); - #$_request = PUT($_url, Accept => $header_params->{Accept}, - # Content_Type => $_content_type, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = PUT($_url, %$header_params, Content => $_body_data); } elsif ($method eq 'GET') { my $headers = HTTP::Headers->new(%$header_params); - #$_request = GET($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}); - $_request = HTTP::Request->new( $method, $_url, $headers) + $_request = GET($_url, %$header_params); } elsif ($method eq 'HEAD') { my $headers = HTTP::Headers->new(%$header_params); - #$_request = HEAD($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}); - $_request = HTTP::Request->new( $method, $_url, $headers) + $_request = HEAD($_url,%$header_params); } elsif ($method eq 'DELETE') { #TODO support form data my $headers = HTTP::Headers->new(%$header_params); - #$_request = DELETE($_url, Accept => $header_params->{'Accept'}, - # Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - $_request = HTTP::Request->new( $method, $_url, $headers, $_body_data ) + $_request = DELETE($_url, %$headers); } elsif ($method eq 'PATCH') { #TODO } diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index bce4da39adf7..d4f3fbb6f4be 100755 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -18,6 +18,10 @@ $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; From afe816efb6019cc9f7d47fc51bb73836de014423 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 4 Jun 2015 20:30:41 +0800 Subject: [PATCH 08/10] remove unused file --- :w | 290 ------------------------------------------------------------- 1 file changed, 290 deletions(-) delete mode 100644 :w diff --git a/:w b/:w deleted file mode 100644 index b34a3de344bd..000000000000 --- a/:w +++ /dev/null @@ -1,290 +0,0 @@ -package WWW::{{invokerPackage}}::ApiClient; - -use strict; -use warnings; -use utf8; - -use MIME::Base64; -use LWP::UserAgent; -use HTTP::Headers; -use HTTP::Response; -use HTTP::Request::Common qw(DELETE POST GET HEAD PUT); -use HTTP::Status; -use URI::Query; -use JSON; -use URI::Escape; -use Scalar::Util; -use Log::Any qw($log); -use Carp; -use Module::Runtime qw(use_module); - -use WWW::{{invokerPackage}}::Configuration; - -sub new -{ - my $class = shift; - my (%args) = ( - 'ua' => LWP::UserAgent->new, - 'base_url' => '{{basePath}}', - @_ - ); - - return bless \%args, $class; -} - -# Set the user agent of the API client -# -# @param string $user_agent The user agent of the API client -# -sub set_user_agent { - my ($self, $user_agent) = @_; - $self->{http_user_agent}= $user_agent; -} - -# Set timeout -# -# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] -# -sub set_timeout { - my ($self, $seconds) = @_; - if (!looks_like_number($seconds)) { - croak('Timeout variable must be numeric.'); - } - $self->{http_timeout} = $seconds; -} - -# make the HTTP request -# @param string $resourcePath path to method endpoint -# @param string $method method to call -# @param array $queryParams parameters to be place in query URL -# @param array $postData parameters to be placed in POST body -# @param array $headerParams parameters to be place in request header -# @return mixed -sub call_api { - my $self = shift; - my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_; - - my $headers = HTTP::Headers->new(%$header_params); - - my $_url = $self->{base_url} . $resource_path; - - # build query - if (%$query_params) { - $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); - } - - # update parameters based on authentication settings - update_params_for_auth(\$query_params, \$header_params, \$auth_settings); - - # 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') { - # multipart - my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ? - 'form-data' : $header_params->{'Content-Type'}; - - $_request = POST($_url, Accept => $header_params->{Accept}, - Content_Type => $_content_type, Content => $_body_data); - } - elsif ($method eq 'PUT') { - # multipart - my $_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); - } - elsif ($method eq 'GET') { - $_request = GET($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - elsif ($method eq 'HEAD') { - $_request = HEAD($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}); - } - elsif ($method eq 'DELETE') { #TODO support form data - $_request = DELETE($_url, Accept => $header_params->{'Accept'}, - Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); - } - elsif ($method eq 'PATCH') { #TODO - } - else { - } - - $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 = $self->{ua}->request($_request); - - unless ($_response->is_success) { - croak("API Exception(".$_response->code."): ".$_response->message); - } - - return $_response->content; - -} - -# 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 ($self, $value) = @_; - return uri_escape($self->to_string($value)); -} - - -# Take value and turn it into a string suitable for inclusion in -# the query, by imploding comma-separated if it's an object. -# If it's a string, pass through unchanged. It will be url-encoded -# later. -# @param object $object an object to be serialized to a string -# @return string the serialized object -sub to_query_value { - my ($self, $object) = @_; - if (is_array($object)) { - return implode(',', $object); - } else { - return $self->to_string($object); - } -} - - -# Take value and turn it into a string suitable for inclusion in -# the header. If it's a string, pass through unchanged -# If it's a datetime object, format it in ISO8601 -# @param string $value a string which will be part of the header -# @return string the header string -sub to_header_value { - my ($self, $value) = @_; - return $self->to_string($value); -} - -# Take value and turn it into a string suitable for inclusion in -# the http body (form parameter). If it's a string, pass through unchanged -# If it's a datetime object, format it in ISO8601 -# @param string $value the value of the form parameter -# @return string the form string -sub to_form_value { - my ($self, $value) = @_; - return $self->to_string($value); -} - -# Take value and turn it into a string suitable for inclusion in -# the parameter. If it's a string, pass through unchanged -# If it's a datetime object, format it in ISO8601 -# @param string $value the value of the parameter -# @return string the header string -sub to_string { - my ($self, $value) = @_; - if (ref($value) eq "DateTime") { # datetime in ISO8601 format - return $value->datetime(); - } - else { - return $value; - } -} - -# Deserialize a JSON string into an object -# -# @param string $class class name is passed as a string -# @param string $data data of the body -# @return object an instance of $class -sub deserialize -{ - my ($self, $class, $data) = @_; - $log->debugf("deserializing %s for %s", $data, $class); - my $_result; - - if (not defined $data) { - return undef; - } elsif ( lc(substr($class, 0, 4)) eq 'map[') { #hash - $_result = \(json_decode $data); - } elsif ( lc(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 = json_decode $data; - my @_values = (); - foreach my $_value (@_json_data) { - push @_values, $self->deserialize($_sub_class, $_value); - } - $_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 - $_result= $data; - } else { # model - my $_instance = use_module("WWW::{{invokerPackage}}::Object::$class")->new; - $_result = $_instance->from_hash(decode_json $data); - } - - return $_result; - -} - -# return 'Accept' based on an array of accept provided -# @param [Array] header_accept_array Array fo 'Accept' -# @return String Accept (e.g. application/json) -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 -# @param [Array] content_type_array Array fo content-type -# @return String Content-Type (e.g. application/json) -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); - } - -} - -# 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 (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(Configuration::username.":".Configuration::password);{{/isBasic}} - {{#isOAuth}}//TODO support oauth{{/isOAuth}} - }{{/authMethods}} - else { - //TODO show warning about security definition not found - } - } -} - - -1; From 0423e0a2dfb211eafc56cba0a970d85e86f63e62 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 4 Jun 2015 21:12:26 +0800 Subject: [PATCH 09/10] support object response --- .../swagger/codegen/languages/PerlClientCodegen.java | 6 ++++-- .../src/main/resources/perl/APIClient.mustache | 2 +- .../client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) 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 74670f27e090..b96b012abbaa 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 class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { 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 class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { languageSpecificPrimitives.add("DateTime"); languageSpecificPrimitives.add("ARRAY"); languageSpecificPrimitives.add("HASH"); + languageSpecificPrimitives.add("object"); typeMapping.put("integer", "int"); typeMapping.put("long", "int"); @@ -75,6 +76,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { 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).replace('/', File.separatorChar), "ApiClient.pm")); supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + invokerPackage).replace('/', File.separatorChar), "Configuration.pm")); diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 1d71ca3f99a3..2bcdd6da6906 100644 --- a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -216,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; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index 0ac34355c4c7..da29b3e246a7 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -216,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; From ba227b7e727f18c5c3d2ff6fe906b072a5237a04 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 4 Jun 2015 21:19:16 +0800 Subject: [PATCH 10/10] rename api client --- .../resources/{perl/APIClient.mustache => perlApiClient.mustache} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/swagger-codegen/src/main/resources/{perl/APIClient.mustache => perlApiClient.mustache} (100%) diff --git a/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache b/modules/swagger-codegen/src/main/resources/perlApiClient.mustache similarity index 100% rename from modules/swagger-codegen/src/main/resources/perl/APIClient.mustache rename to modules/swagger-codegen/src/main/resources/perlApiClient.mustache