Merge pull request #1812 from captin411/perl-flexible-module-name

[Perl] more flexible perl module naming (Allow::This even without WWW::)
This commit is contained in:
wing328
2016-01-07 10:37:45 +08:00
19 changed files with 200 additions and 93 deletions

View File

@@ -8,7 +8,7 @@ WWW::SwaggerClient::Role - a Moose role for the Swagger Petstore
Automatically generated by the Perl Swagger Codegen project:
- Build date: 2015-12-29T16:27:28.775+08:00
- Build date: 2016-01-04T20:36:07.450-08:00
- Build package: class io.swagger.codegen.languages.PerlClientCodegen
- Codegen version:
@@ -167,7 +167,7 @@ maven 3.0.3 or better already installed.
The config file should specify the project name for the generated library:
{"moduleName":"MyProjectName"}
{"moduleName":"WWW::MyProjectName"}
Your library files will be built under `WWW::MyProjectName`.

View File

@@ -319,6 +319,12 @@ sub update_params_for_auth {
if (!defined($auth)) {
# TODO show warning about auth setting not defined
}
elsif ($auth eq 'petstore_auth') {
if ($WWW::SwaggerClient::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token;
}
}
elsif ($auth eq 'api_key') {
my $api_key = $self->get_api_key_with_prefix('api_key');
@@ -326,12 +332,6 @@ sub update_params_for_auth {
$header_params->{'api_key'} = $api_key;
}
}
elsif ($auth eq 'petstore_auth') {
if ($WWW::SwaggerClient::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token;
}
}
else {
# TODO show warning about security definition not found
@@ -341,7 +341,7 @@ sub update_params_for_auth {
# The endpoint API class has not found any settings for auth. This may be deliberate,
# in which case update_params_for_auth() will be a no-op. But it may also be that the
# swagger spec does not describe the intended authorization. So we check in the config for any
# OpenAPI Spec does not describe the intended authorization. So we check in the config for any
# auth tokens and if we find any, we use them for all endpoints;
sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_;

View File

@@ -13,7 +13,7 @@ use WWW::SwaggerClient::ApiClient;
=head1 Name
WWW::SwaggerClient::ApiFactory - constructs APIs to retrieve SwaggerClient objects
WWW::SwaggerClient::ApiFactory - constructs APIs to retrieve WWW::SwaggerClient objects
=head1 Synopsis
@@ -64,7 +64,7 @@ sub new {
$which is a nickname for the class:
WWW::FooBarClient::BazApi has nickname 'Baz'
FooBarClient::BazApi has nickname 'Baz'
=cut

View File

@@ -37,7 +37,7 @@ has version_info => ( is => 'ro',
default => sub { {
app_name => 'Swagger Petstore',
app_version => '1.0.0',
generated_date => '2015-12-29T16:27:28.775+08:00',
generated_date => '2016-01-04T20:36:07.450-08:00',
generator_class => 'class io.swagger.codegen.languages.PerlClientCodegen',
} },
documentation => 'Information about the application version and the codegen codebase version'
@@ -103,7 +103,7 @@ Automatically generated by the Perl Swagger Codegen project:
=over 4
=item Build date: 2015-12-29T16:27:28.775+08:00
=item Build date: 2016-01-04T20:36:07.450-08:00
=item Build package: class io.swagger.codegen.languages.PerlClientCodegen
@@ -119,7 +119,7 @@ WWW::SwaggerClient::ApiFactory for non-Moosey usage.
=head1 SYNOPSIS
The Perl Swagger Codegen project builds a library of Perl modules to interact with
a web service defined by a Swagger specification. See below for how to build the
a web service defined by a OpenAPI Specification. See below for how to build the
library.
This module provides an interface to the generated library. All the classes,
@@ -154,7 +154,7 @@ For documentation of all these methods, see AUTOMATIC DOCUMENTATION below.
=head2 Configuring authentication
In the normal case, the Swagger spec will describe what parameters are
In the normal case, the OpenAPI Spec will describe what parameters are
required and where to put them. You just need to supply the tokens.
my $tokens = {
@@ -269,7 +269,7 @@ maven 3.0.3 or better already installed.
The config file should specify the project name for the generated library:
{"moduleName":"MyProjectName"}
{"moduleName":"WWW::MyProjectName"}
Your library files will be built under C<WWW::MyProjectName>.
@@ -304,7 +304,7 @@ output formats are supported:
The C<-c> option allows you to load and inspect your own application. A dummy
namespace is used if you don't supply your own class.
=head1 DOCUMENTATION FROM THE SWAGGER SPEC
=head1 DOCUMENTATION FROM THE OpenAPI Spec
Additional documentation for each class and method may be provided by the Swagger
spec. If so, this is available via the C<class_documentation()> and

View File

@@ -424,4 +424,4 @@ $attrname
1;
1;

View File

@@ -0,0 +1,96 @@
use Test::More;
use Test::Exception;
use lib 'deep_module_test/lib';
use strict;
use warnings;
if (! -d 'deep_module_test/lib' ) {
plan skip_all => 'bin/perl-petstore.sh needs to be run first';
}
else {
plan tests => 38;
}
use_ok('Something::Deep::PetApi');
use_ok('Something::Deep::ApiClient');
use_ok('Something::Deep::Object::Pet');
use_ok('Something::Deep::Object::Tag');
use_ok('Something::Deep::Object::Category');
my $api_client = Something::Deep::ApiClient->instance('base_url' => 'http://testing');
my $pet_api = Something::Deep::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 = Something::Deep::PetApi->new();
is $api->{api_client}->{base_url}, 'http://testing', 'we still get the original base URL from api client, because it\'s a singleton';
# reset the base_url - no direct access because an application shouldn't be changing
# its base URL halfway through
$api->{api_client}->{base_url} = 'http://petstore.swagger.io/v2';
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';
is $api->{api_client}->select_header_content_type(''), 'application/json', 'get the proper content type application/json (default)';
# test select_header_accept
is $api->{api_client}->select_header_accept('application/xml', 'Application/JSON'), 'application/json', 'get the proper accept application/json but not application/xml';
is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper accept application/json';
is $api->{api_client}->select_header_accept(''), undef, 'get the proper accept "undef" (default)';
my $pet_id = 10008;
my $category = Something::Deep::Object::Category->new('id' => '22', 'name' => 'perl');
my $tag = Something::Deep::Object::Tag->new('id' => '11', 'name' => 'just kidding');
my $pet = Something::Deep::Object::Pet->new('id' => $pet_id, 'name' => 'perl test',
"photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'status' => 'pending', 'category' => $category);
isa_ok($api, 'Something::Deep::PetApi');
isa_ok($category, 'Something::Deep::Object::Category');
isa_ok($tag, 'Something::Deep::Object::Tag');
isa_ok($pet, 'Something::Deep::Object::Pet');
my $pet_hash = $pet->to_hash;
is $pet_hash->{category}->{id}, '22', 'get the proper category id';
is $pet_hash->{category}->{name}, 'perl', 'get the proper category name';
is $pet_hash->{tags}[0]->{name}, 'just kidding', 'get the proper tag name';
is $pet_hash->{tags}[0]->{id}, '11', 'get the proper tag id';
my $add_pet = $api->add_pet(body => $pet);
my $get_pet = $api->get_pet_by_id(pet_id => $pet_id);
my $get_pet_hash = $get_pet->to_hash;
is $get_pet_hash->{name}, 'perl test', 'get the proper pet name from get_pet_by_id';
is $get_pet_hash->{id}, '10008', 'get the proper pet id from get_pet_by_id';
is $get_pet_hash->{category}->{name}, 'perl', 'get the proper category name from get_pet_by_id';
is $get_pet_hash->{category}->{id}, '22', 'get the proper category id from get_pet_by_id';
is $get_pet_hash->{category}->{name}, 'perl', 'get the proper category from get_pet_by_id';
is $get_pet_hash->{tags}[0]->{name}, 'just kidding', 'get the proper tag from get_pet_by_id';
is $get_pet_hash->{tags}[0]->{id}, '11', 'get the proper tag id from get_pet_by_id';
is $get_pet_hash->{photoUrls}->[0], '123', 'get the proper photoUrl from get_pet_by_id';
is $get_pet_hash->{photoUrls}->[1], 'oop', 'get the proper photoUrl from get_pet_by_id';
my $update_pet_with_form = $api->update_pet_with_form(pet_id => $pet_id, name => 'test_name', status => 'sold');
is $update_pet_with_form, undef, 'get the null response from update_pet_wth_form';
my $get_pet_after_update = $api->get_pet_by_id(pet_id => $pet_id);
is $get_pet_after_update->{status}, 'sold', 'get the updated status after update_pet_with_form';
my $upload_pet = $api->upload_file(pet_id => $pet_id, additional_metadata => 'testabc', file => 'test.pl');
is $upload_pet, undef, 'get the null response from upload_pet';
my $delete_pet = $api->delete_pet(pet_id => $pet_id);
is $delete_pet, undef, 'get the null response from delete_pet';
throws_ok{$api->get_pet_by_id(pet_id => $pet_id)} qr/API Exception\(404\): Not Found/, "throw 404 error about pet not found after delete";
#is $get_pet_after_delete->{status}, undef, 'get the updated status after update_pet_with_form';
my $pets;
lives_ok {$pets = $api->find_pets_by_status(status => [qw(sold available)])} 'array query param processed correctly';
isa_ok($pets->[0], 'Something::Deep::Object::Pet');