forked from loafle/openapi-generator-original
[Perl] remove singleton (#5353)
- removed Singleton from ApiClient fixes #5336
This commit is contained in:
@@ -39,22 +39,25 @@ use Module::Runtime qw(use_module);
|
||||
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base 'Class::Singleton';
|
||||
|
||||
sub _new_instance
|
||||
{
|
||||
sub new {
|
||||
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) = (
|
||||
'ua' => LWP::UserAgent->new,
|
||||
'base_url' => 'http://petstore.swagger.io:80/v2',
|
||||
@_
|
||||
'config' => $config,
|
||||
);
|
||||
|
||||
return bless \%args, $class;
|
||||
}
|
||||
|
||||
sub _cfg {'WWW::SwaggerClient::Configuration'}
|
||||
|
||||
# Set 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);
|
||||
|
||||
|
||||
my $_url = $self->{base_url} . $resource_path;
|
||||
my $_url = $self->{config}{base_url} . $resource_path;
|
||||
|
||||
# build query
|
||||
if (%$query_params) {
|
||||
@@ -138,8 +141,8 @@ sub call_api {
|
||||
else {
|
||||
}
|
||||
|
||||
$self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout);
|
||||
$self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::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);
|
||||
@@ -313,11 +316,11 @@ sub get_api_key_with_prefix
|
||||
{
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -347,14 +350,14 @@ sub update_params_for_auth {
|
||||
}
|
||||
elsif ($auth eq 'http_basic_test') {
|
||||
|
||||
if ($WWW::SwaggerClient::Configuration::username || $WWW::SwaggerClient::Configuration::password) {
|
||||
$header_params->{'Authorization'} = 'Basic ' . encode_base64($WWW::SwaggerClient::Configuration::username . ":" . $WWW::SwaggerClient::Configuration::password);
|
||||
if ($self->{config}{username} || $self->{config}{password}) {
|
||||
$header_params->{'Authorization'} = 'Basic ' . encode_base64($self->{config}{username} . ":" . $self->{config}{password});
|
||||
}
|
||||
}
|
||||
elsif ($auth eq 'petstore_auth') {
|
||||
|
||||
if ($WWW::SwaggerClient::Configuration::access_token) {
|
||||
$header_params->{'Authorization'} = 'Bearer ' . $WWW::SwaggerClient::Configuration::access_token;
|
||||
if ($self->{config}{access_token}) {
|
||||
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -370,7 +373,7 @@ elsif ($auth eq 'petstore_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
|
||||
|
||||
@@ -62,19 +62,28 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
|
||||
grep {$_ =~ /Api$/}
|
||||
usesub 'WWW::SwaggerClient';
|
||||
|
||||
=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 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.
|
||||
|
||||
See L<WWW::SwaggerClient::ApiClient> and L<WWW::SwaggerClient::Configuration> for valid paramters
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ($class, %p) = (shift, @_);
|
||||
$p{api_client} = WWW::SwaggerClient::ApiClient->instance(%p);
|
||||
return bless \%p, $class;
|
||||
my ($class) = shift;
|
||||
|
||||
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)
|
||||
@@ -91,7 +100,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()
|
||||
|
||||
@@ -28,71 +28,142 @@ use Carp;
|
||||
|
||||
use constant VERSION => '1.0.0';
|
||||
|
||||
# class/static variables
|
||||
our $http_timeout = 180;
|
||||
our $http_user_agent = 'Swagger-Codegen/1.0.0/perl';
|
||||
=head1 Name
|
||||
|
||||
# authentication setting
|
||||
our $api_key = {};
|
||||
our $api_key_prefix = {};
|
||||
our $api_key_in = {};
|
||||
WWW::SwaggerClient::Configuration - holds the configuration for all WWW::SwaggerClient 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: 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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,25 +28,22 @@ use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
use WWW::SwaggerClient::ApiClient;
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base "Class::Data::Inheritable";
|
||||
|
||||
__PACKAGE__->mk_classdata('method_documentation' => {});
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my (%self) = (
|
||||
'api_client' => WWW::SwaggerClient::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 'WWW::SwaggerClient::ApiClient' ) {
|
||||
$api_client = $_[0];
|
||||
} else {
|
||||
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
|
||||
}
|
||||
|
||||
bless \%self, $class;
|
||||
bless { api_client => $api_client }, $class;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -28,25 +28,22 @@ use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
use WWW::SwaggerClient::ApiClient;
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base "Class::Data::Inheritable";
|
||||
|
||||
__PACKAGE__->mk_classdata('method_documentation' => {});
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my (%self) = (
|
||||
'api_client' => WWW::SwaggerClient::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 'WWW::SwaggerClient::ApiClient' ) {
|
||||
$api_client = $_[0];
|
||||
} else {
|
||||
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
|
||||
}
|
||||
|
||||
bless \%self, $class;
|
||||
bless { api_client => $api_client }, $class;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -28,25 +28,22 @@ use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
use WWW::SwaggerClient::ApiClient;
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base "Class::Data::Inheritable";
|
||||
|
||||
__PACKAGE__->mk_classdata('method_documentation' => {});
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my (%self) = (
|
||||
'api_client' => WWW::SwaggerClient::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 'WWW::SwaggerClient::ApiClient' ) {
|
||||
$api_client = $_[0];
|
||||
} else {
|
||||
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
|
||||
}
|
||||
|
||||
bless \%self, $class;
|
||||
bless { api_client => $api_client }, $class;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +47,8 @@ has tokens => ( is => 'ro',
|
||||
);
|
||||
|
||||
has _cfg => ( is => 'ro',
|
||||
isa => 'Str',
|
||||
default => 'WWW::SwaggerClient::Configuration',
|
||||
isa => 'WWW::SwaggerClient::Configuration',
|
||||
default => sub { WWW::SwaggerClient::Configuration->new() },
|
||||
);
|
||||
|
||||
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
|
||||
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.
|
||||
|
||||
=over 4
|
||||
|
||||
=item C<$WWW::SwaggerClient::Configuration::username>
|
||||
=item C<$cfg-\>{username}>
|
||||
|
||||
String. The username for basic auth.
|
||||
|
||||
=item C<$WWW::SwaggerClient::Configuration::password>
|
||||
=item C<$cfg-\>{password}>
|
||||
|
||||
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).
|
||||
|
||||
$WWW::SwaggerClient::Configuration::api_key = {
|
||||
$cfg->{api_key} = {
|
||||
secretKey => 'aaaabbbbccccdddd',
|
||||
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
|
||||
all api keys require a prefix.
|
||||
|
||||
$WWW::SwaggerClient::Configuration::api_key_prefix = {
|
||||
$cfg->{api_key_prefix} = {
|
||||
secretKey => 'string',
|
||||
anotherKey => 'same or some other string',
|
||||
};
|
||||
|
||||
=item C<$WWW::SwaggerClient::Configuration::access_token>
|
||||
=item C<$config-\>{access_token}>
|
||||
|
||||
String. The OAuth access token.
|
||||
|
||||
@@ -244,8 +244,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>
|
||||
|
||||
|
||||
@@ -28,25 +28,22 @@ use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
use WWW::SwaggerClient::ApiClient;
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base "Class::Data::Inheritable";
|
||||
|
||||
__PACKAGE__->mk_classdata('method_documentation' => {});
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my (%self) = (
|
||||
'api_client' => WWW::SwaggerClient::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 'WWW::SwaggerClient::ApiClient' ) {
|
||||
$api_client = $_[0];
|
||||
} else {
|
||||
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
|
||||
}
|
||||
|
||||
bless \%self, $class;
|
||||
bless { api_client => $api_client }, $class;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -28,25 +28,22 @@ use Carp qw( croak );
|
||||
use Log::Any qw($log);
|
||||
|
||||
use WWW::SwaggerClient::ApiClient;
|
||||
use WWW::SwaggerClient::Configuration;
|
||||
|
||||
use base "Class::Data::Inheritable";
|
||||
|
||||
__PACKAGE__->mk_classdata('method_documentation' => {});
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my (%self) = (
|
||||
'api_client' => WWW::SwaggerClient::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 'WWW::SwaggerClient::ApiClient' ) {
|
||||
$api_client = $_[0];
|
||||
} else {
|
||||
$api_client = WWW::SwaggerClient::ApiClient->new(@_);
|
||||
}
|
||||
|
||||
bless \%self, $class;
|
||||
bless { api_client => $api_client }, $class;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user