ApiClient.pm untested

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 406 CC 51 SCAR 42.0
CC
51
Cov
0%
call_api
182.0
deserialize
132.0
o_auth_post
110.0
CRAP 66.5
1 =begin comment
2
3 Aspose.3D Cloud API Reference
4
5 No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
6
7 OpenAPI spec version: 3.0
8
9 Generated by: https://github.com/swagger-api/swagger-codegen.git
10
11 =end comment
12
13 =cut
14
15 #
16 # NOTE: This class is auto generated by the swagger code generator program.
17 # Do not edit the class manually.
18 # Ref: https://github.com/swagger-api/swagger-codegen
19 #
20 package AsposeThreeDCloud::ApiClient;
21
22 use strict;
23 use warnings;
24 use utf8;
25
26 use MIME::Base64;
27 use LWP::UserAgent;
28 use HTTP::Headers;
29 use HTTP::Response;
30 use HTTP::Request::Common qw(DELETE POST GET HEAD PUT);
31 use HTTP::Status;
32 use URI::Query;
33 use JSON;
34 use URI::Escape;
35 use Scalar::Util;
36 use Log::Any qw($log);
37 use Carp;
38 use Module::Runtime qw(use_module);
39
40 use AsposeThreeDCloud::Configuration;
41
42
43 sub new {
44 my $class = shift;
45
46 my $config;
47 if ( $_[0] && ref $_[0] && ref $_[0] eq 'AsposeThreeDCloud::Configuration' ) {
48 $config = $_[0];
49 } else {
50 $config = AsposeThreeDCloud::Configuration->new(@_);
51 }
52
53 my (%args) = (
54 'ua' => LWP::UserAgent->new,
55 'config' => $config,
56 );
57
58 return bless \%args, $class;
59 }
60
61 # Set the user agent of the API client
62 #
63 # @param string $user_agent The user agent of the API client
64 #
65 sub set_user_agent {
66 my ($self, $user_agent) = @_;
67 $self->{http_user_agent}= $user_agent;
68 }
69
70 # Set timeout
71 #
72 # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
73 #
74 sub set_timeout {
75 my ($self, $seconds) = @_;
76 if (!looks_like_number($seconds)) {
77 croak('Timeout variable must be numeric.');
78 }
79 $self->{http_timeout} = $seconds;
80 }
81
82
83 # @return AccessTokenResponse
84 #
85 sub o_auth_post {
86 my ($self, %args) = @_;
87
88 # verify the required parameter 'grant_type' is set
89 unless (exists $args{'grant_type'}) {
90 croak("Missing the required parameter 'grant_type' when calling o_auth_post");
91 }
92
93 # verify the required parameter 'client_id' is set
94 unless (exists $args{'client_id'}) {
95 croak("Missing the required parameter 'client_id' when calling o_auth_post");
96 }
97
98 # verify the required parameter 'client_secret' is set
99 unless (exists $args{'client_secret'}) {
100 croak("Missing the required parameter 'client_secret' when calling o_auth_post");
101 }
102
103 # parse inputs
104 my $_resource_path = '/connect/token';
105 if($self->{config}->{api_version} eq "v1.1"){
106 $_resource_path = '/connect/token';
107 }
108
109 my $_method = 'POST';
110 my $query_params = {};
111 my $header_params = {};
112 my $form_params = {};
113
114 # 'Accept' and 'Content-Type' header
115 my $_header_accept = $self->select_header_accept('application/json');
116 if ($_header_accept) {
117 $header_params->{'Accept'} = $_header_accept;
118 }
119 $header_params->{'Content-Type'} = $self->select_header_content_type('application/x-www-form-urlencoded');
120
121 # form params
122 if ( exists $args{'grant_type'} ) {
123 $form_params->{'grant_type'} = $self->to_form_value($args{'grant_type'});
124 }
125
126 # form params
127 if ( exists $args{'client_id'} ) {
128 $form_params->{'client_id'} = $self->to_form_value($args{'client_id'});
129 }
130
131 # form params
132 if ( exists $args{'client_secret'} ) {
133 $form_params->{'client_secret'} = $self->to_form_value($args{'client_secret'});
134 }
135
136 my $_body_data;
137 # authentication setting, if any
138 my $auth_settings = [qw()];
139
140 # make the API Call
141 my $response = $self->call_api($_resource_path, $_method,
142 $query_params, $form_params,
143 $header_params, $_body_data, $auth_settings);
144 if (!$response) {
145 return;
146 }
147 my $_response_object = $self->deserialize('AccessTokenResponse', $response);
148 $self->{get_access_token_time} = time();
149 return $_response_object;
150 }
151
152 # make the HTTP request
153 # @param string $resourcePath path to method endpoint
154 # @param string $method method to call
155 # @param array $queryParams parameters to be place in query URL
156 # @param array $postData parameters to be placed in POST body
157 # @param array $headerParams parameters to be place in request header
158 # @return mixed
159 sub call_api {
160 my $self = shift;
161 my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_;
162
163 # update parameters based on authentication settings
164 $self->update_params_for_auth($header_params, $query_params, $auth_settings);
165
166
167 my $_url = $self->{config}{base_url} . $resource_path;
168
169 # build query
170 if (%$query_params) {
171 $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
172 }
173
174
175 # body data
176 $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
177 my $_body_data = %$post_params ? $post_params : $body_data;
178
179 # Make the HTTP request
180 my $_request;
181 if ($method eq 'POST') {
182 # multipart
183 $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
184 'form-data' : $header_params->{'Content-Type'};
185
186 $_request = POST($_url, %$header_params, Content => $_body_data);
187
188 }
189 elsif ($method eq 'PUT') {
190 # multipart
191 $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
192 'form-data' : $header_params->{'Content-Type'};
193
194 $_request = PUT($_url, %$header_params, Content => $_body_data);
195
196 }
197 elsif ($method eq 'GET') {
198 my $headers = HTTP::Headers->new(%$header_params);
199 $_request = GET($_url, %$header_params, Content => $_body_data);
200 }
201 elsif ($method eq 'HEAD') {
202 my $headers = HTTP::Headers->new(%$header_params);
203 $_request = HEAD($_url,%$header_params);
204 }
205 elsif ($method eq 'DELETE') { #TODO support form data
206 my $headers = HTTP::Headers->new(%$header_params);
207 $_request = DELETE($_url, %$headers, Content => $_body_data);
208 }
209 elsif ($method eq 'PATCH') { #TODO
210 }
211 else {
212 }
213
214 $self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout});
215 $self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent});
216
217 $log->debugf("REQUEST: %s", $_request->as_string);
218 my $_response = $self->{ua}->request($_request);
219 $log->debugf("RESPONSE: %s", $_response->as_string);
220
221 unless ($_response->is_success) {
222 croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content);
223 }
224
225 return $_response->content;
226
227 }
228
229 # Take value and turn it into a string suitable for inclusion in
230 # the path, by url-encoding.
231 # @param string $value a string which will be part of the path
232 # @return string the serialized object
233 sub to_path_value {
234 my ($self, $value) = @_;
235 return uri_escape($self->to_string($value));
236 }
237
238
239 # Take value and turn it into a string suitable for inclusion in
240 # the query, by imploding comma-separated if it's an object.
241 # If it's a string, pass through unchanged. It will be url-encoded
242 # later.
243 # @param object $object an object to be serialized to a string
244 # @return string the serialized object
245 sub to_query_value {
246 my ($self, $object) = @_;
247 if (ref($object) eq 'ARRAY') {
248 return join(',', @$object);
249 } else {
250 return $self->to_string($object);
251 }
252 }
253
254
255 # Take value and turn it into a string suitable for inclusion in
256 # the header. If it's a string, pass through unchanged
257 # If it's a datetime object, format it in ISO8601
258 # @param string $value a string which will be part of the header
259 # @return string the header string
260 sub to_header_value {
261 my ($self, $value) = @_;
262 return $self->to_string($value);
263 }
264
265 # Take value and turn it into a string suitable for inclusion in
266 # the http body (form parameter). If it's a string, pass through unchanged
267 # If it's a datetime object, format it in ISO8601
268 # @param string $value the value of the form parameter
269 # @return string the form string
270 sub to_form_value {
271 my ($self, $value) = @_;
272 return $self->to_string($value);
273 }
274
275 # Take value and turn it into a string suitable for inclusion in
276 # the parameter. If it's a string, pass through unchanged
277 # If it's a datetime object, format it in ISO8601
278 # @param string $value the value of the parameter
279 # @return string the header string
280 sub to_string {
281 my ($self, $value) = @_;
282 if (ref($value) eq "DateTime") { # datetime in ISO8601 format
283 return $value->datetime();
284 }
285 else {
286 return $value;
287 }
288 }
289
290 # Deserialize a JSON string into an object
291 #
292 # @param string $class class name is passed as a string
293 # @param string $data data of the body
294 # @return object an instance of $class
295 sub deserialize
296 {
297 my ($self, $class, $data) = @_;
298 $log->debugf("deserializing %s for %s", $data, $class);
299
300 if (not defined $data) {
301 return undef;
302 } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash
303 if ($class =~ /^HASH\[(.*),(.*)\]$/) {
304 my ($key_type, $type) = ($1, $2);
305 my %hash;
306 my $decoded_data = decode_json $data;
307 foreach my $key (keys %$decoded_data) {
308 if (ref $decoded_data->{$key} eq 'HASH') {
309 $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key});
310 } else {
311 $hash{$key} = $self->deserialize($type, $decoded_data->{$key});
312 }
313 }
314 return \%hash;
315 } else {
316 #TODO log error
317 }
318
319 } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data
320 return $data if $data eq '[]'; # return if empty array
321
322 my $_sub_class = substr($class, 6, -1);
323 my $_json_data = decode_json $data;
324 my @_values = ();
325 foreach my $_value (@$_json_data) {
326 if (ref $_value eq 'ARRAY') {
327 push @_values, $self->deserialize($_sub_class, encode_json $_value);
328 } else {
329 push @_values, $self->deserialize($_sub_class, $_value);
330 }
331 }
332 return \@_values;
333 } elsif ($class eq 'DateTime') {
334 return DateTime->from_epoch(epoch => str2time($data));
335 } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object','File')) {
336 return $data;
337 } else { # model
338 my $_instance = use_module("AsposeThreeDCloud::Object::$class")->new;
339 if (ref $data eq "HASH") {
340 return $_instance->from_hash($data);
341 } else { # string, need to json decode first
342 return $_instance->from_hash(decode_json $data);
343 }
344 }
345
346 }
347
348 # return 'Accept' based on an array of accept provided
349 # @param [Array] header_accept_array Array fo 'Accept'
350 # @return String Accept (e.g. application/json)
351 sub select_header_accept
352 {
353 my ($self, @header) = @_;
354
355 if (@header == 0 || (@header == 1 && $header[0] eq '')) {
356 return undef;
357 } elsif (grep(/^application\/json$/i, @header)) {
358 return 'application/json';
359 } else {
360 return join(',', @header);
361 }
362
363 }
364
365 # return the content type based on an array of content-type provided
366 # @param [Array] content_type_array Array fo content-type
367 # @return String Content-Type (e.g. application/json)
368 sub select_header_content_type
369 {
370 my ($self, @header) = @_;
371
372 if (@header == 0 || (@header == 1 && $header[0] eq '')) {
373 return 'application/json'; # default to application/json
374 } elsif (grep(/^application\/json$/i, @header)) {
375 return 'application/json';
376 } else {
377 return join(',', @header);
378 }
379
380 }
381
382 # Get API key (with prefix if set)
383 # @param string key name
384 # @return string API key with the prefix
385 sub get_api_key_with_prefix
386 {
387 my ($self, $key_name) = @_;
388
389 my $api_key = $self->{config}{api_key}{$key_name};
390
391 return unless $api_key;
392
393 my $prefix = $self->{config}{api_key_prefix}{$key_name};
394 return $prefix ? "$prefix $api_key" : $api_key;
395 }
396
397 # update header and query param based on authentication setting
398 #
399 # @param array $headerParams header parameters (by ref)
400 # @param array $queryParams query parameters (by ref)
401 # @param array $authSettings array of authentication scheme (e.g ['api_key'])
402 sub update_params_for_auth {
403 my ($self, $header_params, $query_params, $auth_settings) = @_;
404
405 return $self->_global_auth_setup($header_params, $query_params)
406 unless $auth_settings && @$auth_settings;
407
408 # one endpoint can have more than 1 auth settings
409 foreach my $auth (@$auth_settings) {
410 # determine which one to use
411 if (!defined($auth)) {
412 # TODO show warning about auth setting not defined
413 }
414 elsif ($auth eq 'JWT') {
415
416 if ($self->{config}{access_token}) {
417 $header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
418 }
419 }
420 else {
421 # TODO show warning about security definition not found
422 }
423 }
424 }
425
426 # The endpoint API class has not found any settings for auth. This may be deliberate,
427 # in which case update_params_for_auth() will be a no-op. But it may also be that the
428 # OpenAPI Spec does not describe the intended authorization. So we check in the config for any
429 # auth tokens and if we find any, we use them for all endpoints;
430 sub _global_auth_setup {
431 my ($self, $header_params, $query_params) = @_;
432
433 my $tokens = $self->{config}->get_tokens;
434 return unless keys %$tokens;
435
436 # basic
437 if (my $uname = delete $tokens->{username}) {
438 my $pword = delete $tokens->{password};
439 $header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
440 }
441
442 # oauth
443 if (my $access_token = delete $tokens->{access_token}) {
444 $header_params->{'Authorization'} = 'Bearer ' . $access_token;
445 }
446
447 # other keys
448 foreach my $token_name (keys %$tokens) {
449 my $in = $tokens->{$token_name}->{in};
450 my $token = $self->get_api_key_with_prefix($token_name);
451 if ($in eq 'head') {
452 $header_params->{$token_name} = $token;
453 }
454 elsif ($in eq 'query') {
455 $query_params->{$token_name} = $token;
456 }
457 else {
458 die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
459 }
460 }
461 }
462
463
464 1;