Skip to content

Commit

Permalink
[6313] Add imports to perl objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Bordowitz committed Sep 14, 2017
1 parent 8067612 commit 11d9f02
Show file tree
Hide file tree
Showing 63 changed files with 781 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ use Log::Any qw($log);
use Date::Parse;
use DateTime;

{{#imports}}
use {{moduleName}}::Object::{{.}};
{{/imports}}

use base ("Class::Accessor", "Class::Data::Inheritable");


Expand Down
24 changes: 12 additions & 12 deletions samples/client/petstore-security-test/perl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,37 @@ you are accessing. Usually `prefix` and `in` will be determined by the code gene
the spec and you will not need to set them at run time. If not, `in` will
default to 'head' and `prefix` to the empty string.

The tokens will be placed in the `WWW::SwaggerClient::Configuration` namespace
The tokens will be placed in a L<WWW::SwaggerClient::Configuration> instance
as follows, but you don't need to know about this.

- `$WWW::SwaggerClient::Configuration::username`
- `$cfg->{username}`

String. The username for basic auth.

- `$WWW::SwaggerClient::Configuration::password`
- `$cfg->{password}`

String. The password for basic auth.

- `$WWW::SwaggerClient::Configuration::api_key`
- `$cfg->{api_key}`

Hashref. Keyed on the name of each key (there can be multiple tokens).

$WWW::SwaggerClient::Configuration::api_key = {
$cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};

- `$WWW::SwaggerClient::Configuration::api_key_prefix`
- `$cfg->{api_key_prefix}`

Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix.

$WWW::SwaggerClient::Configuration::api_key_prefix = {
$cfg->{api_key_prefix} = {
secretKey => 'string',
anotherKey => 'same or some other string',
};

- `$WWW::SwaggerClient::Configuration::access_token`
- `$cfg->{access_token}`

String. The OAuth access token.

Expand All @@ -127,8 +127,7 @@ as follows, but you don't need to know about this.
## `base_url`

The generated code has the `base_url` already set as a default value. This method
returns (and optionally sets, but only if the API client has not been
created yet) the current value of `base_url`.
returns the current value of `base_url`.

## `api_factory`

Expand Down Expand Up @@ -247,10 +246,11 @@ use WWW::SwaggerClient::Object::ModelReturn;
# for displaying the API response data
use Data::Dumper;
use WWW::SwaggerClient::Configuration;
use WWW::SwaggerClient::;
my $api_instance = WWW::SwaggerClient::FakeApi->new();
my $api_instance = WWW::SwaggerClient::->new(
);
my $test code inject */ &#39; &quot; &#x3D;end __ \r\n \n \r = 'test code inject */ ' " =end __ \r\n \n \r_example'; # string | To test code injection */ ' \" =_end -- \\r\\n \\n \\r
eval {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,37 @@ you are accessing. Usually `prefix` and `in` will be determined by the code gene
the spec and you will not need to set them at run time. If not, `in` will
default to 'head' and `prefix` to the empty string.

The tokens will be placed in the `Something::Deep::Configuration` namespace
The tokens will be placed in a L<Something::Deep::Configuration> instance
as follows, but you don't need to know about this.

- `$Something::Deep::Configuration::username`
- `$cfg->{username}`

String. The username for basic auth.

- `$Something::Deep::Configuration::password`
- `$cfg->{password}`

String. The password for basic auth.

- `$Something::Deep::Configuration::api_key`
- `$cfg->{api_key}`

Hashref. Keyed on the name of each key (there can be multiple tokens).

$Something::Deep::Configuration::api_key = {
$cfg->{api_key} = {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};

- `$Something::Deep::Configuration::api_key_prefix`
- `$cfg->{api_key_prefix}`

Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix.

$Something::Deep::Configuration::api_key_prefix = {
$cfg->{api_key_prefix} = {
secretKey => 'string',
anotherKey => 'same or some other string',
};

- `$Something::Deep::Configuration::access_token`
- `$cfg->{access_token}`

String. The OAuth access token.

Expand All @@ -127,8 +127,7 @@ as follows, but you don't need to know about this.
## `base_url`

The generated code has the `base_url` already set as a default value. This method
returns (and optionally sets, but only if the API client has not been
created yet) the current value of `base_url`.
returns the current value of `base_url`.

## `api_factory`

Expand Down Expand Up @@ -247,10 +246,11 @@ use Something::Deep::Object::ModelReturn;
# for displaying the API response data
use Data::Dumper;
use Something::Deep::Configuration;
use Something::Deep::;
my $api_instance = Something::Deep::FakeApi->new();
my $api_instance = Something::Deep::->new(
);
my $test code inject */ &#39; &quot; &#x3D;end __ \r\n \n \r = 'test code inject */ ' " =end __ \r\n \n \r_example'; # string | To test code injection */ ' \" =_end -- \\r\\n \\n \\r
eval {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ To test code injection */ ' \" =_end -- \\r\\n \\n \\r
### Example
```perl
use Data::Dumper;
use Something::Deep::Configuration;
use Something::Deep::FakeApi;
my $api_instance = Something::Deep::FakeApi->new(
);

my $api_instance = Something::Deep::FakeApi->new();
my $test code inject */ &#39; &quot; &#x3D;end __ \r\n \n \r = 'test code inject */ ' " =end __ \r\n \n \r_example'; # string | To test code injection */ ' \" =_end -- \\r\\n \\n \\r

eval {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,25 @@ use Module::Runtime qw(use_module);

use Something::Deep::Configuration;

use base 'Class::Singleton';

sub _new_instance
{
sub new {
my $class = shift;

my $config;
if ( $_[0] && ref $_[0] && ref $_[0] eq 'Something::Deep::Configuration' ) {
$config = $_[0];
} else {
$config = Something::Deep::Configuration->new(@_);
}

my (%args) = (
'ua' => LWP::UserAgent->new,
'base_url' => 'https://petstore.swagger.io */ ' \" =_end -- \\r\\n \\n \\r/v2 */ ' \" =_end -- \\r\\n \\n \\r',
@_
'config' => $config,
);

return bless \%args, $class;
}

sub _cfg {'Something::Deep::Configuration'}
# Set the user agent of the API client
#
# @param string $user_agent The user agent of the API client
Expand Down Expand Up @@ -91,7 +94,7 @@ sub call_api {
$self->update_params_for_auth($header_params, $query_params, $auth_settings);


my $_url = $self->{base_url} . $resource_path;
my $_url = $self->{config}{base_url} . $resource_path;

# build query
if (%$query_params) {
Expand Down Expand Up @@ -138,8 +141,8 @@ sub call_api {
else {
}

$self->{ua}->timeout($self->{http_timeout} || $Something::Deep::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $Something::Deep::Configuration::http_user_agent);
$self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
$self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});

$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
Expand Down Expand Up @@ -313,11 +316,11 @@ sub get_api_key_with_prefix
{
my ($self, $key_name) = @_;

my $api_key = $Something::Deep::Configuration::api_key->{$key_name};
my $api_key = $self->{config}{api_key}{$key_name};

return unless $api_key;

my $prefix = $Something::Deep::Configuration::api_key_prefix->{$key_name};
my $prefix = $self->{config}{api_key_prefix}{$key_name};
return $prefix ? "$prefix $api_key" : $api_key;
}

Expand Down Expand Up @@ -347,8 +350,8 @@ sub update_params_for_auth {
}
elsif ($auth eq 'petstore_auth') {

if ($Something::Deep::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . $Something::Deep::Configuration::access_token;
if ($self->{config}{access_token}) {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}
}
else {
Expand All @@ -364,7 +367,7 @@ elsif ($auth eq 'petstore_auth') {
sub _global_auth_setup {
my ($self, $header_params, $query_params) = @_;

my $tokens = $self->_cfg->get_tokens;
my $tokens = $self->{config}->get_tokens;
return unless keys %$tokens;

# basic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,28 @@ my %_apis = map { $_ =~ /^Something::Deep::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'Something::Deep';

=head1 new()
=head1 new($api_client)
Any parameters are optional, and are passed to and stored on the api_client object.
base_url: (optional)
supply this to change the default base URL taken from the Swagger definition.
create a new Something::Deep::ApiFactory instance with the given Something::Deep::ApiClient instance.
=head1 new(%paramters)
Any parameters are optional, and are passed to and stored on the api_client object.
See L<Something::Deep::ApiClient> and L<Something::Deep::Configuration> for valid paramters
=cut

sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = Something::Deep::ApiClient->instance(%p);
return bless \%p, $class;
my ($class) = shift;

my $api_client;
if ($_[0] && ref $_[0] && ref $_[0] eq 'Something::Deep::ApiClient' ) {
$api_client = $_[0];
} else {
$api_client = Something::Deep::ApiClient->new(@_);
}
bless { api_client => $api_client }, $class;
}

=head1 get_api($which)
Expand All @@ -91,7 +100,7 @@ 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($self->api_client);
}

=head1 api_client()
Expand Down
Loading

0 comments on commit 11d9f02

Please sign in to comment.