Apply the singleton pattern to ApiClient

- make the api_client a singleton
- remove it from the configuration namespace
This commit is contained in:
Dave Baird 2015-11-10 09:47:58 +01:00
parent c6cd88b281
commit 521b73b3ef
14 changed files with 25 additions and 18 deletions

View File

@ -20,7 +20,9 @@ use Module::Runtime qw(use_module);
use WWW::{{moduleName}}::Configuration; use WWW::{{moduleName}}::Configuration;
sub new use base 'Class::Singleton';
sub _new_instance
{ {
my $class = shift; my $class = shift;
my (%args) = ( my (%args) = (

View File

@ -65,7 +65,7 @@ my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ }
sub new { sub new {
my ($class, %p) = (shift, @_); my ($class, %p) = (shift, @_);
$p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p); $p{api_client} = WWW::{{moduleName}}::ApiClient->instance(%p);
return bless \%p, $class; return bless \%p, $class;
} }

View File

@ -10,7 +10,6 @@ use Carp;
use constant VERSION => '{{moduleVersion}}'; use constant VERSION => '{{moduleVersion}}';
# class/static variables # class/static variables
our $api_client;
our $http_timeout = 180; our $http_timeout = 180;
our $http_user_agent = 'Perl-Swagger'; our $http_user_agent = 'Perl-Swagger';

View File

@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO
sub new { sub new {
my $class = shift; my $class = shift;
my $default_api_client = $WWW::{{moduleName}}::Configuration::api_client ? $WWW::{{moduleName}}::Configuration::api_client : WWW::{{moduleName}}::ApiClient->new;
my (%self) = ( my (%self) = (
'api_client' => $default_api_client, 'api_client' => WWW::{{moduleName}}::ApiClient->instance,
@_ @_
); );

View File

@ -10,7 +10,7 @@ WWW::SwaggerClient::ApiFactory for non-Moosey usage.
# SYNOPSIS # SYNOPSIS
The Perl Swagger Codegen project builds a library of Perl modules to interact with The Perl Swagger Codegen project builds a library of Perl modules to interact with
a web service defined by a Swagger specification. See below for how to built the a web service defined by a Swagger specification. See below for how to build the
library. library.
This module provides an interface to the generated library. All the classes, This module provides an interface to the generated library. All the classes,

View File

@ -20,7 +20,9 @@ use Module::Runtime qw(use_module);
use WWW::SwaggerClient::Configuration; use WWW::SwaggerClient::Configuration;
sub new use base 'Class::Singleton';
sub _new_instance
{ {
my $class = shift; my $class = shift;
my (%args) = ( my (%args) = (

View File

@ -65,7 +65,7 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
sub new { sub new {
my ($class, %p) = (shift, @_); my ($class, %p) = (shift, @_);
$p{api_client} = WWW::SwaggerClient::ApiClient->new(%p); $p{api_client} = WWW::SwaggerClient::ApiClient->instance(%p);
return bless \%p, $class; return bless \%p, $class;
} }

View File

@ -10,7 +10,6 @@ use Carp;
use constant VERSION => '1.0.0'; use constant VERSION => '1.0.0';
# class/static variables # class/static variables
our $api_client;
our $http_timeout = 180; our $http_timeout = 180;
our $http_user_agent = 'Perl-Swagger'; our $http_user_agent = 'Perl-Swagger';

View File

@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO
sub new { sub new {
my $class = shift; my $class = shift;
my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new;
my (%self) = ( my (%self) = (
'api_client' => $default_api_client, 'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_ @_
); );

View File

@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO
sub new { sub new {
my $class = shift; my $class = shift;
my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new;
my (%self) = ( my (%self) = (
'api_client' => $default_api_client, 'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_ @_
); );

View File

@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO
sub new { sub new {
my $class = shift; my $class = shift;
my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new;
my (%self) = ( my (%self) = (
'api_client' => $default_api_client, 'api_client' => WWW::SwaggerClient::ApiClient->instance,
@_ @_
); );

View File

@ -1,4 +1,4 @@
use Test::More tests => 37; use Test::More tests => 38;
use Test::Exception; use Test::Exception;
use lib 'lib'; use lib 'lib';
@ -11,12 +11,18 @@ 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->new('base_url' => 'http://testing'); my $api_client = WWW::SwaggerClient::ApiClient->instance('base_url' => 'http://testing');
my $pet_api = WWW::SwaggerClient::PetApi->new('api_client' => $api_client); my $pet_api = WWW::SwaggerClient::PetApi->new('api_client' => $api_client);
is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client'; is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base URL from api client';
my $api = WWW::SwaggerClient::PetApi->new(); 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
# its base URL halfway through
$api->{api_client}->{base_url} = 'http://petstore.swagger.io/v2';
is $api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; is $api->{api_client}->{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

View File

@ -15,7 +15,7 @@ use_ok('WWW::SwaggerClient::Object::Category');
use_ok('WWW::SwaggerClient::Object::User'); use_ok('WWW::SwaggerClient::Object::User');
my $api_client = WWW::SwaggerClient::ApiClient->new(); my $api_client = WWW::SwaggerClient::ApiClient->instance();
my $store_api = WWW::SwaggerClient::StoreApi->new('api_client' => $api_client); my $store_api = WWW::SwaggerClient::StoreApi->new('api_client' => $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}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client';

View File

@ -15,6 +15,9 @@ is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base UR
$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
# its base URL halfway through
$pet_api->{api_client}->{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}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client';
# test accessor methods # test accessor methods