[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 base 'Class::Singleton';
sub _new_instance
{
sub new {
my $class = shift;
my $config;
if ( $_[0] && ref $_[0] && ref $_[0] eq '{{moduleName}}::Configuration' ) {
$config = $_[0];
} else {
$config = {{moduleName}}::Configuration->new(@_);
}
my (%args) = (
'ua' => LWP::UserAgent->new,
'base_url' => '{{{basePath}}}',
@_
'config' => $config,
);
return bless \%args, $class;
}
sub _cfg {'{{moduleName}}::Configuration'}
# Set 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);
my $_url = $self->{base_url} . $resource_path;
my $_url = $self->{config}{base_url} . $resource_path;
# build query
if (%$query_params) {
@@ -125,8 +128,8 @@ sub call_api {
else {
}
$self->{ua}->timeout($self->{http_timeout} || ${{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || ${{moduleName}}::Configuration::http_user_agent);
$self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
$self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});
$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
@@ -300,11 +303,11 @@ sub get_api_key_with_prefix
{
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;
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;
}
@@ -335,11 +338,11 @@ sub update_params_for_auth {
if ($api_key) {
$query_params->{'{{keyParamName}}'} = $api_key;
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
if (${{moduleName}}::Configuration::username || ${{moduleName}}::Configuration::password) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64(${{moduleName}}::Configuration::username . ":" . ${{moduleName}}::Configuration::password);
if ($self->{config}{username} || $self->{config}{password}) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
}{{/isBasic}}{{#isOAuth}}
if (${{moduleName}}::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . ${{moduleName}}::Configuration::access_token;
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}{{/isOAuth}}
}
{{/authMethods}}
@@ -356,7 +359,7 @@ sub update_params_for_auth {
sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_;
my $tokens = $self->_cfg->get_tokens;
my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens;
# basic

View File

@@ -49,19 +49,28 @@ my %_apis = map { $_ =~ /^{{moduleName}}::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub '{{moduleName}}';
=head1 new()
=head1 new($api_client)
Any parameters are optional, and are passed to and stored on the api_client object.
base_url: (optional)
supply this to change the default base URL taken from the Swagger definition.
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.
See L<{{moduleName}}::ApiClient> and L<{{moduleName}}::Configuration> for valid paramters
=cut
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = {{moduleName}}::ApiClient->instance(%p);
return bless \%p, $class;
my ($class) = shift;
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)
@@ -78,7 +87,7 @@ 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);
return $api_class->new($self->api_client);
}
=head1 api_client()

View File

@@ -15,71 +15,142 @@ use Carp;
use constant VERSION => '{{moduleVersion}}';
# class/static variables
our $http_timeout = 180;
our $http_user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{moduleVersion}}}/perl{{/httpUserAgent}}';
=head1 Name
# authentication setting
our $api_key = {};
our $api_key_prefix = {};
our $api_key_in = {};
{{moduleName}}::Configuration - holds the configuration for all {{moduleName}} Modules
# username and password for HTTP basic authentication
our $username = '';
our $password = '';
=head1 new(%paramters)
=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 {
my $class = shift;
my $self = shift;
my $tokens = {};
$tokens->{username} = $username if $username;
$tokens->{password} = $password if $password;
$tokens->{access_token} = $access_token if $access_token;
$tokens->{username} = $self->{username} if $self->{username};
$tokens->{password} = $self->{password} if $self->{password};
$tokens->{access_token} = $self->{access_token} if $self->{access_token};
foreach my $token_name (keys %{ $api_key }) {
$tokens->{$token_name}->{token} = $api_key->{$token_name};
$tokens->{$token_name}->{prefix} = $api_key_prefix->{$token_name};
$tokens->{$token_name}->{in} = $api_key_in->{$token_name};
foreach my $token_name (keys %{ $self->{api_key} }) {
$tokens->{$token_name}->{token} = $self->{api_key}{$token_name};
$tokens->{$token_name}->{prefix} = $self->{api_key_prefix}{$token_name};
$tokens->{$token_name}->{in} = $self->{api_key_in}{$token_name};
}
return $tokens;
}
sub clear_tokens {
my $class = shift;
my %tokens = %{$class->get_tokens}; # copy
my $self = shift;
my %tokens = %{$self->get_tokens}; # copy
$username = undef;
$password = undef;
$access_token = undef;
$self->{username} = '';
$self->{password} = '';
$self->{access_token} = '';
$api_key = {};
$api_key_prefix = {};
$api_key_in = {};
$self->{api_key} = {};
$self->{api_key_prefix} = {};
$self->{api_key_in} = {};
return \%tokens;
}
sub accept_tokens {
my ($class, $tokens) = @_;
my ($self, $tokens) = @_;
foreach my $known_name (qw(username password access_token)) {
next unless $tokens->{$known_name};
eval "\$$known_name = delete \$tokens->{\$known_name}";
die $@ if $@;
$self->{$known_name} = delete $tokens->{$known_name};
}
foreach my $token_name (keys %$tokens) {
$api_key->{$token_name} = $tokens->{$token_name}->{token};
if ($tokens->{$token_name}->{prefix}) {
$api_key_prefix->{$token_name} = $tokens->{$token_name}->{prefix};
$self->{api_key}{$token_name} = $tokens->{$token_name}{token};
if ($tokens->{$token_name}{prefix}) {
$self->{api_key_prefix}{$token_name} = $tokens->{$token_name}{prefix};
}
my $in = $tokens->{$token_name}->{in} || 'head';
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
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.
- `${{moduleName}}::Configuration::username`
- `$cfg->{username}`
String. The username for basic auth.
- `${{moduleName}}::Configuration::password`
- `$cfg->{password}`
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).
${{moduleName}}::Configuration::api_key = {
$cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd',
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
all api keys require a prefix.
${{moduleName}}::Configuration::api_key_prefix = {
$cfg->{api_key_prefix} = {
secretKey => 'string',
anotherKey => 'same or some other string',
};
- `${{moduleName}}::Configuration::access_token`
- `$cfg->{access_token}`
String. The OAuth access token.
@@ -133,8 +133,7 @@ as follows, but you don't need to know about this.
## `base_url`
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
created yet) the current value of `base_url`.
returns the current value of `base_url`.
## `api_factory`
@@ -253,21 +252,22 @@ use warnings;
{{/model}}{{/models}}
# for displaying the API response data
use Data::Dumper;
use {{{moduleName}}}::Configuration;
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}}

View File

@@ -34,8 +34,8 @@ has tokens => ( is => 'ro',
);
has _cfg => ( is => 'ro',
isa => 'Str',
default => '{{moduleName}}::Configuration',
isa => '{{moduleName}}::Configuration',
default => sub { {{moduleName}}::Configuration->new() },
);
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
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.
=over 4
=item C<${{moduleName}}::Configuration::username>
=item C<$cfg-\>{username}>
String. The username for basic auth.
=item C<${{moduleName}}::Configuration::password>
=item C<$cfg-\>{password}>
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).
${{moduleName}}::Configuration::api_key = {
$cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd',
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
all api keys require a prefix.
${{moduleName}}::Configuration::api_key_prefix = {
$cfg->{api_key_prefix} = {
secretKey => 'string',
anotherKey => 'same or some other string',
};
=item C<${{moduleName}}::Configuration::access_token>
=item C<$config-\>{access_token}>
String. The OAuth access token.
@@ -239,8 +239,7 @@ String. The OAuth access token.
=head2 C<base_url>
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
created yet) the current value of C<base_url>.
returns the current value of C<base_url>.
=head2 C<api_factory>

View File

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

View File

@@ -25,21 +25,21 @@ Method | HTTP request | Description
### Example
```perl
use Data::Dumper;
use {{{moduleName}}}::Configuration;
use {{moduleName}}::{{classname}};
my $api_instance = {{moduleName}}::{{classname}}->new(
{{#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}}
# 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}}
);
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}}