Merge pull request #1502 from dvz5/master

Rebuild perl petstore client after previous updates
This commit is contained in:
wing328
2015-11-05 11:59:11 +08:00
18 changed files with 359 additions and 205 deletions
@@ -94,6 +94,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiClient.pm"));
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm"));
supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Object/BaseObject.pm"));
supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiFactory.pm"));
}
public CodegenType getTag() {
@@ -118,10 +118,12 @@ sub call_api {
$self->{ua}->timeout($self->{http_timeout} || $WWW::{{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{moduleName}}::Configuration::http_user_agent);
$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
$log->debugf("RESPONSE: %s", $_response->as_string);
unless ($_response->is_success) {
croak("API Exception(".$_response->code."): ".$_response->message);
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
}
return $_response->content;
@@ -294,13 +296,23 @@ sub get_api_key_with_prefix
}
}
# update hearder and query param based on authentication setting
# update header 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) = @_;
# we can defer to the application
if ($self->{auth_setup_handler} && ref($self->{auth_setup_handler}) eq 'CODE') {
$self->{auth_setup_handler}->( api_client => $self,
header_params => $header_params,
query_params => $query_params,
auth_settings => $auth_settings, # presumably this won't be defined if we're doing it this way
);
return;
}
return if (!defined($auth_settings) || scalar(@$auth_settings) == 0);
@@ -0,0 +1,98 @@
package WWW::{{moduleName}}::ApiFactory;
use strict;
use warnings;
use utf8;
use Carp;
use Module::Find;
usesub WWW::{{moduleName}}::Object;
use WWW::{{moduleName}}::ApiClient;
=head1 Name
WWW::{{moduleName}}::ApiFactory - constructs APIs to retrieve {{moduleName}} objects
=head1 Synopsis
package My::Petstore::App;
use WWW::{{moduleName}}::ApiFactory;
my $api_factory = WWW::{{moduleName}}::ApiFactory->new( base_url => 'http://petstore.swagger.io/v2',
..., # other args for ApiClient constructor
);
# later...
my $pet_api = $api_factory->get_api('Pet');
# $pet_api isa WWW::{{moduleName}}::PetApi
my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id);
# object attributes have proper accessors:
printf "Pet's name is %s", $pet->name;
# change the value stored on the object:
$pet->name('Dave');
=cut
# Load all the API classes and construct a lookup table at startup time
my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::{{moduleName}}';
=head1 new()
All parameters are optional, and are passed to and stored on the api_client object.
base_url: supply this to change the default base URL taken from the Swagger definition.
auth_setup_handler: a coderef you can supply to set up authentication.
The coderef receives a hashref with keys: api_client, query_params, header_params, auth_settings.
my $api_factory = WWW::{{moduleName}}::ApiFactory->new( auth_setup_handler => \&setup_auth ); );
sub setup_auth {
my %p = @_;
$p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd';
}
=cut
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p);
return bless \%p, $class;
}
=head1 get_api($which)
Returns an API object of the requested type.
$which is a nickname for the class:
WWW::FooBarClient::BazApi has nickname 'Baz'
=cut
sub get_api {
my ($self, $which) = @_;
croak "API not specified" unless $which;
my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'";
return $api_class->new(api_client => $self->api_client);
}
=head1 api_client()
Returns the api_client object, should you ever need it.
=cut
sub api_client { $_[0]->{api_client} }
1;
@@ -11,6 +11,8 @@ use Log::Any qw($log);
use Date::Parse;
use DateTime;
use base ("Class::Accessor", "Class::Data::Inheritable");
#
#
@@ -18,6 +20,22 @@ use DateTime;
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
__PACKAGE__->mk_classdata('attribute_map');
__PACKAGE__->mk_classdata('swagger_types');
# new object
sub new {
my ($class, %args) = @_;
my $self = bless {}, $class;
foreach my $attribute (keys %{$class->attribute_map}) {
my $args_key = $class->attribute_map->{$attribute};
$self->$attribute( $args{ $args_key } );
}
return $self;
}
# return perl hash
sub to_hash {
@@ -28,9 +46,9 @@ sub to_hash {
sub TO_JSON {
my $self = shift;
my $_data = {};
foreach my $_key (keys %{$self->get_attribute_map}) {
foreach my $_key (keys %{$self->attribute_map}) {
if (defined $self->{$_key}) {
$_data->{$self->get_attribute_map->{$_key}} = $self->{$_key};
$_data->{$self->attribute_map->{$_key}} = $self->{$_key};
}
}
return $_data;
@@ -41,8 +59,8 @@ sub from_hash {
my ($self, $hash) = @_;
# loop through attributes and use swagger_types to deserialize the data
while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) {
my $_json_attribute = $self->get_attribute_map->{$_key};
while ( my ($_key, $_type) = each %{$self->swagger_types} ) {
my $_json_attribute = $self->attribute_map->{$_key};
if ($_type =~ /^array\[/i) { # array
my $_subclass = substr($_type, 6, -1);
my @_array = ();
@@ -100,7 +100,7 @@ sub {{nickname}} {
{{#formParams}}# form params
if ( exists $args{'{{paramName}}'} ) {
{{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'};
push $form_params->{'{{baseName}}'}, $args{'{{paramName}}'};
push @{$form_params->{'{{baseName}}'}}, $args{'{{paramName}}'};
{{/isFile}}
{{^isFile}}$form_params->{'{{baseName}}'} = $self->{api_client}->to_form_value($args{'{{paramName}}'});
{{/isFile}}
@@ -21,37 +21,17 @@ use base "WWW::{{moduleName}}::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
{{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
{{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
{{#vars}}#{{#description}}{{{description}}}{{/description}}
'{{name}}' => $args{'{{baseName}}'}{{#hasMore}},
{{/hasMore}}{{/vars}}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
{{/model}}
@@ -118,10 +118,12 @@ sub call_api {
$self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent);
$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
$log->debugf("RESPONSE: %s", $_response->as_string);
unless ($_response->is_success) {
croak("API Exception(".$_response->code."): ".$_response->message);
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
}
return $_response->content;
@@ -153,6 +155,7 @@ sub to_query_value {
}
}
# 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
@@ -293,13 +296,23 @@ sub get_api_key_with_prefix
}
}
# update hearder and query param based on authentication setting
# update header 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) = @_;
# we can defer to the application
if ($self->{auth_setup_handler} && ref($self->{auth_setup_handler}) eq 'CODE') {
$self->{auth_setup_handler}->( api_client => $self,
header_params => $header_params,
query_params => $query_params,
auth_settings => $auth_settings, # presumably this won't be defined if we're doing it this way
);
return;
}
return if (!defined($auth_settings) || scalar(@$auth_settings) == 0);
@@ -0,0 +1,98 @@
package WWW::SwaggerClient::ApiFactory;
use strict;
use warnings;
use utf8;
use Carp;
use Module::Find;
usesub WWW::SwaggerClient::Object;
use WWW::SwaggerClient::ApiClient;
=head1 Name
WWW::SwaggerClient::ApiFactory - constructs APIs to retrieve SwaggerClient objects
=head1 Synopsis
package My::Petstore::App;
use WWW::SwaggerClient::ApiFactory;
my $api_factory = WWW::SwaggerClient::ApiFactory->new( base_url => 'http://petstore.swagger.io/v2',
..., # other args for ApiClient constructor
);
# later...
my $pet_api = $api_factory->get_api('Pet');
# $pet_api isa WWW::SwaggerClient::PetApi
my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id);
# object attributes have proper accessors:
printf "Pet's name is %s", $pet->name;
# change the value stored on the object:
$pet->name('Dave');
=cut
# Load all the API classes and construct a lookup table at startup time
my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::SwaggerClient';
=head1 new()
All parameters are optional, and are passed to and stored on the api_client object.
base_url: supply this to change the default base URL taken from the Swagger definition.
auth_setup_handler: a coderef you can supply to set up authentication.
The coderef receives a hashref with keys: api_client, query_params, header_params, auth_settings.
my $api_factory = WWW::SwaggerClient::ApiFactory->new( auth_setup_handler => \&setup_auth ); );
sub setup_auth {
my %p = @_;
$p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd';
}
=cut
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::SwaggerClient::ApiClient->new(%p);
return bless \%p, $class;
}
=head1 get_api($which)
Returns an API object of the requested type.
$which is a nickname for the class:
WWW::FooBarClient::BazApi has nickname 'Baz'
=cut
sub get_api {
my ($self, $which) = @_;
croak "API not specified" unless $which;
my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'";
return $api_class->new(api_client => $self->api_client);
}
=head1 api_client()
Returns the api_client object, should you ever need it.
=cut
sub api_client { $_[0]->{api_client} }
1;
@@ -11,6 +11,8 @@ use Log::Any qw($log);
use Date::Parse;
use DateTime;
use base ("Class::Accessor", "Class::Data::Inheritable");
#
#
@@ -18,8 +20,24 @@ use DateTime;
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
__PACKAGE__->mk_classdata('attribute_map');
__PACKAGE__->mk_classdata('swagger_types');
# return json string
# new object
sub new {
my ($class, %args) = @_;
my $self = bless {}, $class;
foreach my $attribute (keys %{$class->attribute_map}) {
my $args_key = $class->attribute_map->{$attribute};
$self->$attribute( $args{ $args_key } );
}
return $self;
}
# return perl hash
sub to_hash {
return decode_json(JSON->new->convert_blessed->encode( shift ));
}
@@ -28,36 +46,38 @@ sub to_hash {
sub TO_JSON {
my $self = shift;
my $_data = {};
foreach my $_key (keys %{$self->get_attribute_map}) {
foreach my $_key (keys %{$self->attribute_map}) {
if (defined $self->{$_key}) {
$_data->{$self->get_attribute_map->{$_key}} = $self->{$_key};
$_data->{$self->attribute_map->{$_key}} = $self->{$_key};
}
}
return $_data;
}
# from json string
# from Perl hashref
sub from_hash {
my ($self, $hash) = @_;
# loop through attributes and use swagger_types to deserialize the data
while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) {
while ( my ($_key, $_type) = each %{$self->swagger_types} ) {
my $_json_attribute = $self->attribute_map->{$_key};
if ($_type =~ /^array\[/i) { # array
my $_subclass = substr($_type, 6, -1);
my @_array = ();
foreach my $_element (@{$hash->{$self->get_attribute_map->{$_key}}}) {
foreach my $_element (@{$hash->{$_json_attribute}}) {
push @_array, $self->_deserialize($_subclass, $_element);
}
$self->{$_key} = \@_array;
} elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime
$self->{$_key} = $self->_deserialize($_type, $hash->{$_key});
} elsif (exists $hash->{$_json_attribute}) { #hash(model), primitive, datetime
$self->{$_key} = $self->_deserialize($_type, $hash->{$_json_attribute});
} else {
$log->debugf("warning: %s not defined\n", $_key);
$log->debugf("Warning: %s (%s) does not exist in input hash\n", $_key, $_json_attribute);
}
}
return $self;
}
# deserialize non-array data
sub _deserialize {
my ($self, $type, $data) = @_;
@@ -19,37 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
'id' => 'int',
'name' => 'string'
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
'id' => 'id',
'name' => 'name'
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
#
'id' => $args{'id'},
#
'name' => $args{'name'}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
@@ -19,53 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
'id' => 'int',
'pet_id' => 'int',
'quantity' => 'int',
'ship_date' => 'DateTime',
'status' => 'string',
'complete' => 'boolean'
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
'id' => 'id',
'pet_id' => 'petId',
'quantity' => 'quantity',
'ship_date' => 'shipDate',
'status' => 'status',
'complete' => 'complete'
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
#
'id' => $args{'id'},
#
'pet_id' => $args{'petId'},
#
'quantity' => $args{'quantity'},
#
'ship_date' => $args{'shipDate'},
#Order Status
'status' => $args{'status'},
#
'complete' => $args{'complete'}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
@@ -19,53 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
'id' => 'int',
'category' => 'Category',
'name' => 'string',
'photo_urls' => 'ARRAY[string]',
'tags' => 'ARRAY[Tag]',
'status' => 'string'
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
'id' => 'id',
'category' => 'category',
'name' => 'name',
'photo_urls' => 'photoUrls',
'tags' => 'tags',
'status' => 'status'
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
#
'id' => $args{'id'},
#
'category' => $args{'category'},
#
'name' => $args{'name'},
#
'photo_urls' => $args{'photoUrls'},
#
'tags' => $args{'tags'},
#pet status in the store
'status' => $args{'status'}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
@@ -19,37 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
'id' => 'int',
'name' => 'string'
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
'id' => 'id',
'name' => 'name'
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
#
'id' => $args{'id'},
#
'name' => $args{'name'}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
@@ -19,7 +19,7 @@ use base "WWW::SwaggerClient::Object::BaseObject";
#NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
#
my $swagger_types = {
__PACKAGE__->swagger_types( {
'id' => 'int',
'username' => 'string',
'first_name' => 'string',
@@ -28,9 +28,9 @@ my $swagger_types = {
'password' => 'string',
'phone' => 'string',
'user_status' => 'int'
};
} );
my $attribute_map = {
__PACKAGE__->attribute_map( {
'id' => 'id',
'username' => 'username',
'first_name' => 'firstName',
@@ -39,41 +39,8 @@ my $attribute_map = {
'password' => 'password',
'phone' => 'phone',
'user_status' => 'userStatus'
};
} );
# new object
sub new {
my ($class, %args) = @_;
my $self = {
#
'id' => $args{'id'},
#
'username' => $args{'username'},
#
'first_name' => $args{'firstName'},
#
'last_name' => $args{'lastName'},
#
'email' => $args{'email'},
#
'password' => $args{'password'},
#
'phone' => $args{'phone'},
#User Status
'user_status' => $args{'userStatus'}
};
return bless $self, $class;
}
# get swagger type of the attribute
sub get_swagger_types {
return $swagger_types;
}
# get attribute mappping
sub get_attribute_map {
return $attribute_map;
}
__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map});
1;
@@ -300,7 +300,7 @@ sub get_pet_by_id {
# authentication setting, if any
my $auth_settings = ['api_key', 'petstore_auth'];
my $auth_settings = ['api_key'];
# make the API Call
my $response = $self->{api_client}->call_api($_resource_path, $_method,
@@ -495,7 +495,7 @@ sub upload_file {
}# form params
if ( exists $args{'file'} ) {
$form_params->{'file'} = [] unless defined $form_params->{'file'};
push $form_params->{'file'}, $args{'file'};
push @{$form_params->{'file'}}, $args{'file'};
}
+13
View File
@@ -52,6 +52,19 @@
</arguments>
</configuration>
</execution>
<execution>
<id>Test::More for ApiFactory</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>perl</executable>
<arguments>
<argument>t/03_api_factory.t</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
+1 -1
View File
@@ -1,4 +1,4 @@
use Test::More tests => 35;
use Test::More tests => 37;
use Test::Exception;
use lib 'lib';
@@ -0,0 +1,34 @@
use Test::More tests => 13;
use Test::Exception;
use lib 'lib';
use strict;
use warnings;
use_ok('WWW::SwaggerClient::ApiFactory');
my $api_factory = WWW::SwaggerClient::ApiFactory->new('base_url' => 'http://testing');
my $pet_api = $api_factory->get_api('Pet');
isa_ok($pet_api, 'WWW::SwaggerClient::PetApi');
is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client';
$api_factory = WWW::SwaggerClient::ApiFactory->new;
$pet_api = $api_factory->get_api('Pet');
is $pet_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client';
# test accessor methods
my $pet_id = 10008;
# note - we don't need to 'use' these modules because they've already been loaded by ApiFactory
my ($category, $tag, $pet);
lives_ok { $category = WWW::SwaggerClient::Object::Category->new('id' => '22', 'name' => 'perl') } 'Category.pm loaded OK';
lives_ok { $tag = WWW::SwaggerClient::Object::Tag->new('id' => '11', 'name' => 'just kidding') } 'Tag.pm loaded OK';
lives_ok { $pet = WWW::SwaggerClient::Object::Pet->new('id' => $pet_id, 'name' => 'perl test',
"photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'status' => 'pending', 'category' => $category) } 'Pet.pm loaded OK';
is $pet->id, '10008', 'got the proper pet id';
is $pet->name, 'perl test', 'got the proper pet name';
is $pet->category->id, '22', 'got the proper category id';
is $pet->category->name, 'perl', 'got the proper category name';
is $pet->tags->[0]->name, 'just kidding', 'got the proper tag name';
is $pet->tags->[0]->id, '11', 'got the proper tag id';