Skip to content

Commit 866b546

Browse files
committed
make api client pluggable
1 parent 095771d commit 866b546

File tree

10 files changed

+166
-267
lines changed

10 files changed

+166
-267
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PerlClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public PerlClientCodegen() {
7777
typeMapping.put("map", "HASH");
7878

7979
supportingFiles.add(new SupportingFile("APIClient.mustache", "lib/WWW/" + invokerPackage, "APIClient.pm"));
80+
supportingFiles.add(new SupportingFile("Configuration.mustache", "lib/WWW/" + invokerPackage, "Configuration.pm"));
8081
supportingFiles.add(new SupportingFile("BaseObject.mustache", "lib/WWW/" + invokerPackage, "Object/BaseObject.pm"));
8182
}
8283

modules/swagger-codegen/src/main/resources/perl/APIClient.mustache

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ use URI::Escape;
1515
use Scalar::Util;
1616
use Log::Any qw($log);
1717
use Carp;
18-
use Switch;
1918
use Module::Runtime qw(use_module);
2019

21-
# class variables
22-
my $ua = LWP::UserAgent->new;
23-
my $http_user_agent = 'Perl-Swagger'; # HTTP user-agent
24-
my $http_timeout; #timeout
25-
my $base_url = "{{basePath}}";
26-
27-
2820
sub new
2921
{
3022
my $class = shift;
31-
my %args = @_;
23+
my (%args) = (
24+
'ua' => LWP::UserAgent->new,
25+
'base_url' => '{{basePath}}',
26+
@_
27+
);
3228
3329
return bless \%args, $class;
3430
}
@@ -38,20 +34,20 @@ sub new
3834
# @param string $user_agent The user agent of the API client
3935
#
4036
sub set_user_agent {
41-
my $user_agent = shift;
42-
$http_user_agent= $user_agent;
37+
my ($self, $user_agent) = @_;
38+
$self->{http_user_agent}= $user_agent;
4339
}
4440

4541
# Set timeout
4642
#
4743
# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
4844
#
4945
sub set_timeout {
50-
my $seconds = shift;
46+
my ($self, $seconds) = @_;
5147
if (!looks_like_number($seconds)) {
5248
croak('Timeout variable must be numeric.');
5349
}
54-
$http_timeout = $seconds;
50+
$self->{http_timeout} = $seconds;
5551
}
5652

