diff --git a/.gitignore b/.gitignore index c904ba4a5fe..37b18e5e3cc 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,7 @@ samples/client/petstore/python/.projectile samples/client/petstore/python/.venv/ */.settings + +*.mustache~ +*.java~ +*.pm~ \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java index ba03f3944c5..ee2fa0ddbf3 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java @@ -95,6 +95,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm")); supportingFiles.add(new SupportingFile("BaseObject.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Object/BaseObject.pm")); supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiFactory.pm")); + supportingFiles.add(new SupportingFile("Role.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Role.pm")); } public CodegenType getTag() { diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache index 06aae8a0e55..02f9ff2221f 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache @@ -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 diff --git a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache index fc309c91b9c..724ad63b048 100644 --- a/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache @@ -49,18 +49,17 @@ my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $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::{{moduleName}}::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; diff --git a/modules/swagger-codegen/src/main/resources/perl/Role.mustache b/modules/swagger-codegen/src/main/resources/perl/Role.mustache new file mode 100644 index 00000000000..f349317c9ab --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/perl/Role.mustache @@ -0,0 +1,54 @@ +package WWW::{{moduleName}}::Role; +use utf8; + +use Moose::Role; +use namespace::autoclean; +use Class::Inspector; + +use WWW::{{moduleName}}::ApiFactory; + +requires 'auth_setup_handler'; + +has base_url => ( is => 'ro', + required => 0, + isa => 'Str', + ); + +has api_factory => ( is => 'ro', + isa => 'WWW::{{moduleName}}::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::{{moduleName}}::ApiFactory->new(%args); +} + + + +1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm index de4a3e2d2fb..028b983808a 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiClient.pm @@ -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 diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm index 701a22603c5..bf3617d86f1 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/ApiFactory.pm @@ -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; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm new file mode 100644 index 00000000000..c47f0908844 --- /dev/null +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Role.pm @@ -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;