[Perl] remove singleton (#5353)

- removed Singleton from ApiClient

fixes #5336
This commit is contained in:
tbe 2017-04-10 16:52:32 +02:00 committed by wing328
parent c35fdc3bda
commit d59ac2ae65
27 changed files with 503 additions and 362 deletions

View File

@ -26,22 +26,25 @@ use Module::Runtime qw(use_module);
use {{moduleName}}::Configuration; use {{moduleName}}::Configuration;
use base 'Class::Singleton';
sub _new_instance sub new {
{
my $class = shift; my $class = shift;
my $config;
if ( $_[0] && ref $_[0] && ref $_[0] eq '{{moduleName}}::Configuration' ) {
$config = $_[0];
} else {
$config = {{moduleName}}::Configuration->new(@_);
}
my (%args) = ( my (%args) = (
'ua' => LWP::UserAgent->new, 'ua' => LWP::UserAgent->new,
'base_url' => '{{{basePath}}}', 'config' => $config,
@_
); );
return bless \%args, $class; return bless \%args, $class;
} }
sub _cfg {'{{moduleName}}::Configuration'}
# Set the user agent of the API client # Set the user agent of the API client
# #
# @param string $user_agent The user agent of the API client # @param string $user_agent The user agent of the API client
@ -78,7 +81,7 @@ sub call_api {
$self->update_params_for_auth($header_params, $query_params, $auth_settings); $self->update_params_for_auth($header_params, $query_params, $auth_settings);
my $_url = $self->{base_url} . $resource_path; my $_url = $self->{config}{base_url} . $resource_path;
# build query # build query
if (%$query_params) { if (%$query_params) {
@ -125,8 +128,8 @@ sub call_api {
else { else {
} }
$self->{ua}->timeout($self->{http_timeout} || ${{moduleName}}::Configuration::http_timeout); $self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
$self->{ua}->agent($self->{http_user_agent} || ${{moduleName}}::Configuration::http_user_agent); $self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});
$log->debugf("REQUEST: %s", $_request->as_string); $log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request); my $_response = $self->{ua}->request($_request);
@ -300,11 +303,11 @@ sub get_api_key_with_prefix
{ {
my ($self, $key_name) = @_; my ($self, $key_name) = @_;
my $api_key = ${{moduleName}}::Configuration::api_key->{$key_name}; my $api_key = $self->{config}{api_key}{$key_name};
return unless $api_key; return unless $api_key;
my $prefix = ${{moduleName}}::Configuration::api_key_prefix->{$key_name}; my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key; return $prefix ? "$prefix $api_key" : $api_key;
} }
@ -335,11 +338,11 @@ sub update_params_for_auth {
if ($api_key) { if ($api_key) {
$query_params->{'{{keyParamName}}'} = $api_key; $query_params->{'{{keyParamName}}'} = $api_key;
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}} }{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
if (${{moduleName}}::Configuration::username || ${{moduleName}}::Configuration::password) { if ($self->{config}{username} || $self->{config}{password}) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64(${{moduleName}}::Configuration::username . ":" . ${{moduleName}}::Configuration::password); $header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
}{{/isBasic}}{{#isOAuth}} }{{/isBasic}}{{#isOAuth}}
if (${{moduleName}}::Configuration::access_token) { if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . ${{moduleName}}::Configuration::access_token; $header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}{{/isOAuth}} }{{/isOAuth}}
} }
{{/authMethods}} {{/authMethods}}
@ -356,7 +359,7 @@ sub update_params_for_auth {
sub _global_auth_setup { sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_; my ($self, $header_params, $query_params) = @_;
my $tokens = $self->_cfg->get_tokens; my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens; return unless keys %$tokens;
# basic # basic

View File

@ -49,19 +49,28 @@ my %_apis = map { $_ =~ /^{{moduleName}}::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/} grep {$_ =~ /Api$/}
usesub '{{moduleName}}'; usesub '{{moduleName}}';
=head1 new() =head1 new($api_client)
create a new {{moduleName}}::ApiFactory instance with the given {{moduleName}}::ApiClient instance.
=head1 new(%paramters)
Any parameters are optional, and are passed to and stored on the api_client object. Any parameters are optional, and are passed to and stored on the api_client object.
base_url: (optional) See L<{{moduleName}}::ApiClient> and L<{{moduleName}}::Configuration> for valid paramters
supply this to change the default base URL taken from the Swagger definition.
=cut =cut
sub new { sub new {
my ($class, %p) = (shift, @_); my ($class) = shift;
$p{api_client} = {{moduleName}}::ApiClient->instance(%p);
return bless \%p, $class; my $api_client;
if ($_[0] && ref $_[0] && ref $_[0] eq '{{moduleName}}::ApiClient' ) {
$api_client = $_[0];
} else {
$api_client = {{moduleName}}::ApiClient->new(@_);
}
bless { api_client => $api_client }, $class;
} }
=head1 get_api($which) =head1 get_api($which)
@ -78,7 +87,7 @@ sub get_api {
my ($self, $which) = @_; my ($self, $which) = @_;
croak "API not specified" unless $which; croak "API not specified" unless $which;
my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'"; my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'";
return $api_class->new(api_client => $self->api_client); return $api_class->new($self->api_client);
} }
=head1 api_client() =head1 api_client()

View File

@ -15,71 +15,142 @@ use Carp;
use constant VERSION => '{{moduleVersion}}'; use constant VERSION => '{{moduleVersion}}';
# class/static variables =head1 Name
our $http_timeout = 180;
our $http_user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{moduleVersion}}}/perl{{/httpUserAgent}}';
# authentication setting {{moduleName}}::Configuration - holds the configuration for all {{moduleName}} Modules
our $api_key = {};
our $api_key_prefix = {};
our $api_key_in = {};
# username and password for HTTP basic authentication =head1 new(%paramters)
our $username = '';
our $password = ''; =over 4
=item http_timeout: (optional)
Integer. timeout for HTTP requests in seconds
default: 180
=item http_user_agent: (optional)
String. custom UserAgent header
default: {{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{moduleVersion}}}/perl{{/httpUserAgent}}
=item api_key: (optional)
Hashref. Keyed on the name of each key (there can be multiple tokens).
api_key => {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};
=item api_key_prefix: (optional)
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not all api keys require a prefix.
api_key_prefix => {
secretKey => 'string',
anotherKey => 'same or some other string',
};
=item api_key_in: (optional)
=item username: (optional)
String. The username for basic auth.
=item password: (optional)
String. The password for basic auth.
=item access_token: (optional)
String. The OAuth access token.
=item base_url: (optional)
String. The base URL of the API
default: {{{basePath}}}
=back
=cut
sub new {
my ($self, %p) = (shift,@_);
# class/static variables
$p{http_timeout} //= 180;
$p{http_user_agent} //= '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{moduleVersion}}}/perl{{/httpUserAgent}}';
# authentication setting
$p{api_key} //= {};
$p{api_key_prefix} //= {};
$p{api_key_in} //= {};
# username and password for HTTP basic authentication
$p{username} //= '';
$p{password} //= '';
# access token for OAuth
$p{access_token} //= '';
# base_url
$p{base_url} //= '{{{basePath}}}';
return bless \%p => $self;
}
# access token for OAuth
our $access_token = '';
sub get_tokens { sub get_tokens {
my $class = shift; my $self = shift;
my $tokens = {}; my $tokens = {};
$tokens->{username} = $username if $username; $tokens->{username} = $self->{username} if $self->{username};
$tokens->{password} = $password if $password; $tokens->{password} = $self->{password} if $self->{password};
$tokens->{access_token} = $access_token if $access_token; $tokens->{access_token} = $self->{access_token} if $self->{access_token};
foreach my $token_name (keys %{ $api_key }) { foreach my $token_name (keys %{ $self->{api_key} }) {
$tokens->{$token_name}->{token} = $api_key->{$token_name}; $tokens->{$token_name}->{token} = $self->{api_key}{$token_name};
$tokens->{$token_name}->{prefix} = $api_key_prefix->{$token_name}; $tokens->{$token_name}->{prefix} = $self->{api_key_prefix}{$token_name};
$tokens->{$token_name}->{in} = $api_key_in->{$token_name}; $tokens->{$token_name}->{in} = $self->{api_key_in}{$token_name};
} }
return $tokens; return $tokens;
} }
sub clear_tokens { sub clear_tokens {
my $class = shift; my $self = shift;
my %tokens = %{$class->get_tokens}; # copy my %tokens = %{$self->get_tokens}; # copy
$username = undef; $self->{username} = '';
$password = undef; $self->{password} = '';
$access_token = undef; $self->{access_token} = '';
$api_key = {}; $self->{api_key} = {};
$api_key_prefix = {}; $self->{api_key_prefix} = {};
$api_key_in = {}; $self->{api_key_in} = {};
return \%tokens; return \%tokens;
} }
sub accept_tokens { sub accept_tokens {
my ($class, $tokens) = @_; my ($self, $tokens) = @_;
foreach my $known_name (qw(username password access_token)) { foreach my $known_name (qw(username password access_token)) {
next unless $tokens->{$known_name}; next unless $tokens->{$known_name};
eval "\$$known_name = delete \$tokens->{\$known_name}"; $self->{$known_name} = delete $tokens->{$known_name};
die $@ if $@;
} }
foreach my $token_name (keys %$tokens) { foreach my $token_name (keys %$tokens) {
$api_key->{$token_name} = $tokens->{$token_name}->{token}; $self->{api_key}{$token_name} = $tokens->{$token_name}{token};
if ($tokens->{$token_name}->{prefix}) { if ($tokens->{$token_name}{prefix}) {
$api_key_prefix->{$token_name} = $tokens->{$token_name}->{prefix}; $self->{api_key_prefix}{$token_name} = $tokens->{$token_name}{prefix};
} }
my $in = $tokens->{$token_name}->{in} || 'head'; my $in = $tokens->{$token_name}->{in} || 'head';
croak "Tokens can only go in 'head' or 'query' (not in '$in')" unless $in =~ /^(?:head|query)$/; croak "Tokens can only go in 'head' or 'query' (not in '$in')" unless $in =~ /^(?:head|query)$/;
$api_key_in->{$token_name} = $in; $self->{api_key_in}{$token_name} = $in;
} }
} }

View File

@ -94,37 +94,37 @@ you are accessing. Usually `prefix` and `in` will be determined by the code gene
the spec and you will not need to set them at run time. If not, `in` will the spec and you will not need to set them at run time. If not, `in` will
default to 'head' and `prefix` to the empty string. default to 'head' and `prefix` to the empty string.
The tokens will be placed in the `{{moduleName}}::Configuration` namespace The tokens will be placed in a L<{{moduleName}}::Configuration> instance
as follows, but you don't need to know about this. as follows, but you don't need to know about this.
- `${{moduleName}}::Configuration::username` - `$cfg->{username}`
String. The username for basic auth. String. The username for basic auth.
- `${{moduleName}}::Configuration::password` - `$cfg->{password}`
String. The password for basic auth. String. The password for basic auth.
- `${{moduleName}}::Configuration::api_key` - `$cfg->{api_key}`
Hashref. Keyed on the name of each key (there can be multiple tokens). Hashref. Keyed on the name of each key (there can be multiple tokens).
${{moduleName}}::Configuration::api_key = { $cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd', secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444', anotherKey => '1111222233334444',
}; };
- `${{moduleName}}::Configuration::api_key_prefix` - `$cfg->{api_key_prefix}`
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix. all api keys require a prefix.
${{moduleName}}::Configuration::api_key_prefix = { $cfg->{api_key_prefix} = {
secretKey => 'string', secretKey => 'string',
anotherKey => 'same or some other string', anotherKey => 'same or some other string',
}; };
- `${{moduleName}}::Configuration::access_token` - `$cfg->{access_token}`
String. The OAuth access token. String. The OAuth access token.
@ -133,8 +133,7 @@ as follows, but you don't need to know about this.
## `base_url` ## `base_url`
The generated code has the `base_url` already set as a default value. This method The generated code has the `base_url` already set as a default value. This method
returns (and optionally sets, but only if the API client has not been returns the current value of `base_url`.
created yet) the current value of `base_url`.
## `api_factory` ## `api_factory`
@ -253,21 +252,22 @@ use warnings;
{{/model}}{{/models}} {{/model}}{{/models}}
# for displaying the API response data # for displaying the API response data
use Data::Dumper; use Data::Dumper;
use {{{moduleName}}}::Configuration;
use {{moduleName}}::{{classname}}; use {{moduleName}}::{{classname}};
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}}
${{{moduleName}}}::Configuration::username = 'YOUR_USERNAME';
${{{moduleName}}}::Configuration::password = 'YOUR_PASSWORD';{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}}
${{{moduleName}}}::Configuration::api_key->{'{{{keyParamName}}}'} = 'YOUR_API_KEY';
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#${{{moduleName}}}::Configuration::api_key_prefix->{'{{{keyParamName}}}'} = 'Bearer';{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
${{{moduleName}}}::Configuration::access_token = 'YOUR_ACCESS_TOKEN';{{/isOAuth}}{{/authMethods}}
{{/hasAuthMethods}}
my $api_instance = {{moduleName}}::{{classname}}->new(); my $api_instance = {{moduleName}}::{{classname}}->new(
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}}
username => 'YOUR_USERNAME',
password => 'YOUR_PASSWORD',{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}}
api_key => {'{{{keyParamName}}}' => 'YOUR_API_KEY'},
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#api_key_prefix => {'{{{keyParamName}}}' => 'Bearer'},{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
access_token => 'YOUR_ACCESS_TOKEN',{{/isOAuth}}{{/authMethods}}
{{/hasAuthMethods}}
);
{{#allParams}}my ${{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{{moduleName}}}::Object::{{dataType}}->new(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; # {{{dataType}}} | {{{description}}} {{#allParams}}my ${{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{{moduleName}}}::Object::{{dataType}}->new(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; # {{{dataType}}} | {{{description}}}
{{/allParams}} {{/allParams}}

View File

@ -34,8 +34,8 @@ has tokens => ( is => 'ro',
); );
has _cfg => ( is => 'ro', has _cfg => ( is => 'ro',
isa => 'Str', isa => '{{moduleName}}::Configuration',
default => '{{moduleName}}::Configuration', default => sub { {{moduleName}}::Configuration->new() },
); );
has version_info => ( is => 'ro', has version_info => ( is => 'ro',
@ -196,39 +196,39 @@ you are accessing. Usually C<prefix> and C<in> will be determined by the code ge
the spec and you will not need to set them at run time. If not, C<in> will the spec and you will not need to set them at run time. If not, C<in> will
default to 'head' and C<prefix> to the empty string. default to 'head' and C<prefix> to the empty string.
The tokens will be placed in the C<{{moduleName}}::Configuration> namespace The tokens will be placed in a L<{{moduleName}}::Configuration> instance
as follows, but you don't need to know about this. as follows, but you don't need to know about this.
=over 4 =over 4
=item C<${{moduleName}}::Configuration::username> =item C<$cfg-\>{username}>
String. The username for basic auth. String. The username for basic auth.
=item C<${{moduleName}}::Configuration::password> =item C<$cfg-\>{password}>
String. The password for basic auth. String. The password for basic auth.
=item C<${{moduleName}}::Configuration::api_key> =item C<$cfg-\>{api_key}>
Hashref. Keyed on the name of each key (there can be multiple tokens). Hashref. Keyed on the name of each key (there can be multiple tokens).
${{moduleName}}::Configuration::api_key = { $cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd', secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444', anotherKey => '1111222233334444',
}; };
=item C<${{moduleName}}::Configuration::api_key_prefix> =item C<$cfg->{api_key_prefix}>
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix. all api keys require a prefix.
${{moduleName}}::Configuration::api_key_prefix = { $cfg->{api_key_prefix} = {
secretKey => 'string', secretKey => 'string',
anotherKey => 'same or some other string', anotherKey => 'same or some other string',
}; };
=item C<${{moduleName}}::Configuration::access_token> =item C<$config-\>{access_token}>
String. The OAuth access token. String. The OAuth access token.
@ -239,8 +239,7 @@ String. The OAuth access token.
=head2 C<base_url> =head2 C<base_url>
The generated code has the C<base_url> already set as a default value. This method The generated code has the C<base_url> already set as a default value. This method
returns (and optionally sets, but only if the API client has not been returns the current value of C<base_url>.
created yet) the current value of C<base_url>.
=head2 C<api_factory> =head2 C<api_factory>

View File

@ -15,7 +15,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use {{moduleName}}::ApiClient; use {{moduleName}}::ApiClient;
use {{moduleName}}::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -23,17 +22,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => {{moduleName}}::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq '{{moduleName}}::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = {{moduleName}}::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }
{{#operations}} {{#operations}}

View File

@ -25,21 +25,21 @@ Method | HTTP request | Description
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use {{{moduleName}}}::Configuration;
use {{moduleName}}::{{classname}}; use {{moduleName}}::{{classname}};
my $api_instance = {{moduleName}}::{{classname}}->new(
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} {{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}} # Configure HTTP basic authorization: {{{name}}}
${{{moduleName}}}::Configuration::username = 'YOUR_USERNAME'; username => 'YOUR_USERNAME',
${{{moduleName}}}::Configuration::password = 'YOUR_PASSWORD';{{/isBasic}}{{#isApiKey}} password => 'YOUR_PASSWORD',{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}} # Configure API key authorization: {{{name}}}
${{{moduleName}}}::Configuration::api_key->{'{{{keyParamName}}}'} = 'YOUR_API_KEY'; api_key => {'{{{keyParamName}}}' => 'YOUR_API_KEY'},
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed # uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#${{{moduleName}}}::Configuration::api_key_prefix->{'{{{keyParamName}}}'} = "Bearer";{{/isApiKey}}{{#isOAuth}} #api_key_prefix => {'{{{keyParamName}}}' => 'Bearer'},{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}} # Configure OAuth2 access token for authorization: {{{name}}}
${{{moduleName}}}::Configuration::access_token = 'YOUR_ACCESS_TOKEN';{{/isOAuth}}{{/authMethods}} access_token => 'YOUR_ACCESS_TOKEN',{{/isOAuth}}{{/authMethods}}
{{/hasAuthMethods}} {{/hasAuthMethods}}
);
my $api_instance = {{moduleName}}::{{classname}}->new();
{{#allParams}}my ${{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{{moduleName}}}::Object::{{dataType}}->new(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; # {{{dataType}}} | {{{description}}} {{#allParams}}my ${{paramName}} = {{#isListContainer}}[{{/isListContainer}}{{#isBodyParam}}{{{moduleName}}}::Object::{{dataType}}->new(){{/isBodyParam}}{{^isBodyParam}}{{{example}}}{{/isBodyParam}}{{#isListContainer}}]{{/isListContainer}}; # {{{dataType}}} | {{{description}}}
{{/allParams}} {{/allParams}}

View File

@ -88,37 +88,37 @@ you are accessing. Usually `prefix` and `in` will be determined by the code gene
the spec and you will not need to set them at run time. If not, `in` will the spec and you will not need to set them at run time. If not, `in` will
default to 'head' and `prefix` to the empty string. default to 'head' and `prefix` to the empty string.
The tokens will be placed in the `WWW::SwaggerClient::Configuration` namespace The tokens will be placed in a L<WWW::SwaggerClient::Configuration> instance
as follows, but you don't need to know about this. as follows, but you don't need to know about this.
- `$WWW::SwaggerClient::Configuration::username` - `$cfg->{username}`
String. The username for basic auth. String. The username for basic auth.
- `$WWW::SwaggerClient::Configuration::password` - `$cfg->{password}`
String. The password for basic auth. String. The password for basic auth.
- `$WWW::SwaggerClient::Configuration::api_key` - `$cfg->{api_key}`
Hashref. Keyed on the name of each key (there can be multiple tokens). Hashref. Keyed on the name of each key (there can be multiple tokens).
$WWW::SwaggerClient::Configuration::api_key = { $cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd', secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444', anotherKey => '1111222233334444',
}; };
- `$WWW::SwaggerClient::Configuration::api_key_prefix` - `$cfg->{api_key_prefix}`
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix. all api keys require a prefix.
$WWW::SwaggerClient::Configuration::api_key_prefix = { $cfg->{api_key_prefix} = {
secretKey => 'string', secretKey => 'string',
anotherKey => 'same or some other string', anotherKey => 'same or some other string',
}; };
- `$WWW::SwaggerClient::Configuration::access_token` - `$cfg->{access_token}`
String. The OAuth access token. String. The OAuth access token.
@ -127,8 +127,7 @@ as follows, but you don't need to know about this.
## `base_url` ## `base_url`
The generated code has the `base_url` already set as a default value. This method The generated code has the `base_url` already set as a default value. This method
returns (and optionally sets, but only if the API client has not been returns the current value of `base_url`.
created yet) the current value of `base_url`.
## `api_factory` ## `api_factory`
@ -317,10 +316,11 @@ use WWW::SwaggerClient::Object::User;
# for displaying the API response data # for displaying the API response data
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::; use WWW::SwaggerClient::;
my $api_instance = WWW::SwaggerClient::FakeApi->new(); my $api_instance = WWW::SwaggerClient::->new(
);
my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model
eval { eval {

View File

@ -24,10 +24,10 @@ To test \"client\" model
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::FakeApi; use WWW::SwaggerClient::FakeApi;
my $api_instance = WWW::SwaggerClient::FakeApi->new(
);
my $api_instance = WWW::SwaggerClient::FakeApi->new();
my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model
eval { eval {
@ -70,14 +70,14 @@ Fake endpoint for testing various parameters 假端點 偽のエンドポイン
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::FakeApi; use WWW::SwaggerClient::FakeApi;
my $api_instance = WWW::SwaggerClient::FakeApi->new(
# Configure HTTP basic authorization: http_basic_test # Configure HTTP basic authorization: http_basic_test
$WWW::SwaggerClient::Configuration::username = 'YOUR_USERNAME'; username => 'YOUR_USERNAME',
$WWW::SwaggerClient::Configuration::password = 'YOUR_PASSWORD'; password => 'YOUR_PASSWORD',
);
my $api_instance = WWW::SwaggerClient::FakeApi->new();
my $number = 3.4; # Number | None my $number = 3.4; # Number | None
my $double = 1.2; # double | None my $double = 1.2; # double | None
my $pattern_without_delimiter = 'pattern_without_delimiter_example'; # string | None my $pattern_without_delimiter = 'pattern_without_delimiter_example'; # string | None
@ -145,10 +145,10 @@ To test enum parameters
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::FakeApi; use WWW::SwaggerClient::FakeApi;
my $api_instance = WWW::SwaggerClient::FakeApi->new(
);
my $api_instance = WWW::SwaggerClient::FakeApi->new();
my $enum_form_string_array = []; # ARRAY[string] | Form parameter enum test (string array) my $enum_form_string_array = []; # ARRAY[string] | Form parameter enum test (string array)
my $enum_form_string = 'enum_form_string_example'; # string | Form parameter enum test (string) my $enum_form_string = 'enum_form_string_example'; # string | Form parameter enum test (string)
my $enum_header_string_array = []; # ARRAY[string] | Header parameter enum test (string array) my $enum_header_string_array = []; # ARRAY[string] | Header parameter enum test (string array)

View File

@ -5,7 +5,7 @@
use WWW::SwaggerClient::Object::FakeClassnameTags123Api; use WWW::SwaggerClient::Object::FakeClassnameTags123Api;
``` ```
All URIs are relative to *http://petstore.swagger.io/v2* All URIs are relative to *http://petstore.swagger.io:80/v2*
Method | HTTP request | Description Method | HTTP request | Description
------------- | ------------- | ------------- ------------- | ------------- | -------------
@ -20,10 +20,10 @@ To test class name in snake case
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::FakeClassnameTags123Api; use WWW::SwaggerClient::FakeClassnameTags123Api;
my $api_instance = WWW::SwaggerClient::FakeClassnameTags123Api->new(
);
my $api_instance = WWW::SwaggerClient::FakeClassnameTags123Api->new();
my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model my $body = WWW::SwaggerClient::Object::Client->new(); # Client | client model
eval { eval {

View File

@ -29,13 +29,13 @@ Add a new pet to the store
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $body = WWW::SwaggerClient::Object::Pet->new(); # Pet | Pet object that needs to be added to the store my $body = WWW::SwaggerClient::Object::Pet->new(); # Pet | Pet object that needs to be added to the store
eval { eval {
@ -77,13 +77,13 @@ Deletes a pet
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $pet_id = 789; # int | Pet id to delete my $pet_id = 789; # int | Pet id to delete
my $api_key = 'api_key_example'; # string | my $api_key = 'api_key_example'; # string |
@ -127,13 +127,13 @@ Multiple status values can be provided with comma separated strings
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $status = []; # ARRAY[string] | Status values that need to be considered for filter my $status = []; # ARRAY[string] | Status values that need to be considered for filter
eval { eval {
@ -176,13 +176,13 @@ Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $tags = []; # ARRAY[string] | Tags to filter by my $tags = []; # ARRAY[string] | Tags to filter by
eval { eval {
@ -225,15 +225,15 @@ Returns a single pet
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure API key authorization: api_key # Configure API key authorization: api_key
$WWW::SwaggerClient::Configuration::api_key->{'api_key'} = 'YOUR_API_KEY'; api_key => {'api_key' => 'YOUR_API_KEY'},
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed # uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#$WWW::SwaggerClient::Configuration::api_key_prefix->{'api_key'} = "Bearer"; #api_key_prefix => {'api_key' => 'Bearer'},
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $pet_id = 789; # int | ID of pet to return my $pet_id = 789; # int | ID of pet to return
eval { eval {
@ -276,13 +276,13 @@ Update an existing pet
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $body = WWW::SwaggerClient::Object::Pet->new(); # Pet | Pet object that needs to be added to the store my $body = WWW::SwaggerClient::Object::Pet->new(); # Pet | Pet object that needs to be added to the store
eval { eval {
@ -324,13 +324,13 @@ Updates a pet in the store with form data
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $pet_id = 789; # int | ID of pet that needs to be updated my $pet_id = 789; # int | ID of pet that needs to be updated
my $name = 'name_example'; # string | Updated name of the pet my $name = 'name_example'; # string | Updated name of the pet
my $status = 'status_example'; # string | Updated status of the pet my $status = 'status_example'; # string | Updated status of the pet
@ -376,13 +376,13 @@ uploads an image
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::PetApi; use WWW::SwaggerClient::PetApi;
my $api_instance = WWW::SwaggerClient::PetApi->new(
# Configure OAuth2 access token for authorization: petstore_auth # Configure OAuth2 access token for authorization: petstore_auth
$WWW::SwaggerClient::Configuration::access_token = 'YOUR_ACCESS_TOKEN'; access_token => 'YOUR_ACCESS_TOKEN',
);
my $api_instance = WWW::SwaggerClient::PetApi->new();
my $pet_id = 789; # int | ID of pet to update my $pet_id = 789; # int | ID of pet to update
my $additional_metadata = 'additional_metadata_example'; # string | Additional data to pass to server my $additional_metadata = 'additional_metadata_example'; # string | Additional data to pass to server
my $file = '/path/to/file.txt'; # File | file to upload my $file = '/path/to/file.txt'; # File | file to upload

View File

@ -25,10 +25,10 @@ For valid response try integer IDs with value < 1000. Anything above 1000 or non
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::StoreApi; use WWW::SwaggerClient::StoreApi;
my $api_instance = WWW::SwaggerClient::StoreApi->new(
);
my $api_instance = WWW::SwaggerClient::StoreApi->new();
my $order_id = 'order_id_example'; # string | ID of the order that needs to be deleted my $order_id = 'order_id_example'; # string | ID of the order that needs to be deleted
eval { eval {
@ -70,15 +70,15 @@ Returns a map of status codes to quantities
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::StoreApi; use WWW::SwaggerClient::StoreApi;
my $api_instance = WWW::SwaggerClient::StoreApi->new(
# Configure API key authorization: api_key # Configure API key authorization: api_key
$WWW::SwaggerClient::Configuration::api_key->{'api_key'} = 'YOUR_API_KEY'; api_key => {'api_key' => 'YOUR_API_KEY'},
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed # uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#$WWW::SwaggerClient::Configuration::api_key_prefix->{'api_key'} = "Bearer"; #api_key_prefix => {'api_key' => 'Bearer'},
);
my $api_instance = WWW::SwaggerClient::StoreApi->new();
eval { eval {
my $result = $api_instance->get_inventory(); my $result = $api_instance->get_inventory();
@ -117,10 +117,10 @@ For valid response try integer IDs with value <= 5 or > 10. Other values will ge
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::StoreApi; use WWW::SwaggerClient::StoreApi;
my $api_instance = WWW::SwaggerClient::StoreApi->new(
);
my $api_instance = WWW::SwaggerClient::StoreApi->new();
my $order_id = 789; # int | ID of pet that needs to be fetched my $order_id = 789; # int | ID of pet that needs to be fetched
eval { eval {
@ -163,10 +163,10 @@ Place an order for a pet
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::StoreApi; use WWW::SwaggerClient::StoreApi;
my $api_instance = WWW::SwaggerClient::StoreApi->new(
);
my $api_instance = WWW::SwaggerClient::StoreApi->new();
my $body = WWW::SwaggerClient::Object::Order->new(); # Order | order placed for purchasing the pet my $body = WWW::SwaggerClient::Object::Order->new(); # Order | order placed for purchasing the pet
eval { eval {

View File

@ -29,10 +29,10 @@ This can only be done by the logged in user.
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $body = WWW::SwaggerClient::Object::User->new(); # User | Created user object my $body = WWW::SwaggerClient::Object::User->new(); # User | Created user object
eval { eval {
@ -74,10 +74,10 @@ Creates list of users with given input array
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $body = [WWW::SwaggerClient::Object::ARRAY[User]->new()]; # ARRAY[User] | List of user object my $body = [WWW::SwaggerClient::Object::ARRAY[User]->new()]; # ARRAY[User] | List of user object
eval { eval {
@ -119,10 +119,10 @@ Creates list of users with given input array
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $body = [WWW::SwaggerClient::Object::ARRAY[User]->new()]; # ARRAY[User] | List of user object my $body = [WWW::SwaggerClient::Object::ARRAY[User]->new()]; # ARRAY[User] | List of user object
eval { eval {
@ -164,10 +164,10 @@ This can only be done by the logged in user.
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $username = 'username_example'; # string | The name that needs to be deleted my $username = 'username_example'; # string | The name that needs to be deleted
eval { eval {
@ -209,10 +209,10 @@ Get user by user name
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $username = 'username_example'; # string | The name that needs to be fetched. Use user1 for testing. my $username = 'username_example'; # string | The name that needs to be fetched. Use user1 for testing.
eval { eval {
@ -255,10 +255,10 @@ Logs user into the system
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $username = 'username_example'; # string | The user name for login my $username = 'username_example'; # string | The user name for login
my $password = 'password_example'; # string | The password for login in clear text my $password = 'password_example'; # string | The password for login in clear text
@ -303,10 +303,10 @@ Logs out current logged in user session
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
eval { eval {
$api_instance->logout_user(); $api_instance->logout_user();
@ -344,10 +344,10 @@ This can only be done by the logged in user.
### Example ### Example
```perl ```perl
use Data::Dumper; use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::UserApi; use WWW::SwaggerClient::UserApi;
my $api_instance = WWW::SwaggerClient::UserApi->new(
);
my $api_instance = WWW::SwaggerClient::UserApi->new();
my $username = 'username_example'; # string | name that need to be deleted my $username = 'username_example'; # string | name that need to be deleted
my $body = WWW::SwaggerClient::Object::User->new(); # User | Updated user object my $body = WWW::SwaggerClient::Object::User->new(); # User | Updated user object

View File

@ -39,22 +39,25 @@ use Module::Runtime qw(use_module);
use WWW::SwaggerClient::Configuration; use WWW::SwaggerClient::Configuration;
use base 'Class::Singleton';
sub _new_instance sub new {
{
my $class = shift; my $class = shift;
my $config;
if ( $_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::Configuration' ) {
$config = $_[0];
} else {
$config = WWW::SwaggerClient::Configuration->new(@_);
}
my (%args) = ( my (%args) = (
'ua' => LWP::UserAgent->new, 'ua' => LWP::UserAgent->new,
'base_url' => 'http://petstore.swagger.io:80/v2', 'config' => $config,
@_
); );
return bless \%args, $class; return bless \%args, $class;
} }
sub _cfg {'WWW::SwaggerClient::Configuration'}
# Set the user agent of the API client # Set the user agent of the API client
# #
# @param string $user_agent The user agent of the API client # @param string $user_agent The user agent of the API client
@ -91,7 +94,7 @@ sub call_api {
$self->update_params_for_auth($header_params, $query_params, $auth_settings); $self->update_params_for_auth($header_params, $query_params, $auth_settings);
my $_url = $self->{base_url} . $resource_path; my $_url = $self->{config}{base_url} . $resource_path;
# build query # build query
if (%$query_params) { if (%$query_params) {
@ -138,8 +141,8 @@ sub call_api {
else { else {
} }
$self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout); $self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
$self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent); $self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});
$log->debugf("REQUEST: %s", $_request->as_string); $log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request); my $_response = $self->{ua}->request($_request);
@ -313,11 +316,11 @@ sub get_api_key_with_prefix
{ {
my ($self, $key_name) = @_; my ($self, $key_name) = @_;
my $api_key = $WWW::SwaggerClient::Configuration::api_key->{$key_name}; my $api_key = $self->{config}{api_key}{$key_name};
return unless $api_key; return unless $api_key;
my $prefix = $WWW::SwaggerClient::Configuration::api_key_prefix->{$key_name}; my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key; return $prefix ? "$prefix $api_key" : $api_key;
} }
@ -347,14 +350,14 @@ sub update_params_for_auth {
} }
elsif ($auth eq 'http_basic_test') { elsif ($auth eq 'http_basic_test') {
if ($WWW::SwaggerClient::Configuration::username || $WWW::SwaggerClient::Configuration::password) { if ($self->{config}{username} || $self->{config}{password}) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64($WWW::SwaggerClient::Configuration::username . ":" . $WWW::SwaggerClient::Configuration::password); $header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
} }
} }
elsif ($auth eq 'petstore_auth') { elsif ($auth eq 'petstore_auth') {
if ($WWW::SwaggerClient::Configuration::access_token) { if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token; $header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
} }
} }
else { else {
@ -370,7 +373,7 @@ elsif ($auth eq 'petstore_auth') {
sub _global_auth_setup { sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_; my ($self, $header_params, $query_params) = @_;
my $tokens = $self->_cfg->get_tokens; my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens; return unless keys %$tokens;
# basic # basic

View File

@ -62,19 +62,28 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/} grep {$_ =~ /Api$/}
usesub 'WWW::SwaggerClient'; usesub 'WWW::SwaggerClient';
=head1 new() =head1 new($api_client)
create a new WWW::SwaggerClient::ApiFactory instance with the given WWW::SwaggerClient::ApiClient instance.
=head1 new(%paramters)
Any parameters are optional, and are passed to and stored on the api_client object. Any parameters are optional, and are passed to and stored on the api_client object.
base_url: (optional) See L<WWW::SwaggerClient::ApiClient> and L<WWW::SwaggerClient::Configuration> for valid paramters
supply this to change the default base URL taken from the Swagger definition.
=cut =cut
sub new { sub new {
my ($class, %p) = (shift, @_); my ($class) = shift;
$p{api_client} = WWW::SwaggerClient::ApiClient->instance(%p);
return bless \%p, $class; my $api_client;
if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
$api_client = $_[0];
} else {
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless { api_client => $api_client }, $class;
} }
=head1 get_api($which) =head1 get_api($which)
@ -91,7 +100,7 @@ sub get_api {
my ($self, $which) = @_; my ($self, $which) = @_;
croak "API not specified" unless $which; croak "API not specified" unless $which;
my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'"; my $api_class = $_apis{"${which}Api"} || croak "No known API for '$which'";
return $api_class->new(api_client => $self->api_client); return $api_class->new($self->api_client);
} }
=head1 api_client() =head1 api_client()

View File

@ -28,71 +28,142 @@ use Carp;
use constant VERSION => '1.0.0'; use constant VERSION => '1.0.0';
# class/static variables =head1 Name
our $http_timeout = 180;
our $http_user_agent = 'Swagger-Codegen/1.0.0/perl';
# authentication setting WWW::SwaggerClient::Configuration - holds the configuration for all WWW::SwaggerClient Modules
our $api_key = {};
our $api_key_prefix = {};
our $api_key_in = {};
# username and password for HTTP basic authentication =head1 new(%paramters)
our $username = '';
our $password = ''; =over 4
=item http_timeout: (optional)
Integer. timeout for HTTP requests in seconds
default: 180
=item http_user_agent: (optional)
String. custom UserAgent header
default: Swagger-Codegen/1.0.0/perl
=item api_key: (optional)
Hashref. Keyed on the name of each key (there can be multiple tokens).
api_key => {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};
=item api_key_prefix: (optional)
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not all api keys require a prefix.
api_key_prefix => {
secretKey => 'string',
anotherKey => 'same or some other string',
};
=item api_key_in: (optional)
=item username: (optional)
String. The username for basic auth.
=item password: (optional)
String. The password for basic auth.
=item access_token: (optional)
String. The OAuth access token.
=item base_url: (optional)
String. The base URL of the API
default: http://petstore.swagger.io:80/v2
=back
=cut
sub new {
my ($self, %p) = (shift,@_);
# class/static variables
$p{http_timeout} //= 180;
$p{http_user_agent} //= 'Swagger-Codegen/1.0.0/perl';
# authentication setting
$p{api_key} //= {};
$p{api_key_prefix} //= {};
$p{api_key_in} //= {};
# username and password for HTTP basic authentication
$p{username} //= '';
$p{password} //= '';
# access token for OAuth
$p{access_token} //= '';
# base_url
$p{base_url} //= 'http://petstore.swagger.io:80/v2';
return bless \%p => $self;
}
# access token for OAuth
our $access_token = '';
sub get_tokens { sub get_tokens {
my $class = shift; my $self = shift;
my $tokens = {}; my $tokens = {};
$tokens->{username} = $username if $username; $tokens->{username} = $self->{username} if $self->{username};
$tokens->{password} = $password if $password; $tokens->{password} = $self->{password} if $self->{password};
$tokens->{access_token} = $access_token if $access_token; $tokens->{access_token} = $self->{access_token} if $self->{access_token};
foreach my $token_name (keys %{ $api_key }) { foreach my $token_name (keys %{ $self->{api_key} }) {
$tokens->{$token_name}->{token} = $api_key->{$token_name}; $tokens->{$token_name}->{token} = $self->{api_key}{$token_name};
$tokens->{$token_name}->{prefix} = $api_key_prefix->{$token_name}; $tokens->{$token_name}->{prefix} = $self->{api_key_prefix}{$token_name};
$tokens->{$token_name}->{in} = $api_key_in->{$token_name}; $tokens->{$token_name}->{in} = $self->{api_key_in}{$token_name};
} }
return $tokens; return $tokens;
} }
sub clear_tokens { sub clear_tokens {
my $class = shift; my $self = shift;
my %tokens = %{$class->get_tokens}; # copy my %tokens = %{$self->get_tokens}; # copy
$username = undef; $self->{username} = '';
$password = undef; $self->{password} = '';
$access_token = undef; $self->{access_token} = '';
$api_key = {}; $self->{api_key} = {};
$api_key_prefix = {}; $self->{api_key_prefix} = {};
$api_key_in = {}; $self->{api_key_in} = {};
return \%tokens; return \%tokens;
} }
sub accept_tokens { sub accept_tokens {
my ($class, $tokens) = @_; my ($self, $tokens) = @_;
foreach my $known_name (qw(username password access_token)) { foreach my $known_name (qw(username password access_token)) {
next unless $tokens->{$known_name}; next unless $tokens->{$known_name};
eval "\$$known_name = delete \$tokens->{\$known_name}"; $self->{$known_name} = delete $tokens->{$known_name};
die $@ if $@;
} }
foreach my $token_name (keys %$tokens) { foreach my $token_name (keys %$tokens) {
$api_key->{$token_name} = $tokens->{$token_name}->{token}; $self->{api_key}{$token_name} = $tokens->{$token_name}{token};
if ($tokens->{$token_name}->{prefix}) { if ($tokens->{$token_name}{prefix}) {
$api_key_prefix->{$token_name} = $tokens->{$token_name}->{prefix}; $self->{api_key_prefix}{$token_name} = $tokens->{$token_name}{prefix};
} }
my $in = $tokens->{$token_name}->{in} || 'head'; my $in = $tokens->{$token_name}->{in} || 'head';
croak "Tokens can only go in 'head' or 'query' (not in '$in')" unless $in =~ /^(?:head|query)$/; croak "Tokens can only go in 'head' or 'query' (not in '$in')" unless $in =~ /^(?:head|query)$/;
$api_key_in->{$token_name} = $in; $self->{api_key_in}{$token_name} = $in;
} }
} }

View File

@ -28,7 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::ApiClient;
use WWW::SwaggerClient::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -36,17 +35,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }

View File

@ -28,7 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::ApiClient;
use WWW::SwaggerClient::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -36,17 +35,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }

View File

@ -28,7 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::ApiClient;
use WWW::SwaggerClient::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -36,17 +35,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }

View File

@ -47,8 +47,8 @@ has tokens => ( is => 'ro',
); );
has _cfg => ( is => 'ro', has _cfg => ( is => 'ro',
isa => 'Str', isa => 'WWW::SwaggerClient::Configuration',
default => 'WWW::SwaggerClient::Configuration', default => sub { WWW::SwaggerClient::Configuration->new() },
); );
has version_info => ( is => 'ro', has version_info => ( is => 'ro',
@ -201,39 +201,39 @@ you are accessing. Usually C<prefix> and C<in> will be determined by the code ge
the spec and you will not need to set them at run time. If not, C<in> will the spec and you will not need to set them at run time. If not, C<in> will
default to 'head' and C<prefix> to the empty string. default to 'head' and C<prefix> to the empty string.
The tokens will be placed in the C<WWW::SwaggerClient::Configuration> namespace The tokens will be placed in a L<WWW::SwaggerClient::Configuration> instance
as follows, but you don't need to know about this. as follows, but you don't need to know about this.
=over 4 =over 4
=item C<$WWW::SwaggerClient::Configuration::username> =item C<$cfg-\>{username}>
String. The username for basic auth. String. The username for basic auth.
=item C<$WWW::SwaggerClient::Configuration::password> =item C<$cfg-\>{password}>
String. The password for basic auth. String. The password for basic auth.
=item C<$WWW::SwaggerClient::Configuration::api_key> =item C<$cfg-\>{api_key}>
Hashref. Keyed on the name of each key (there can be multiple tokens). Hashref. Keyed on the name of each key (there can be multiple tokens).
$WWW::SwaggerClient::Configuration::api_key = { $cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd', secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444', anotherKey => '1111222233334444',
}; };
=item C<$WWW::SwaggerClient::Configuration::api_key_prefix> =item C<$cfg->{api_key_prefix}>
Hashref. Keyed on the name of each key (there can be multiple tokens). Note not Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix. all api keys require a prefix.
$WWW::SwaggerClient::Configuration::api_key_prefix = { $cfg->{api_key_prefix} = {
secretKey => 'string', secretKey => 'string',
anotherKey => 'same or some other string', anotherKey => 'same or some other string',
}; };
=item C<$WWW::SwaggerClient::Configuration::access_token> =item C<$config-\>{access_token}>
String. The OAuth access token. String. The OAuth access token.
@ -244,8 +244,7 @@ String. The OAuth access token.
=head2 C<base_url> =head2 C<base_url>
The generated code has the C<base_url> already set as a default value. This method The generated code has the C<base_url> already set as a default value. This method
returns (and optionally sets, but only if the API client has not been returns the current value of C<base_url>.
created yet) the current value of C<base_url>.
=head2 C<api_factory> =head2 C<api_factory>

View File

@ -28,7 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::ApiClient;
use WWW::SwaggerClient::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -36,17 +35,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }

View File

@ -28,7 +28,6 @@ use Carp qw( croak );
use Log::Any qw($log); use Log::Any qw($log);
use WWW::SwaggerClient::ApiClient; use WWW::SwaggerClient::ApiClient;
use WWW::SwaggerClient::Configuration;
use base "Class::Data::Inheritable"; use base "Class::Data::Inheritable";
@ -36,17 +35,15 @@ __PACKAGE__->mk_classdata('method_documentation' => {});
sub new { sub new {
my $class = shift; my $class = shift;
my (%self) = ( my $api_client;
'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_
);
#my $self = { if ($_[0] && ref $_[0] && ref $_[0] eq 'WWW::SwaggerClient::ApiClient' ) {
# #api_client => $options->{api_client} $api_client = $_[0];
# api_client => $default_api_client } else {
#}; $api_client = WWW::SwaggerClient::ApiClient->new(@_);
}
bless \%self, $class; bless { api_client => $api_client }, $class;
} }

View File

@ -15,15 +15,14 @@ use JSON;
use Data::Dumper; use Data::Dumper;
use DateTime; use DateTime;
$WWW::SwaggerClient::Configuration::http_user_agent = 'Perl-Swagger-Test'; my $api = WWW::SwaggerClient::PetApi->new(
$WWW::SwaggerClient::Configuration::api_key->{'api_key'} = 'ZZZZZZZZZZZZZZ'; http_user_agent => 'Perl-Swagger-Test',
$WWW::SwaggerClient::Configuration::api_key_prefix->{'api_key'} = 'Bearer'; api_key => { api_key => 'ZZZZZZZZZZZZZZ' },
api_key_prefix => { api_key => 'Bearer' },
$WWW::SwaggerClient::Configuration::username = 'username'; username => 'username',
$WWW::SwaggerClient::Configuration::password = 'password'; password => 'password',
);
my $api = WWW::SwaggerClient::PetApi->new();
# exception handling # exception handling
#eval { #eval {

View File

@ -1,4 +1,4 @@
use Test::More tests => 38; use Test::More tests => 37;
use Test::Exception; use Test::Exception;
use lib 'lib'; use lib 'lib';
@ -11,19 +11,15 @@ use_ok('WWW::SwaggerClient::Object::Pet');
use_ok('WWW::SwaggerClient::Object::Tag'); use_ok('WWW::SwaggerClient::Object::Tag');
use_ok('WWW::SwaggerClient::Object::Category'); use_ok('WWW::SwaggerClient::Object::Category');
my $api_client = WWW::SwaggerClient::ApiClient->instance('base_url' => 'http://testing'); my $api_client = WWW::SwaggerClient::ApiClient->new('base_url' => 'http://testing');
my $pet_api = WWW::SwaggerClient::PetApi->new('api_client' => $api_client); my $api = WWW::SwaggerClient::PetApi->new($api_client);
is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client'; is $api->{api_client}{config}{base_url}, 'http://testing', 'get the proper base URL from api client';
my $api = WWW::SwaggerClient::PetApi->new();
is $api->{api_client}->{base_url}, 'http://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 # reset the base_url - no direct access because an application shouldn't be changing
# its base URL halfway through # its base URL halfway through
$api->{api_client}->{base_url} = 'http://petstore.swagger.io/v2'; $api->{api_client}{config}{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'; is $api->{api_client}{config}{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client';
# test select_header_content_type # 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/JSON'), 'application/json', 'get the proper content type application/json but not application/xml';

View File

@ -15,10 +15,10 @@ use_ok('WWW::SwaggerClient::Object::Category');
use_ok('WWW::SwaggerClient::Object::User'); use_ok('WWW::SwaggerClient::Object::User');
my $api_client = WWW::SwaggerClient::ApiClient->instance(); my $api_client = WWW::SwaggerClient::ApiClient->new();
my $store_api = WWW::SwaggerClient::StoreApi->new('api_client' => $api_client); my $store_api = WWW::SwaggerClient::StoreApi->new($api_client);
is $store_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; is $store_api->{api_client}{config}{base_url}, 'http://petstore.swagger.io:80/v2', 'get the default base URL from api client';
my $get_inventory_response = $store_api->get_inventory(); my $get_inventory_response = $store_api->get_inventory();

View File

@ -10,15 +10,15 @@ use_ok('WWW::SwaggerClient::ApiFactory');
my $api_factory = WWW::SwaggerClient::ApiFactory->new('base_url' => 'http://testing'); my $api_factory = WWW::SwaggerClient::ApiFactory->new('base_url' => 'http://testing');
my $pet_api = $api_factory->get_api('Pet'); my $pet_api = $api_factory->get_api('Pet');
isa_ok($pet_api, 'WWW::SwaggerClient::PetApi'); isa_ok($pet_api, 'WWW::SwaggerClient::PetApi');
is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client'; is $pet_api->{api_client}{config}{base_url}, 'http://testing', 'get the proper base URL from api client';
$api_factory = WWW::SwaggerClient::ApiFactory->new; $api_factory = WWW::SwaggerClient::ApiFactory->new;
$pet_api = $api_factory->get_api('Pet'); $pet_api = $api_factory->get_api('Pet');
# reset the base_url - no direct access because an application shouldn't be changing # reset the base_url - no direct access because an application shouldn't be changing
# its base URL halfway through # its base URL halfway through
$pet_api->{api_client}->{base_url} = 'http://petstore.swagger.io/v2'; $pet_api->{api_client}{config}{base_url} = 'http://petstore.swagger.io/v2';
is $pet_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; is $pet_api->{api_client}{config}{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client';
# test accessor methods # test accessor methods
my $pet_id = 10008; my $pet_id = 10008;

View File

@ -95,26 +95,26 @@ my $tokens = {
$api->_cfg->accept_tokens({%$tokens}); # pass a copy $api->_cfg->accept_tokens({%$tokens}); # pass a copy
no warnings 'once'; no warnings 'once';
is $WWW::SwaggerClient::Configuration::username, 'UserName', 'accept_tokens() correctly set the username'; is $api->_cfg->{username}, 'UserName', 'accept_tokens() correctly set the username';
is $WWW::SwaggerClient::Configuration::password, 'PassWord', 'accept_tokens() correctly set the password'; is $api->_cfg->{password}, 'PassWord', 'accept_tokens() correctly set the password';
is $WWW::SwaggerClient::Configuration::access_token, 'OAuth_token', 'accept_tokens() correctly set the oauth'; is $api->_cfg->{access_token}, 'OAuth_token', 'accept_tokens() correctly set the oauth';
my $api_key_href = { my $api_key_href = {
'anotherKey' => 'another_key_token', 'anotherKey' => 'another_key_token',
'someKey' => 'some_key_token' 'someKey' => 'some_key_token'
}; };
cmp_deeply( $WWW::SwaggerClient::Configuration::api_key, $api_key_href, 'accept_tokens() correctly set api_key' ); cmp_deeply( $api->_cfg->{api_key}, $api_key_href, 'accept_tokens() correctly set api_key' );
my $api_key_prefix_href = { my $api_key_prefix_href = {
'someKey' => 'some_key_prefix' 'someKey' => 'some_key_prefix'
}; };
cmp_deeply( $WWW::SwaggerClient::Configuration::api_key_prefix, $api_key_prefix_href, 'accept_tokens() correctly set api_key_prefix' ); cmp_deeply( $api->_cfg->{api_key_prefix}, $api_key_prefix_href, 'accept_tokens() correctly set api_key_prefix' );
my $api_key_in = { my $api_key_in = {
'someKey' => 'query', 'someKey' => 'query',
'anotherKey' => 'head' 'anotherKey' => 'head'
}; };
cmp_deeply( $WWW::SwaggerClient::Configuration::api_key_in, $api_key_in, 'accept_tokens() correctly set api_key_in' ); cmp_deeply( $api->_cfg->{api_key_in}, $api_key_in, 'accept_tokens() correctly set api_key_in' );
use warnings 'once'; use warnings 'once';