5753
# make the HTTP request
@@ -67,7 +63,7 @@ sub call_api {
6763
6864
my $headers = HTTP::Headers->new(%$header_params);
6965
70-
my $_url = $base_url . $resource_path;
66+
my $_url = $self->{base_url} . $resource_path;
7167

7268
# build query
7369
if (%$query_params) {
@@ -80,43 +76,42 @@ sub call_api {
8076

8177
# Make the HTTP request
8278
my $_request;
83-
switch ($method) {
84-
case 'POST' {
79+
if ($method eq 'POST') {
8580
# multipart
8681
my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
8782
'form-data' : $header_params->{'Content-Type'};
8883

8984
$_request = POST($_url, Accept => $header_params->{Accept},
9085
Content_Type => $_content_type, Content => $_body_data);
91-
}
92-
case 'PUT' {
86+
}
87+
elsif ($method eq 'PUT') {
9388
# multipart
9489
my $_content_type = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
9590
'form-data' : $header_params->{'Content-Type'};
9691
$_request = PUT($_url, Accept => $header_params->{Accept},
9792
Content_Type => $_content_type, Content => $_body_data);
98-
}
99-
case 'GET' {
93+
}
94+
elsif ($method eq 'GET') {
10095
$_request = GET($_url, Accept => $header_params->{'Accept'},
10196
Content_Type => $header_params->{'Content-Type'});
102-
}
103-
case 'HEAD' {
97+
}
98+
elsif ($method eq 'HEAD') {
10499
$_request = HEAD($_url, Accept => $header_params->{'Accept'},
105100
Content_Type => $header_params->{'Content-Type'});
106-
}
107-
case 'DELETE' { #TODO support form data
101+
}
102+
elsif ($method eq 'DELETE') { #TODO support form data
108103
$_request = DELETE($_url, Accept => $header_params->{'Accept'},
109104
Content_Type => $header_params->{'Content-Type'}, Content => $_body_data);
110-
}
111-
case 'PATCH' { #TODO
112-
}
113-
105+
}
106+
elsif ($method eq 'PATCH') { #TODO
107+
}
108+
else {
114109
}
115110

116-
$ua->timeout($http_timeout);
117-
$ua->agent($http_user_agent);
111+
$self->{ua}->timeout($self->{http_timeout} || $WWW::{{invokerPackage}}::Configuration::http_timeout);
112+
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{invokerPackage}}::Configuration::http_user_agent);
118113

119-
my $_response = $ua->request($_request);
114+
my $_response = $self->{ua}->request($_request);
120115

121116
unless ($_response->is_success) {
122117
croak("API Exception(".$_response->code."): ".$_response->message);
@@ -126,41 +121,13 @@ sub call_api {
126121

127122
}
128123

129-
130-
# Build a JSON POST object
131-
sub sanitize_for_serialization
132-
{
133-
# my $data = shift;
134-
# if (is_scalar($data) || null === $data) {
135-
# $sanitized = $data;
136-
# } else if ($data instanceof \DateTime) {
137-
# $sanitized = $data->format(\DateTime::ISO8601);
138-
# } else if (is_array($data)) {
139-
# foreach ($data as $property => $value) {
140-
# $data[$property] = $this->sanitizeForSerialization($value);
141-
# }
142-
# $sanitized = $data;
143-
# } else if (is_object($data)) {
144-
# $values = array();
145-
# foreach (array_keys($data::$swaggerTypes) as $property) {
146-
# $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
147-
# }
148-
# $sanitized = $values;
149-
# } else {
150-
# $sanitized = (string)$data;
151-
# }
152-
#
153-
# return $sanitized;
154-
}
155-
156-
157124
# Take value and turn it into a string suitable for inclusion in
158125
# the path, by url-encoding.
159126
# @param string $value a string which will be part of the path
160127
# @return string the serialized object
161128
sub to_path_value {
162-
my $value = shift;
163-
return uri_escape(to_string($value));
129+
my ($self, $value) = @_;
130+
return uri_escape($self->to_string($value));
164131
}
165132

166133

@@ -171,11 +138,11 @@ sub to_path_value {
171138
# @param object $object an object to be serialized to a string
172139
# @return string the serialized object
173140
sub to_query_value {
174-
my $object = shift;
141+
my ($self, $object) = @_;
175142
if (is_array($object)) {
176143
return implode(',', $object);
177144
} else {
178-
return toString($object);
145+
return $self->to_string($object);
179146
}
180147
}
181148

@@ -186,8 +153,8 @@ sub to_query_value {
186153
# @param string $value a string which will be part of the header
187154
# @return string the header string
188155
sub to_header_value {
189-
my $value = shift;
190-
return to_string($value);
156+
my ($self, $value) = @_;
157+
return $self->to_string($value);
191158
}
192159

193160
# Take value and turn it into a string suitable for inclusion in
@@ -196,8 +163,8 @@ sub to_header_value {
196163
# @param string $value the value of the form parameter
197164
# @return string the form string
198165
sub to_form_value {
199-
my $value = shift;
200-
return to_string($value);
166+
my ($self, $value) = @_;
167+
return $self->to_string($value);
201168
}
202169

203170
# Take value and turn it into a string suitable for inclusion in
@@ -206,7 +173,7 @@ sub to_form_value {
206173
# @param string $value the value of the parameter
207174
# @return string the header string
208175
sub to_string {
209-
my $value = shift;
176+
my ($self, $value) = @_;
210177
if (ref($value) eq "DateTime") { # datetime in ISO8601 format
211178
return $value->datetime();
212179
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package WWW::{{invokerPackage}}::Configuration;
2+
3+
use strict;
4+
use warnings;
5+
use utf8;
6+
7+
use Log::Any qw($log);
8+
use Carp;
9+
10+
# class/static variables
11+
my $api_client = WWW::SwaggerClient::APIClient->new;
12+
my $http_timeout = 180;
13+
my $http_user_agent = 'Perl-Swagger';
14+
15+
16+
1;

modules/swagger-codegen/src/main/resources/perl/api.mustache

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ use Carp qw( croak );
2828
use Log::Any qw($log);
2929

3030

31-
#use WWW::Swagger::Model::Category;
32-
#use WWW::Swagger::Model::Pet;
33-
3431
{{#operations}}
3532

3633
use WWW::{{invokerPackage}}::APIClient;
@@ -92,37 +89,34 @@ sub new {
9289
}
9390
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type({{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}});
9491

95-
{{#queryParams}} # query params
92+
{{#queryParams}}# query params
9693
if ( exists $args{'{{paramName}}'}) {
97-
$query_params->{'{{baseName}}'} = WWW::{{invokerPacakge}}::APIClient::to_query_value($args{'{{paramName}}'});
94+
$query_params->{'{{baseName}}'} = $self->{api_client}->to_query_value($args{'{{paramName}}'});
9895
}{{/queryParams}}
99-
{{#headerParams}} # header params
96+
{{#headerParams}}# header params
10097
if ( exists $args{'{{paramName}}'}) {
101-
$header_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_header_value($args{'{{paramName}}'});
98+
$header_params->{'{{baseName}}'} = $self->{api_client}->to_header_value($args{'{{paramName}}'});
10299
}{{/headerParams}}
103-
{{#pathParams}} # path params
100+
{{#pathParams}}# path params
104101
if ( exists $args{'{{paramName}}'}) {
105102
my $_base_variable = "{" . "{{baseName}}" . "}";
106-
my $_base_value = WWW::{{invokerPackage}}::APIClient::to_path_value($args{'{{paramName}}'});
103+
my $_base_value = $self->{api_client}->to_path_value($args{'{{paramName}}'});
107104
$_resource_path =~ s/$_base_variable/$_base_value/g;
108105
}{{/pathParams}}
109-
{{#formParams}} # form params
106+
{{#formParams}}# form params
110107
if ( exists $args{'{{paramName}}'} ) {
111108
{{#isFile}}$form_params->{'{{baseName}}'} = [] unless defined $form_params->{'{{baseName}}'};
112109
push $form_params->{'{{baseName}}'}, $args{'{{paramName}}'};
113110
{{/isFile}}
114-
{{^isFile}}$form_params->{'{{baseName}}'} = WWW::{{invokerPackage}}::APIClient::to_form_value($args{'{{paramName}}'});
111+
{{^isFile}}$form_params->{'{{baseName}}'} = $self->{api_client}->to_form_value($args{'{{paramName}}'});
115112
{{/isFile}}
116113
}{{/formParams}}
117114
my $_body_data;
118-
{{#bodyParams}} # body params
115+
{{#bodyParams}}# body params
119116
if ( exists $args{'{{paramName}}'}) {
120117
$_body_data = $args{'{{paramName}}'};
121118
}{{/bodyParams}}
122119

123-
# for HTTP post (form)
124-
#$_body_data = $_body ? undef : $form_params;
125-
126120
# make the API Call
127121
{{#returnType}}my $response = $self->{api_client}->call_api($_resource_path, $_method,
128122
$query_params, $form_params,

0 commit comments

Comments
 (0)