From 0a34793f5abc3462ccd36754e94b37a8e2a2a189 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 7 May 2015 10:03:30 +0800 Subject: [PATCH] add inheritance to object (model) --- .../codegen/languages/PerlClientCodegen.java | 5 +- .../main/resources/perl/APIClient.mustache | 106 ++++---- .../main/resources/perl/BaseObject.mustache | 76 ++++++ .../src/main/resources/perl/api.mustache | 8 +- .../perl/{model.mustache => object.mustache} | 7 +- .../perl/lib/WWW/SwaggerClient/APIClient.pm | 7 +- .../WWW/SwaggerClient/Object/BaseObject.pm | 76 ++++++ .../{Model => Object}/Category.pm | 7 +- .../SwaggerClient/{Model => Object}/Order.pm | 7 +- .../SwaggerClient/{Model => Object}/Pet.pm | 7 +- .../SwaggerClient/{Model => Object}/Tag.pm | 7 +- .../SwaggerClient/{Model => Object}/User.pm | 7 +- .../perl/lib/WWW/SwaggerClient/PetApi.pm | 4 +- .../perl/lib/WWW/SwaggerClient/Swagger.pl | 230 ------------------ .../perl/lib/WWW/SwaggerClient/UserApi.pm | 4 +- samples/client/petstore/perl/test.pl | 20 +- 16 files changed, 262 insertions(+), 316 deletions(-) rename samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pm => modules/swagger-codegen/src/main/resources/perl/APIClient.mustache (71%) create mode 100644 modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache rename modules/swagger-codegen/src/main/resources/perl/{model.mustache => object.mustache} (91%) create mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm rename samples/client/petstore/perl/lib/WWW/SwaggerClient/{Model => Object}/Category.pm (91%) rename samples/client/petstore/perl/lib/WWW/SwaggerClient/{Model => Object}/Order.pm (93%) rename samples/client/petstore/perl/lib/WWW/SwaggerClient/{Model => Object}/Pet.pm (93%) rename samples/client/petstore/perl/lib/WWW/SwaggerClient/{Model => Object}/Tag.pm (91%) rename samples/client/petstore/perl/lib/WWW/SwaggerClient/{Model => Object}/User.pm (93%) delete mode 100644 samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pl 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 d187bcd901..4edead804d 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,9 +27,9 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { public PerlClientCodegen() { super(); - modelPackage = "Model"; + modelPackage = "Object"; outputFolder = "generated-code/perl"; - modelTemplateFiles.put("model.mustache", ".pm"); + modelTemplateFiles.put("object.mustache", ".pm"); apiTemplateFiles.put("api.mustache", ".pm"); templateDir = "perl"; @@ -60,6 +60,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("map", "map"); supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm")); + supportingFiles.add(new SupportingFile("BaseObject.mustache", "lib/WWW/" + invokerPackage, "Object/BaseObject.pm")); } @Override diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pm b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache similarity index 71% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pm rename to modules/swagger-codegen/src/main/resources/perl/APIClient.mustache index 6f7e645bfd..44d5b60b21 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pm +++ b/modules/swagger-codegen/src/main/resources/perl/APIClient.mustache @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Swagger; +package WWW::{{invokerPackage}}::APIClient; use strict; use warnings; @@ -7,17 +7,22 @@ use utf8; use LWP::UserAgent; use HTTP::Headers; use HTTP::Response; +use HTTP::Request::Common; use HTTP::Status; use URI::Query; use JSON; - +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"; +my $base_url = "{{basePath}}"; sub new @@ -65,7 +70,7 @@ sub call_api { my $_url = $base_url . $resource_path; # build query - if ($query_params) { + if (%$query_params) { $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); } @@ -73,17 +78,30 @@ sub call_api { my $_body_data = $post_params ? $post_params : $body_data; # Make the HTTP request - my $_request = HTTP::Request->new( - $method, $_url, $headers, $_body_data - ); + my $_request; + switch ($method) { + case 'POST' { + #TODO: multipart + $_request = POST($_url, Accept => $header_params->{Accept}, + Content_Type => $header_params->{'Content-Type'}, Content => $_body_data); + } + case 'GET' { + $_request = GET($_url, Accept => $header_params->{'Accept'}, + Content_Type => $header_params->{'Content-Type'}); + } + case 'PUT' { + + } + } $ua->timeout($http_timeout); $ua->agent($http_user_agent); my $_response = $ua->request($_request); - if (!$_response->is_success) { - #TODO croak("Can't connect ot the api ($_response{code}): $_response{message}"); + unless ($_response->is_success) { + croak("Can't connect to the server"); + #croak("Can't connect to the api ($_response{code}): $_response{message}"); } return $_response->content; @@ -179,52 +197,42 @@ sub to_string { } } - # Deserialize a JSON string into an object # -# @param object $object object or primitive to be deserialized # @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 ($data, $class) = @_; -# if (null === $data) { -# $deserialized = null; -# } elseif (substr($class, 0, 4) == 'map[') { -# $inner = substr($class, 4, -1); -# $values = array(); -# if(strrpos($inner, ",") !== false) { -# $subClass_array = explode(',', $inner, 2); -# $subClass = $subClass_array[1]; -# foreach ($data as $key => $value) { -# $values[] = array($key => self::deserialize($value, $subClass)); -# } -# } -# $deserialized = $values; -# } elseif (strcasecmp(substr($class, 0, 6),'array[') == 0) { -# $subClass = substr($class, 6, -1); -# $values = array(); -# foreach ($data as $key => $value) { -# $values[] = self::deserialize($value, $subClass); -# } -# $deserialized = $values; -# } elseif ($class == 'DateTime') { -# $deserialized = new \DateTime($data); -# } elseif (in_array($class, array('string', 'int', 'float', 'bool'))) { -# settype($data, $class); -# $deserialized = $data; -# } else { -# $instance = new $class(); -# foreach ($instance::$swaggerTypes as $property => $type) { -# if (isset($data->$property)) { -# $original_property_name = $instance::$attributeMap[$property]; -# $instance->$property = self::deserialize($data->$original_property_name, $type); -# } -# } -# $deserialized = $instance; -# } -# -# return $deserialized; + 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; + } 1; diff --git a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache new file mode 100644 index 0000000000..be8937a16f --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache @@ -0,0 +1,76 @@ +package WWW::{{invokerPackage}}::Object::BaseObject; + +require 5.6.0; +use strict; +use warnings; +use utf8; +use JSON qw(decode_json); +use Data::Dumper; +use Module::Runtime qw(use_module); +use Log::Any qw($log); +use Date::Parse; +use DateTime; + + +# +# +# +#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. +# + + +# return json string +sub to_hash { + return decode_json(JSON->new->convert_blessed->encode( shift )); +} + +# used by JSON for serialization +sub TO_JSON { + my $self = shift; + my $_data = {}; + foreach my $_key (keys $self->{attribute_map}) { + if (defined $self->{$self->{attribute_map}->{$_key}}) { + $_data->{$self->{attribute_map}->{$_key}} = $self->{$_key}; + } + } + return $_data; +} + +# from json string +sub from_hash { + my ($self, $hash) = @_; + # loop through attributes and use swagger_types to deserialize the data + while ( my ($_key, $_type) = each $self->{swagger_types}) { + if ($_type =~ /^array\[/i) { # array + my $_subclass = substr($_type, 6, -1); + my @_array = (); + foreach my $_element (@{$hash->{$self->{attribute_map}->{$_key}}}) { + push @_array, $self->_deserialize($_subclass, $_element); + } + $self->{$_key} = \@_array; + } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime + $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); + } else { + $log->debugf("warning: %s not defined\n", $_key); + } + } + + return $self; +} + +# deserialize non-array data +sub _deserialize { + my ($self, $type, $data) = @_; + $log->debugf("deserializing %s with %s",Dumper($data), $type); + + if ($type eq 'DateTime') { + return DateTime->from_epoch(epoch => str2time($data)); + } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { + return $data; + } else { # hash(model) + my $_instance = eval "WWW::{{invokerPackage}}::Object::$type->new()"; + return $_instance->from_hash($data); + } +} + +1; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index 56596c5733..7eb16b8a1a 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -98,21 +98,21 @@ sub new { {{#queryParams}} # query params if ( exists $args->{'{{paramName}}'}) { - $query_params->{'{{baseName}}'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'{{paramName}}'}); + $query_params->{'{{baseName}}'} = WWW::{{invokerPacakge}}::APIClient::to_query_value($args->{'{{paramName}}'}); }{{/queryParams}} {{#headerParams}} # header params if ( exists $args->{'{{paramName}}'}) { - $header_params->{'{{baseName}}'} = WWW::SwaggerClient::APIClient::to_header_value($args->{'{{paramName}}'}); + $header_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_header_value($args->{'{{paramName}}'}); }{{/headerParams}} {{#pathParams}} # path params if ( exists $args->{'{{paramName}}'}) { my $_base_variable = "{" . "{{baseName}}" . "}"; - my $_base_value = WWW::SwaggerClient::APIClient::to_path_value($args->{'{{paramName}}'}); + my $_base_value = WWW::{{invokerPackage}}::APIClient::to_path_value($args->{'{{paramName}}'}); $_resource_path =~ s/$_base_variable/$_base_value/g; }{{/pathParams}} {{#formParams}} # form params if ( exists $args->{'{{paramName}}'} ) { - $form_params->{'{{baseName}}'} = {{#isFile}}'@' . {{/isFile}}WWW::SwaggerClient::APIClient::to_form_value($args->{'{{paramName}}'}); + $form_params->{'{{baseName}}'} = {{#isFile}}'@' . {{/isFile}}WWW::{{invokerPackage}}::APIClient::to_form_value($args->{'{{paramName}}'}); }{{/formParams}} my $_body_data; {{#bodyParams}} # body params diff --git a/modules/swagger-codegen/src/main/resources/perl/model.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache similarity index 91% rename from modules/swagger-codegen/src/main/resources/perl/model.mustache rename to modules/swagger-codegen/src/main/resources/perl/object.mustache index 3612ed1050..73b2e749ea 100644 --- a/modules/swagger-codegen/src/main/resources/perl/model.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -1,6 +1,6 @@ {{#models}} {{#model}} -package WWW::{{invokerPackage}}::Model::{{classname}}; +package WWW::{{invokerPackage}}::Object::{{classname}}; require 5.6.0; use strict; @@ -10,7 +10,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::{{invokerPackage}}::Object::BaseObject"; # #{{description}} @@ -89,7 +92,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::{{invokerPackage}}::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm index cf9e85119f..1824600478 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/APIClient.pm @@ -199,7 +199,6 @@ sub to_string { # Deserialize a JSON string into an object # -# @param object $object object or primitive to be deserialized # @param string $class class name is passed as a string # @param string $data data of the body # @return object an instance of $class @@ -211,8 +210,8 @@ sub deserialize if (not defined $data) { return undef; - } elsif (substr($class, 0, 4) eq 'map[') { #TODO map - $_result = $data; + } 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 @@ -228,7 +227,7 @@ sub deserialize } elsif (grep /^$data$/, ('string', 'int', 'float', 'bool')) { #TODO revise the primitive type $_result= $data; } else { # model - my $_instance = use_module("WWW::SwaggerClient::Model::$class")->new; + my $_instance = use_module("WWW::SwaggerClient::Object::$class")->new; $_result = $_instance->from_hash(decode_json $data); } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm new file mode 100644 index 0000000000..276b3f46f0 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm @@ -0,0 +1,76 @@ +package WWW::SwaggerClient::Object::BaseObject; + +require 5.6.0; +use strict; +use warnings; +use utf8; +use JSON qw(decode_json); +use Data::Dumper; +use Module::Runtime qw(use_module); +use Log::Any qw($log); +use Date::Parse; +use DateTime; + + +# +# +# +#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. +# + + +# return json string +sub to_hash { + return decode_json(JSON->new->convert_blessed->encode( shift )); +} + +# used by JSON for serialization +sub TO_JSON { + my $self = shift; + my $_data = {}; + foreach my $_key (keys $self->{attribute_map}) { + if (defined $self->{$self->{attribute_map}->{$_key}}) { + $_data->{$self->{attribute_map}->{$_key}} = $self->{$_key}; + } + } + return $_data; +} + +# from json string +sub from_hash { + my ($self, $hash) = @_; + # loop through attributes and use swagger_types to deserialize the data + while ( my ($_key, $_type) = each $self->{swagger_types}) { + if ($_type =~ /^array\[/i) { # array + my $_subclass = substr($_type, 6, -1); + my @_array = (); + foreach my $_element (@{$hash->{$self->{attribute_map}->{$_key}}}) { + push @_array, $self->_deserialize($_subclass, $_element); + } + $self->{$_key} = \@_array; + } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime + $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); + } else { + $log->debugf("warning: %s not defined\n", $_key); + } + } + + return $self; +} + +# deserialize non-array data +sub _deserialize { + my ($self, $type, $data) = @_; + $log->debugf("deserializing %s with %s",Dumper($data), $type); + + if ($type eq 'DateTime') { + return DateTime->from_epoch(epoch => str2time($data)); + } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { + return $data; + } else { # hash(model) + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; + return $_instance->from_hash($data); + } +} + +1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Category.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm similarity index 91% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Category.pm rename to samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm index 03e22e073a..b82bd5f82a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Category.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Model::Category; +package WWW::SwaggerClient::Object::Category; require 5.6.0; use strict; @@ -8,7 +8,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::SwaggerClient::Object::BaseObject"; # # @@ -88,7 +91,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Order.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm similarity index 93% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Order.pm rename to samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm index 7bfc8e4a85..35a6e96fac 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Order.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Model::Order; +package WWW::SwaggerClient::Object::Order; require 5.6.0; use strict; @@ -8,7 +8,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::SwaggerClient::Object::BaseObject"; # # @@ -104,7 +107,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Pet.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm similarity index 93% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Pet.pm rename to samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm index 1b41f6fd88..1e692d7bb5 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Pet.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Model::Pet; +package WWW::SwaggerClient::Object::Pet; require 5.6.0; use strict; @@ -8,7 +8,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::SwaggerClient::Object::BaseObject"; # # @@ -104,7 +107,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Tag.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm similarity index 91% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Tag.pm rename to samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm index eb838d1d7e..b46b44927f 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/Tag.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Model::Tag; +package WWW::SwaggerClient::Object::Tag; require 5.6.0; use strict; @@ -8,7 +8,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::SwaggerClient::Object::BaseObject"; # # @@ -88,7 +91,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/User.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm similarity index 93% rename from samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/User.pm rename to samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm index 4a431457ab..08d9ff6b08 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Model/User.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm @@ -1,4 +1,4 @@ -package WWW::SwaggerClient::Model::User; +package WWW::SwaggerClient::Object::User; require 5.6.0; use strict; @@ -8,7 +8,10 @@ use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); +use Date::Parse; +use DateTime; +use base "WWW::SwaggerClient::Object::BaseObject"; # # @@ -112,7 +115,7 @@ sub _deserialize { } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { return $data; } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Model::$type->new()"; + my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; return $_instance->from_hash($data); } } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 4771c55988..8b02083e63 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -201,7 +201,7 @@ sub new { # query params if ( exists $args->{'status'}) { - $query_params->{'status'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'status'}); + $query_params->{'status'} = WWW::::APIClient::to_query_value($args->{'status'}); } @@ -255,7 +255,7 @@ sub new { # query params if ( exists $args->{'tags'}) { - $query_params->{'tags'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'tags'}); + $query_params->{'tags'} = WWW::::APIClient::to_query_value($args->{'tags'}); } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pl b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pl deleted file mode 100644 index a5a82fcbd3..0000000000 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Swagger.pl +++ /dev/null @@ -1,230 +0,0 @@ -package WWW::SwaggerClient::Swagger; - -use strict; -use warnings; -use utf8; - -use LWP::UserAgent; -use HTTP::Headers; -use HTTP::Response; -use HTTP::Status; -use URI::Query; -use JSON; - -use Scalar::Util; - -# class variables -my $ua = LWP::UserAgent->new; -my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent -my $http_timeout; #timeout -my $base_url; - - -sub new -{ - my $class = shift; - my %args = @_; - - 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 $user_agent = shift; - $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 $seconds = shift; - if (!looks_like_number($seconds)) { - croak('Timeout variable must be numeric.'); - } - $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) = @_; - - my $headers = HTTP::Headers->new(%$header_params); - - my $_url = $base_url . $resource_path; - - # build query - if ($query_params) { - $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); - } - - # body data - my $_body_data = $post_params ? $post_params : $body_data; - - # Make the HTTP request - my $_request = HTTP::Request->new( - $method, $_url, $headers, $_body_data - ); - - $ua->timeout($http_timeout); - $ua->agent($http_user_agent); - - my $_response = $ua->request($_request); - - if (!$_response->is_success) { - #TODO croak("Can't connect ot the api ($_response{code}): $_response{message}"); - } - - return $_response->content; - -} - - -# 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)); -} - - -# 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 $object = shift; - if (is_array($object)) { - return implode(',', $object); - } else { - return toString($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 $value = shift; - return 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 $value = shift; - return 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 $value = shift; - if (ref($value) eq "DateTime") { # datetime in ISO8601 format - return $value->datetime(); - } - else { - return $value; - } -} - - -# Deserialize a JSON string into an object -# -# @param object $object object or primitive to be deserialized -# @param string $class class name is passed as a string -# @return object an instance of $class -#sub deserialize -#{ -# my ($data, $class) = @_; -# if (null === $data) { -# $deserialized = null; -# } elseif (substr($class, 0, 4) == 'map[') { -# $inner = substr($class, 4, -1); -# $values = array(); -# if(strrpos($inner, ",") !== false) { -# $subClass_array = explode(',', $inner, 2); -# $subClass = $subClass_array[1]; -# foreach ($data as $key => $value) { -# $values[] = array($key => self::deserialize($value, $subClass)); -# } -# } -# $deserialized = $values; -# } elseif (strcasecmp(substr($class, 0, 6),'array[') == 0) { -# $subClass = substr($class, 6, -1); -# $values = array(); -# foreach ($data as $key => $value) { -# $values[] = self::deserialize($value, $subClass); -# } -# $deserialized = $values; -# } elseif ($class == 'DateTime') { -# $deserialized = new \DateTime($data); -# } elseif (in_array($class, array('string', 'int', 'float', 'bool'))) { -# settype($data, $class); -# $deserialized = $data; -# } else { -# $instance = new $class(); -# foreach ($instance::$swaggerTypes as $property => $type) { -# if (isset($data->$property)) { -# $original_property_name = $instance::$attributeMap[$property]; -# $instance->$property = self::deserialize($data->$original_property_name, $type); -# } -# } -# $deserialized = $instance; -# } -# -# return $deserialized; -#} - -1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index 3582520cd5..a8a52ae48a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -253,10 +253,10 @@ sub new { # query params if ( exists $args->{'username'}) { - $query_params->{'username'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'username'}); + $query_params->{'username'} = WWW::::APIClient::to_query_value($args->{'username'}); } # query params if ( exists $args->{'password'}) { - $query_params->{'password'} = WWW::SwaggerClient::APIClient::to_query_value($args->{'password'}); + $query_params->{'password'} = WWW::::APIClient::to_query_value($args->{'password'}); } diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index f415dc36c0..25ac786166 100644 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -6,22 +6,22 @@ use strict; use warnings; use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::APIClient; -use WWW::SwaggerClient::Model::Pet; -use WWW::SwaggerClient::Model::Tag; -use WWW::SwaggerClient::Model::Category; +use WWW::SwaggerClient::Object::Pet; +use WWW::SwaggerClient::Object::Tag; +use WWW::SwaggerClient::Object::Category; use JSON; use Data::Dumper; my $api = WWW::SwaggerClient::PetApi->new(); -print WWW::SwaggerClient::APIClient::to_form_value('testing 123'); +#print WWW::SwaggerClient::APIClient::to_form_value('testing 123'); my $pet_id = 5; -my $tag = WWW::SwaggerClient::Model::Tag->new({'id' => 'tag1', 'name' => 'just kidding', +my $tag = WWW::SwaggerClient::Object::Tag->new({'id' => 'tag1', 'name' => 'just kidding', "photoUrls" => ['123', 'oop']}); -my $pet = WWW::SwaggerClient::Model::Pet->new({'id' => 5, 'name' => 'haha', - "photoUrls" => ['123', 'oop'], 'tags' => [$tag]}); +my $pet = WWW::SwaggerClient::Object::Pet->new({'id' => 5, 'name' => 'haha', + "photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'created' => '2003-04-05 02:58:00'}); ##print Dumper $pet; @@ -29,11 +29,9 @@ my $json = JSON->new->convert_blessed; #print $json->convert_blessed->encode($pet); #print $json->get_convert_blessed; -##print Dumper($pet->to_hash); - - +print Dumper($pet->to_hash); #my $pet2 = WWW::SwaggerClient::Model::Pet->from_json($pet->to_hash); -my $pet2 = WWW::SwaggerClient::Model::Pet->new(); +my $pet2 = WWW::SwaggerClient::Object::Pet->new(); $pet2 = $pet2->from_hash($pet->to_hash); #$pet2->from_json($pet->to_hash); ##print Dumper($pet2->to_hash);