| 1 | =begin comment | ||
| 2 | |||
| 3 | Aspose.Diagram 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 AsposeDiagramCloud::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 AsposeDiagramCloud::Configuration; | ||
| 41 | |||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $class = shift; | ||
| 45 | |||
| 46 | my $config; | ||
| 47 | if ( $_[0] && ref $_[0] && ref $_[0] eq 'AsposeDiagramCloud::Configuration' ) { | ||
| 48 | $config = $_[0]; | ||
| 49 | } else { | ||
| 50 | $config = AsposeDiagramCloud::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 | sub o_auth_post { | ||
| 83 | my ($self, %args) = @_; | ||
| 84 | |||
| 85 | # verify the required parameter 'grant_type' is set | ||
| 86 | unless (exists $args{'grant_type'}) { | ||
| 87 | croak("Missing the required parameter 'grant_type' when calling o_auth_post"); | ||
| 88 | } | ||
| 89 | |||
| 90 | # verify the required parameter 'client_id' is set | ||
| 91 | unless (exists $args{'client_id'}) { | ||
| 92 | croak("Missing the required parameter 'client_id' when calling o_auth_post"); | ||
| 93 | } | ||
| 94 | |||
| 95 | # verify the required parameter 'client_secret' is set | ||
| 96 | unless (exists $args{'client_secret'}) { | ||
| 97 | croak("Missing the required parameter 'client_secret' when calling o_auth_post"); | ||
| 98 | } | ||
| 99 | |||
| 100 | # parse inputs | ||
| 101 | my $_resource_path = '/connect/token'; | ||
| 102 | if($self->{config}->{api_version} eq "v1.1"){ | ||
| 103 | $_resource_path = '/oauth2/token'; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $_method = 'POST'; | ||
| 107 | my $query_params = {}; | ||
| 108 | my $header_params = {}; | ||
| 109 | my $form_params = {}; | ||
| 110 | |||
| 111 | # 'Accept' and 'Content-Type' header | ||
| 112 | my $_header_accept = $self->select_header_accept('application/json'); | ||
| 113 | if ($_header_accept) { | ||
| 114 | $header_params->{'Accept'} = $_header_accept; | ||
| 115 | } | ||
| 116 | $header_params->{'Content-Type'} = $self->select_header_content_type('application/x-www-form-urlencoded'); | ||
| 117 | |||
| 118 | # form params | ||
| 119 | if ( exists $args{'grant_type'} ) { | ||
| 120 | $form_params->{'grant_type'} = $self->to_form_value($args{'grant_type'}); | ||
| 121 | } | ||
| 122 | |||
| 123 | # form params | ||
| 124 | if ( exists $args{'client_id'} ) { | ||
| 125 | $form_params->{'client_id'} = $self->to_form_value($args{'client_id'}); | ||
| 126 | } | ||
| 127 | |||
| 128 | # form params | ||
| 129 | if ( exists $args{'client_secret'} ) { | ||
| 130 | $form_params->{'client_secret'} = $self->to_form_value($args{'client_secret'}); | ||
| 131 | } | ||
| 132 | |||
| 133 | my $_body_data; | ||
| 134 | # authentication setting, if any | ||
| 135 | my $auth_settings = [qw()]; | ||
| 136 | |||
| 137 | # make the API Call | ||
| 138 | my $response = $self->call_api($_resource_path, $_method, | ||
| 139 | $query_params, $form_params, | ||
| 140 | $header_params, $_body_data, $auth_settings); | ||
| 141 | if (!$response) { | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | my $_response_object = $self->deserialize('AccessTokenResponse', $response); | ||
| 145 | $self->{get_access_token_time} = time(); | ||
| 146 | return $_response_object; | ||
| 147 | } | ||
| 148 | |||
| 149 | # make the HTTP request | ||
| 150 | # @param string $resourcePath path to method endpoint | ||
| 151 | # @param string $method method to call | ||
| 152 | # @param array $queryParams parameters to be place in query URL | ||
| 153 | # @param array $postData parameters to be placed in POST body | ||
| 154 | # @param array $headerParams parameters to be place in request header | ||
| 155 | # @return mixed | ||
| 156 | sub call_api { | ||
| 157 | my $self = shift; | ||
| 158 | my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_; | ||
| 159 | |||
| 160 | # update parameters based on authentication settings | ||
| 161 | $self->update_params_for_auth($header_params, $query_params, $auth_settings); | ||
| 162 | |||
| 163 | |||
| 164 | my $_url = $self->{config}{base_url} . $resource_path; | ||
| 165 | |||
| 166 | # build query | ||
| 167 | if (%$query_params) { | ||
| 168 | $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | # body data | ||
| 173 | $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string | ||
| 174 | my $_body_data = %$post_params ? $post_params : $body_data; | ||
| 175 | |||
| 176 | # Make the HTTP request | ||
| 177 | my $_request; | ||
| 178 | if ($method eq 'POST') { | ||
| 179 | # multipart | ||
| 180 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 181 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 182 | |||
| 183 | $_request = POST($_url, %$header_params, Content => $_body_data); | ||
| 184 | |||
| 185 | } | ||
| 186 | elsif ($method eq 'PUT') { | ||
| 187 | # multipart | ||
| 188 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 189 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 190 | |||
| 191 | $_request = PUT($_url, %$header_params, Content => $_body_data); | ||
| 192 | |||
| 193 | } | ||
| 194 | elsif ($method eq 'GET') { | ||
| 195 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 196 | $_request = GET($_url, %$header_params, Content => $_body_data); | ||
| 197 | } | ||
| 198 | elsif ($method eq 'HEAD') { | ||
| 199 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 200 | $_request = HEAD($_url,%$header_params); | ||
| 201 | } | ||
| 202 | elsif ($method eq 'DELETE') { #TODO support form data | ||
| 203 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 204 | $_request = DELETE($_url, %$headers, Content => $_body_data); | ||
| 205 | } | ||
| 206 | elsif ($method eq 'PATCH') { #TODO | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | } | ||
| 210 | #proxy#################################################################### | ||
| 211 | #$self->{ua}=LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 },); | ||
| 212 | #$self->{ua}->proxy(['https'], "http://127.0.0.1:8888"); | ||
| 213 | #printf $self->{ua}->ssl_opts;############################################ | ||
| 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')) { | ||
| 336 | return $data; | ||
| 337 | } else { # model | ||
| 338 | my $_instance = use_module("AsposeDiagramCloud::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 | # sid key | ||
| 442 | if (my $app_sid = delete $tokens->{app_sid}) { | ||
| 443 | my $app_key = delete $tokens->{app_key}; | ||
| 444 | $query_params->{'app_sid'} = $app_sid; | ||
| 445 | $query_params->{'app_key'} = $app_key; | ||
| 446 | } | ||
| 447 | |||
| 448 | # oauth | ||
| 449 | if (my $access_token = delete $tokens->{access_token}) { | ||
| 450 | $header_params->{'Authorization'} = 'Bearer ' . $access_token; | ||
| 451 | } | ||
| 452 | |||
| 453 | # other keys | ||
| 454 | foreach my $token_name (keys %$tokens) { | ||
| 455 | my $in = $tokens->{$token_name}->{in}; | ||
| 456 | my $token = $self->get_api_key_with_prefix($token_name); | ||
| 457 | if ($in eq 'head') { | ||
| 458 | $header_params->{$token_name} = $token; | ||
| 459 | } | ||
| 460 | elsif ($in eq 'query') { | ||
| 461 | $query_params->{$token_name} = $token; | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')"; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | |||
| 470 | 1; |