Minor tidying up

Some documentation, fix a warning, added a couple of logger calls.
This commit is contained in:
Dave Baird 2015-11-04 20:27:23 +01:00
parent a93eb8aeaa
commit 4264b74e40
6 changed files with 104 additions and 10 deletions

View File

@ -118,10 +118,12 @@ sub call_api {
$self->{ua}->timeout($self->{http_timeout} || $WWW::{{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{moduleName}}::Configuration::http_user_agent);
$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
$log->debugf("RESPONSE: %s", $_response->as_string);
unless ($_response->is_success) {
croak("API Exception(".$_response->code."): ".$_response->message);
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
}
return $_response->content;
@ -294,7 +296,7 @@ sub get_api_key_with_prefix
}
}
# update hearder and query param based on authentication setting
# update header and query param based on authentication setting
#
# @param array $headerParams header parameters (by ref)
# @param array $queryParams query parameters (by ref)
@ -302,6 +304,16 @@ sub get_api_key_with_prefix
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,
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
);
return;
}
return if (!defined($auth_settings) || scalar(@$auth_settings) == 0);
# one endpoint can have more than 1 auth settings

View File

@ -45,19 +45,54 @@ my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::{{moduleName}}';
=head1 new()
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.
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.
my $api_factory = WWW::{{moduleName}}::ApiFactory->new( auth_setup_handler => \&setup_auth ); );
sub setup_auth {
my %p = @_;
$p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd';
}
=cut
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::{{moduleName}}::ApiClient->new(%p);
return bless \%p, $class;
}
=head1 get_api($which)
Returns an API object of the requested type.
$which is a nickname for the class:
WWW::FooBarClient::BazApi has nickname 'Baz'
=cut
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(api_client => $self->api_client);
}
sub _api_client { $_[0]->{api_client} }
=head1 api_client()
Returns the api_client object, should you ever need it.
=cut
sub api_client { $_[0]->{api_client} }
1;

View File

@ -100,7 +100,7 @@ sub {{nickname}} {
{{#formParams}}# form params
if ( exists $args{'{{paramName}}'} ) {
{{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'};
push $form_params->{'{{baseName}}'}, $args{'{{paramName}}'};
push @{$form_params->{'{{baseName}}'}}, $args{'{{paramName}}'};
{{/isFile}}
{{^isFile}}$form_params->{'{{baseName}}'} = $self->{api_client}->to_form_value($args{'{{paramName}}'});
{{/isFile}}

View File

@ -118,10 +118,12 @@ sub call_api {
$self->{ua}->timeout($self->{http_timeout} || $WWW::SwaggerClient::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::SwaggerClient::Configuration::http_user_agent);
$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
$log->debugf("RESPONSE: %s", $_response->as_string);
unless ($_response->is_success) {
croak("API Exception(".$_response->code."): ".$_response->message);
croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
}
return $_response->content;
@ -294,7 +296,7 @@ sub get_api_key_with_prefix
}
}
# update hearder and query param based on authentication setting
# update header and query param based on authentication setting
#
# @param array $headerParams header parameters (by ref)
# @param array $queryParams query parameters (by ref)
@ -302,6 +304,16 @@ sub get_api_key_with_prefix
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,
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
);
return;
}
return if (!defined($auth_settings) || scalar(@$auth_settings) == 0);
# one endpoint can have more than 1 auth settings

View File

@ -45,19 +45,54 @@ my %_apis = map { $_ =~ /^WWW::SwaggerClient::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::SwaggerClient';
=head1 new()
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.
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.
my $api_factory = WWW::SwaggerClient::ApiFactory->new( auth_setup_handler => \&setup_auth ); );
sub setup_auth {
my %p = @_;
$p{header_params}->{'X-SomeApp-FunkyKeyName'} = 'aaaaabbbbbcccccddddd';
}
=cut
sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::SwaggerClient::ApiClient->new(%p);
return bless \%p, $class;
}
=head1 get_api($which)
Returns an API object of the requested type.
$which is a nickname for the class:
WWW::FooBarClient::BazApi has nickname 'Baz'
=cut
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(api_client => $self->api_client);
}
sub _api_client { $_[0]->{api_client} }
=head1 api_client()
Returns the api_client object, should you ever need it.
=cut
sub api_client { $_[0]->{api_client} }
1;

View File

@ -515,7 +515,7 @@ sub upload_file {
}# form params
if ( exists $args{'file'} ) {
$form_params->{'file'} = [] unless defined $form_params->{'file'};
push $form_params->{'file'}, $args{'file'};
push @{$form_params->{'file'}}, $args{'file'};
}