@@ -15,20 +15,16 @@ use URI::Escape;
15
15
use Scalar::Util;
16
16
use Log::Any qw($log);
17
17
use Carp;
18
- use Switch;
19
18
use Module::Runtime qw(use_module);
20
19
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
-
28
20
sub new
29
21
{
30
22
my $class = shift;
31
- my % args = @_;
23
+ my (% args) = (
24
+ ' ua' => LWP::UserAgent-> new ,
25
+ ' base_url' => ' {{basePath}}' ,
26
+ @_
27
+ );
32
28
33
29
return bless \% args, $class ;
34
30
}
@@ -38,20 +34,20 @@ sub new
38
34
# @param string $user_agent The user agent of the API client
39
35
#
40
36
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;
43
39
}
44
40
45
41
# Set timeout
46
42
#
47
43
# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
48
44
#
49
45
sub set_timeout {
50
- my $ seconds = shift ;
46
+ my ( $ self , $ seconds ) = @_ ;
51
47
if (! looks_like_number($seconds )) {
52
48
croak(' Timeout variable must be numeric.' );
53
49
}
54
- $http_timeout = $seconds;
50
+ $self-> { http_timeout} = $seconds;
55
51
}
56
52
57
53
# make the HTTP request
@@ -67,7 +63,7 @@ sub call_api {
67
63
68
64
my $headers = HTTP::Headers-> new (% $header_params );
69
65
70
- my $_url = $base_url . $resource_path ;
66
+ my $_url = $self - > { base_url} . $resource_path;
71
67
72
68
# build query
73
69
if (%$query_params) {
@@ -80,43 +76,42 @@ sub call_api {
80
76
81
77
# Make the HTTP request
82
78
my $_request;
83
- switch ($method) {
84
- case ' POST' {
79
+ if ($method eq 'POST') {
85
80
# multipart
86
81
my $_content_type = lc $header_params -> {' Content-Type' } eq 'multipart/form' ?
87
82
'form-data' : $header_params->{ ' Content-Type' } ;
88
83
89
84
$_request = POST($_url, Accept => $header_params->{ Accept} ,
90
85
Content_Type => $_content_type, Content => $_body_data);
91
- }
92
- case 'PUT' {
86
+ }
87
+ elsif ($method eq 'PUT') {
93
88
# multipart
94
89
my $_content_type = lc $header_params -> {' Content-Type' } eq 'multipart/form' ?
95
90
'form-data' : $header_params->{ ' Content-Type' } ;
96
91
$_request = PUT($_url, Accept => $header_params->{ Accept} ,
97
92
Content_Type => $_content_type, Content => $_body_data);
98
- }
99
- case 'GET' {
93
+ }
94
+ elsif ($method eq 'GET') {
100
95
$_request = GET($_url , Accept => $header_params -> {' Accept' } ,
101
96
Content_Type => $header_params->{ ' Content-Type' } );
102
- }
103
- case 'HEAD' {
97
+ }
98
+ elsif ($method eq 'HEAD') {
104
99
$_request = HEAD($_url , Accept => $header_params -> {' Accept' } ,
105
100
Content_Type => $header_params->{ ' Content-Type' } );
106
- }
107
- case 'DELETE' { #TODO support form data
101
+ }
102
+ elsif ($method eq 'DELETE') { #TODO support form data
108
103
$_request = DELETE($_url , Accept => $header_params -> {' Accept' } ,
109
104
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 {
114
109
}
115
110
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);
118
113
119
- my $_response = $ua ->request($_request);
114
+ my $_response = $self-> { ua } ->request($_request);
120
115
121
116
unless ($_response->is_success) {
122
117
croak(" API Exception(" .$_response -> code ." ): " .$_response -> message );
@@ -126,41 +121,13 @@ sub call_api {
126
121
127
122
}
128
123
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
-
157
124
# Take value and turn it into a string suitable for inclusion in
158
125
# the path, by url-encoding.
159
126
# @param string $value a string which will be part of the path
160
127
# @return string the serialized object
161
128
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 ));
164
131
}
165
132
166
133
@@ -171,11 +138,11 @@ sub to_path_value {
171
138
# @param object $object an object to be serialized to a string
172
139
# @return string the serialized object
173
140
sub to_query_value {
174
- my $ object = shift ;
141
+ my ( $ self , $ object ) = @_ ;
175
142
if (is_array($object )) {
176
143
return implode(' ,' , $object );
177
144
} else {
178
- return toString ($object );
145
+ return $ self -> to_string ($object );
179
146
}
180
147
}
181
148
@@ -186,8 +153,8 @@ sub to_query_value {
186
153
# @param string $value a string which will be part of the header
187
154
# @return string the header string
188
155
sub to_header_value {
189
- my $ value = shift ;
190
- return to_string($value );
156
+ my ( $ self , $ value ) = @_ ;
157
+ return $ self -> to_string ($value );
191
158
}
192
159
193
160
# Take value and turn it into a string suitable for inclusion in
@@ -196,8 +163,8 @@ sub to_header_value {
196
163
# @param string $value the value of the form parameter
197
164
# @return string the form string
198
165
sub to_form_value {
199
- my $ value = shift ;
200
- return to_string($value );
166
+ my ( $ self , $ value ) = @_ ;
167
+ return $ self -> to_string ($value );
201
168
}
202
169
203
170
# Take value and turn it into a string suitable for inclusion in
@@ -206,7 +173,7 @@ sub to_form_value {
206
173
# @param string $value the value of the parameter
207
174
# @return string the header string
208
175
sub to_string {
209
- my $ value = shift ;
176
+ my ( $ self , $ value ) = @_ ;
210
177
if (ref($value ) eq " DateTime" ) { # datetime in ISO8601 format
211
178
return $value -> datetime ();
212
179
}
0 commit comments