mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
add inheritance to object (model)
This commit is contained in:
parent
07cd23edac
commit
0a34793f5a
@ -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
|
||||
|
@ -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;
|
@ -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;
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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'});
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
@ -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'});
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user