Flatten entire API into a single class

Added a Moose role which flattens all the individual endpoint APIs into
a single class.
This commit is contained in:
Dave Baird
2015-11-07 20:37:13 +01:00
parent 107452d406
commit 6c19f0c26c
8 changed files with 165 additions and 24 deletions

View File

@@ -305,8 +305,9 @@ sub update_params_for_auth {
my ($self, $header_params, $query_params, $auth_settings) = @_;
# we can defer to the application
if ($self->{auth_setup_handler} && ref($self->{auth_setup_handler}) eq 'CODE') {
$self->{auth_setup_handler}->( api_client => $self,
if ($self->{auth_setup_handler_object}) {
$self->{auth_setup_handler_object}->auth_setup_handler(
api_client => $self,
header_params => $header_params,
query_params => $query_params,
auth_settings => $auth_settings, # presumably this won't be defined if we're doing it this way

View File

@@ -49,18 +49,17 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
All parameters are optional, and are passed to and stored on the api_client object.
base_url: supply this to change the default base URL taken from the Swagger definition.
base_url: (optional)
supply this to change the default base URL taken from the Swagger definition.
auth_setup_handler: a coderef you can supply to set up authentication.
The coderef receives a hashref with keys: api_client, query_params, header_params, auth_settings.
auth_setup_handler_object: (optional)
An object (or class name) that implements an auth_setup_handler() method.
my $api_factory = WWW::SwaggerClient::ApiFactory->new( auth_setup_handler => \&setup_auth ); );
sub setup_auth {
my %p = @_;
$p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd';
}
If set, the auth_setup_handler() method will be called on the object, and
passed a hashref with keys: api_client, query_params, header_params, auth_settings.
The method should implement the required auth policy, for example, by setting
secret keys in the header, or username and password in the URL, etc.
=cut
@@ -95,4 +94,18 @@ sub get_api {
sub api_client { $_[0]->{api_client} }
=head1 apis_available()
=cut
sub apis_available { return map { $_ =~ s/Api$//; $_ } sort keys %_apis }
=head1 classname_for()
=cut
sub classname_for {
my ($self, $api_name) = @_;
return $_apis{"${api_name}Api"};
}
1;

View File

@@ -0,0 +1,54 @@
package WWW::SwaggerClient::Role;
use utf8;
use Moose::Role;
use namespace::autoclean;
use Class::Inspector;
use WWW::SwaggerClient::ApiFactory;
requires 'auth_setup_handler';
has base_url => ( is => 'ro',
required => 0,
isa => 'Str',
);
has api_factory => ( is => 'ro',
isa => 'WWW::SwaggerClient::ApiFactory',
builder => '_build_af',
lazy => 1,
);
sub BUILD {
my $self = shift;
my %outsiders = map {$_ => 1} qw( croak );
foreach my $name ($self->api_factory->apis_available) {
my $att_name = sprintf "%s_api", lc($name);
my $api_class = $self->api_factory->classname_for($name);
my $methods = Class::Inspector->methods($api_class, 'expanded');
my @local_methods = grep {! $outsiders{$_}} map {$_->[2]} grep {$_->[1] eq $api_class} @$methods;
$self->meta->add_attribute( $att_name => (
is => 'ro',
isa => $api_class,
default => sub {$self->api_factory->get_api($name)},
lazy => 1,
handles => \@local_methods,
) );
}
}
sub _build_af {
my $self = shift;
my %args = ( auth_setup_handler_object => $self );
$args{base_url} = $self->base_url if $self->base_url;
return WWW::SwaggerClient::ApiFactory->new(%args);
}
1;