Add ApiFactory class and proper accessors on object classes

ApiFactory provides a get_api() method to generate API objects without
having to hard-code class names. All API and object classes are loaded
automatically.

Also, added proper accessors for attributes of object classes.
This commit is contained in:
Dave Baird
2015-11-03 17:58:53 +01:00
parent 8428e7963b
commit 37b123530f
14 changed files with 192 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
package WWW::SwaggerClient::ApiFactory;
use strict;
use warnings;
use utf8;
use Carp;
use Module::Find;
usesub WWW::SwaggerClient::Object;
use WWW::SwaggerClient::ApiClient;
=head1 Name
WWW::SwaggerClient::ApiFactory - constructs APIs to retrieve SwaggerClient objects
=head1 Synopsis
package My::Petstore::App;
use WWW::SwaggerClient::ApiFactory;
my $api_factory = WWW::SwaggerClient::ApiFactory->new( base_url => 'http://petstore.swagger.io/v2',
..., # other args for ApiClient constructor
);
# later...
my $pet_api = $api_factory->get_api('Pet');
# $pet_api isa WWW::SwaggerClient::PetApi
my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id);
# object attributes have proper accessors:
printf "Pet's name is %s", $pet->name;
# change the value stored on the object:
$pet->name('Dave');
=cut
# Load all the API classes and construct a lookup table at startup time
my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::SwaggerClient';
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::SwaggerClient::ApiClient->new(%p);
return bless \%p, $class;
}
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);
}
sub _api_client { $_[0]->{api_client} }
1;

View File

@@ -31,6 +31,8 @@ my $attribute_map = {
'message' => 'message'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;

View File

@@ -11,6 +11,8 @@ use Log::Any qw($log);
use Date::Parse;
use DateTime;
use base "Class::Accessor";
#
#

View File

@@ -29,6 +29,8 @@ my $attribute_map = {
'name' => 'name'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;

View File

@@ -37,6 +37,8 @@ my $attribute_map = {
'complete' => 'complete'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;

View File

@@ -37,6 +37,8 @@ my $attribute_map = {
'status' => 'status'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;

View File

@@ -29,6 +29,8 @@ my $attribute_map = {
'name' => 'name'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;

View File

@@ -41,6 +41,8 @@ my $attribute_map = {
'user_status' => 'userStatus'
};
__PACKAGE__->mk_accessors(keys %$attribute_map);
# new object
sub new {
my ($class, %args) = @_;