From 521b73b3ef33b74b2db9efca89ec9f159f66910b Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Tue, 10 Nov 2015 09:47:58 +0100 Subject: [PATCH] Apply the singleton pattern to ApiClient - make the api_client a singleton - remove it from the configuration namespace --- .../src/main/resources/perl/ApiClient.mustache | 4 +++- .../src/main/resources/perl/ApiFactory.mustache | 2 +- .../src/main/resources/perl/Configuration.mustache | 1 - .../src/main/resources/perl/api.mustache | 3 +-- samples/client/petstore/perl/README.md | 2 +- .../petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm | 4 +++- .../petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm | 2 +- .../perl/lib/WWW/SwaggerClient/Configuration.pm | 1 - .../petstore/perl/lib/WWW/SwaggerClient/PetApi.pm | 3 +-- .../petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm | 3 +-- .../petstore/perl/lib/WWW/SwaggerClient/UserApi.pm | 3 +-- samples/client/petstore/perl/t/01_pet_api.t | 10 ++++++++-- samples/client/petstore/perl/t/02_store_api.t | 2 +- samples/client/petstore/perl/t/03_api_factory.t | 3 +++ 14 files changed, 25 insertions(+), 18 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache index 89e5650759a..ae8dce3d351 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache @@ -20,7 +20,9 @@ use Module::Runtime qw(use_module); use WWW::{{moduleName}}::Configuration; -sub new +use base 'Class::Singleton'; + +sub _new_instance { my $class = shift; my (%args) = ( diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache index 724ad63b048..cb619091ca3 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache @@ -65,7 +65,7 @@ my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ } sub new { my ($class, %p) = (shift, @_); - $p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p); + $p{api_client} = WWW::{{moduleName}}::ApiClient->instance(%p); return bless \%p, $class; } diff --git a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache index d0b8b2c0e68..d0cc157647f 100644 --- a/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/Configuration.mustache @@ -10,7 +10,6 @@ use Carp; use constant VERSION => '{{moduleVersion}}'; # class/static variables -our $api_client; our $http_timeout = 180; our $http_user_agent = 'Perl-Swagger'; diff --git a/modules/swagger-codegen/src/main/resources/perl/api.mustache b/modules/swagger-codegen/src/main/resources/perl/api.mustache index a93cbc2b4a4..e31f2f32d6c 100644 --- a/modules/swagger-codegen/src/main/resources/perl/api.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/api.mustache @@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO sub new { my $class = shift; - my $default_api_client = $WWW::{{moduleName}}::Configuration::api_client ? $WWW::{{moduleName}}::Configuration::api_client : WWW::{{moduleName}}::ApiClient->new; my (%self) = ( - 'api_client' => $default_api_client, + 'api_client' => WWW::{{moduleName}}::ApiClient->instance, @_ ); diff --git a/samples/client/petstore/perl/README.md b/samples/client/petstore/perl/README.md index 54c8b861eda..a04d5a0aad6 100644 --- a/samples/client/petstore/perl/README.md +++ b/samples/client/petstore/perl/README.md @@ -10,7 +10,7 @@ WWW::SwaggerClient::ApiFactory for non-Moosey usage. # SYNOPSIS The Perl Swagger Codegen project builds a library of Perl modules to interact with -a web service defined by a Swagger specification. See below for how to built the +a web service defined by a Swagger specification. See below for how to build the library. This module provides an interface to the generated library. All the classes, diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index 8b51bd3c97b..d3568c812e4 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -20,7 +20,9 @@ use Module::Runtime qw(use_module); use WWW::SwaggerClient::Configuration; -sub new +use base 'Class::Singleton'; + +sub _new_instance { my $class = shift; my (%args) = ( diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm index bf3617d86f1..77c23fdb2b3 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm @@ -65,7 +65,7 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ } sub new { my ($class, %p) = (shift, @_); - $p{api_client} = WWW::SwaggerClient::ApiClient->new(%p); + $p{api_client} = WWW::SwaggerClient::ApiClient->instance(%p); return bless \%p, $class; } diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm index 6a4afe254e0..91670003518 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Configuration.pm @@ -10,7 +10,6 @@ use Carp; use constant VERSION => '1.0.0'; # class/static variables -our $api_client; our $http_timeout = 180; our $http_user_agent = 'Perl-Swagger'; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm index 0404c9c74be..813e73468a2 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/PetApi.pm @@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( - 'api_client' => $default_api_client, + 'api_client' => WWW::SwaggerClient::ApiClient->instance, @_ ); diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm index d8cd0cbff7d..533c2e77a26 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/StoreApi.pm @@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( - 'api_client' => $default_api_client, + 'api_client' => WWW::SwaggerClient::ApiClient->instance, @_ ); diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm index 18d69ed5883..5d1f7f33f1b 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/UserApi.pm @@ -38,9 +38,8 @@ __PACKAGE__->mk_classdata('class_documentation' => {}); # TODO sub new { my $class = shift; - my $default_api_client = $WWW::SwaggerClient::Configuration::api_client ? $WWW::SwaggerClient::Configuration::api_client : WWW::SwaggerClient::ApiClient->new; my (%self) = ( - 'api_client' => $default_api_client, + 'api_client' => WWW::SwaggerClient::ApiClient->instance, @_ ); diff --git a/samples/client/petstore/perl/t/01_pet_api.t b/samples/client/petstore/perl/t/01_pet_api.t index 5460c3282b2..c7bcdf98a0a 100644 --- a/samples/client/petstore/perl/t/01_pet_api.t +++ b/samples/client/petstore/perl/t/01_pet_api.t @@ -1,4 +1,4 @@ -use Test::More tests => 37; +use Test::More tests => 38; use Test::Exception; use lib 'lib'; @@ -11,12 +11,18 @@ use_ok('WWW::SwaggerClient::Object::Pet'); use_ok('WWW::SwaggerClient::Object::Tag'); 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); is $pet_api->{api_client}->{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 +# its base URL halfway through +$api->{api_client}->{base_url} = 'http://petstore.swagger.io/v2'; + is $api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; # test select_header_content_type diff --git a/samples/client/petstore/perl/t/02_store_api.t b/samples/client/petstore/perl/t/02_store_api.t index dfee5c46839..39b18381b0e 100644 --- a/samples/client/petstore/perl/t/02_store_api.t +++ b/samples/client/petstore/perl/t/02_store_api.t @@ -15,7 +15,7 @@ use_ok('WWW::SwaggerClient::Object::Category'); 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); is $store_api->{api_client}->{base_url}, 'http://petstore.swagger.io/v2', 'get the default base URL from api client'; diff --git a/samples/client/petstore/perl/t/03_api_factory.t b/samples/client/petstore/perl/t/03_api_factory.t index 2e6ee6317f9..a06805e2bcf 100644 --- a/samples/client/petstore/perl/t/03_api_factory.t +++ b/samples/client/petstore/perl/t/03_api_factory.t @@ -15,6 +15,9 @@ is $pet_api->{api_client}->{base_url}, 'http://testing', 'get the proper base UR $api_factory = WWW::SwaggerClient::ApiFactory->new; $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'; # test accessor methods