| 1 | =begin comment | ||
| 2 | |||
| 3 | Copyright (c) 2026 Aspose.Cells Cloud | ||
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 5 | of this software and associated documentation files (the "Software"), to deal | ||
| 6 | in the Software without restriction, including without limitation the rights | ||
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 8 | copies of the Software, and to permit persons to whom the Software is | ||
| 9 | furnished to do so, subject to the following conditions: | ||
| 10 | |||
| 11 | The above copyright notice and this permission notice shall be included in all | ||
| 12 | copies or substantial portions of the Software. | ||
| 13 | |||
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 20 | SOFTWARE. | ||
| 21 | |||
| 22 | =end comment | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package AsposeCellsCloud::ApiClient; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | use utf8; | ||
| 31 | |||
| 32 | use MIME::Base64; | ||
| 33 | use LWP::UserAgent; | ||
| 34 | use HTTP::Headers; | ||
| 35 | use HTTP::Response; | ||
| 36 | use HTTP::Request::Common qw(DELETE POST GET HEAD PUT); | ||
| 37 | use HTTP::Status; | ||
| 38 | use URI::Query; | ||
| 39 | use JSON; | ||
| 40 | use URI::Escape; | ||
| 41 | use Scalar::Util; | ||
| 42 | use Log::Any qw($log); | ||
| 43 | use Carp; | ||
| 44 | use Module::Runtime qw(use_module); | ||
| 45 | |||
| 46 | use AsposeCellsCloud::Configuration; | ||
| 47 | |||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | |||
| 52 | my $config; | ||
| 53 | my $get_access_token_time; | ||
| 54 | if ( $_[0] && ref $_[0] && ref $_[0] eq 'AsposeCellsCloud::Configuration' ) { | ||
| 55 | $config = $_[0]; | ||
| 56 | } else { | ||
| 57 | $config = AsposeCellsCloud::Configuration->new(@_); | ||
| 58 | } | ||
| 59 | |||
| 60 | my (%args) = ( | ||
| 61 | 'ua' => LWP::UserAgent->new, | ||
| 62 | 'config' => $config, | ||
| 63 | 'get_access_token_time' =>$get_access_token_time, | ||
| 64 | ); | ||
| 65 | |||
| 66 | return bless \%args, $class; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub need_auth{ | ||
| 70 | my ($self) = @_; | ||
| 71 | |||
| 72 | if( $self->{config}->{client_id} || $self->{config}->{client_secret} ) { | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Set the user agent of the API client | ||
| 79 | # | ||
| 80 | # @param string $user_agent The user agent of the API client | ||
| 81 | # | ||
| 82 | sub set_user_agent { | ||
| 83 | my ($self, $user_agent) = @_; | ||
| 84 | $self->{http_user_agent}= $user_agent; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Set timeout | ||
| 88 | # | ||
| 89 | # @param integer $seconds Number of seconds before timing out [set to 0 for no timeout] | ||
| 90 | # | ||
| 91 | sub set_timeout { | ||
| 92 | my ($self, $seconds) = @_; | ||
| 93 | if (!looks_like_number($seconds)) { | ||
| 94 | croak('Timeout variable must be numeric.'); | ||
| 95 | } | ||
| 96 | $self->{http_timeout} = $seconds; | ||
| 97 | } | ||
| 98 | |||
| 99 | # @return AccessTokenResponse | ||
| 100 | # | ||
| 101 | sub o_auth_post { | ||
| 102 | my ($self, %args) = @_; | ||
| 103 | |||
| 104 | # verify the required parameter 'grant_type' is set | ||
| 105 | unless (exists $args{'grant_type'}) { | ||
| 106 | croak("Missing the required parameter 'grant_type' when calling o_auth_post"); | ||
| 107 | } | ||
| 108 | |||
| 109 | # verify the required parameter 'client_id' is set | ||
| 110 | unless (exists $args{'client_id'}) { | ||
| 111 | croak("Missing the required parameter 'client_id' when calling o_auth_post"); | ||
| 112 | } | ||
| 113 | |||
| 114 | # verify the required parameter 'client_secret' is set | ||
| 115 | unless (exists $args{'client_secret'}) { | ||
| 116 | croak("Missing the required parameter 'client_secret' when calling o_auth_post"); | ||
| 117 | } | ||
| 118 | |||
| 119 | # parse inputs | ||
| 120 | my $_resource_path = '/v3.0/cells/connect/token'; | ||
| 121 | if($self->{config}->{api_version} eq "v1.1"){ | ||
| 122 | $_resource_path = '/oauth2/token'; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $_method = 'POST'; | ||
| 126 | my $query_params = {}; | ||
| 127 | my $header_params = {}; | ||
| 128 | my $form_params = {}; | ||
| 129 | |||
| 130 | # 'Accept' and 'Content-Type' header | ||
| 131 | my $_header_accept = $self->select_header_accept('application/json'); | ||
| 132 | if ($_header_accept) { | ||
| 133 | $header_params->{'Accept'} = $_header_accept; | ||
| 134 | } | ||
| 135 | $header_params->{'Content-Type'} = $self->select_header_content_type('application/x-www-form-urlencoded'); | ||
| 136 | |||
| 137 | # form params | ||
| 138 | if ( exists $args{'grant_type'} ) { | ||
| 139 | $form_params->{'grant_type'} = $self->to_form_value($args{'grant_type'}); | ||
| 140 | } | ||
| 141 | |||
| 142 | # form params | ||
| 143 | if ( exists $args{'client_id'} ) { | ||
| 144 | $form_params->{'client_id'} = $self->to_form_value($args{'client_id'}); | ||
| 145 | } | ||
| 146 | |||
| 147 | # form params | ||
| 148 | if ( exists $args{'client_secret'} ) { | ||
| 149 | $form_params->{'client_secret'} = $self->to_form_value($args{'client_secret'}); | ||
| 150 | } | ||
| 151 | |||
| 152 | my $_body_data; | ||
| 153 | # authentication setting, if any | ||
| 154 | my $auth_settings = [qw()]; | ||
| 155 | |||
| 156 | # make the API Call | ||
| 157 | my $response = $self->call_api($_resource_path, $_method, | ||
| 158 | $query_params, $form_params, | ||
| 159 | $header_params, $_body_data, $auth_settings, 'get_token'); | ||
| 160 | if (!$response) { | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | my $_response_object = $self->deserialize('AccessTokenResponse', $response); | ||
| 164 | $self->{get_access_token_time} = time(); | ||
| 165 | return $_response_object; | ||
| 166 | } | ||
| 167 | # check access token | ||
| 168 | sub check_access_token { | ||
| 169 | my ($self, %args) = @_; | ||
| 170 | if(!$self->need_auth()){ | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | if($self->{get_access_token_time}){ | ||
| 174 | my $difference_in_seconds=time() - $self->{get_access_token_time}; | ||
| 175 | if($difference_in_seconds < 86300){ | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | my $access_token = $self->o_auth_post('grant_type' => "client_credentials", 'client_id' => $self->{config}->{client_id}, 'client_secret' =>$self->{config}->{client_secret})->access_token; | ||
| 180 | $self->{config}->{access_token} = $access_token; | ||
| 181 | } | ||
| 182 | |||
| 183 | # make the HTTP request | ||
| 184 | # @param string $resourcePath path to method endpoint | ||
| 185 | # @param string $method method to call | ||
| 186 | # @param array $queryParams parameters to be place in query URL | ||
| 187 | # @param array $postData parameters to be placed in POST body | ||
| 188 | # @param array $headerParams parameters to be place in request header | ||
| 189 | # @return mixed | ||
| 190 | sub call_api { | ||
| 191 | my $self = shift; | ||
| 192 | my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings,$get_token) = @_; | ||
| 193 | |||
| 194 | # update parameters based on authentication settings | ||
| 195 | $self->update_params_for_auth($header_params, $query_params, $auth_settings ); | ||
| 196 | |||
| 197 | |||
| 198 | my $_url = $self->{config}{base_url}."/" . $resource_path; | ||
| 199 | if($get_token){ | ||
| 200 | $_url = $self->{config}{base_url} . $resource_path; | ||
| 201 | } | ||
| 202 | |||
| 203 | # build header | ||
| 204 | |||
| 205 | $header_params->{'x-aspose-client'} = 'perl sdk'; | ||
| 206 | $header_params->{'x-aspose-client-version'} = '26.3.1'; | ||
| 207 | # build query | ||
| 208 | if (%$query_params) { | ||
| 209 | $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify }); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | # body data | ||
| 214 | $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string | ||
| 215 | my $_body_data = %$post_params ? $post_params : $body_data; | ||
| 216 | |||
| 217 | # Make the HTTP request | ||
| 218 | my $_request; | ||
| 219 | if ($method eq 'POST') { | ||
| 220 | # multipart | ||
| 221 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 222 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 223 | |||
| 224 | if($_body_data){ | ||
| 225 | $_request = POST($_url, %$header_params, Content => $_body_data); | ||
| 226 | } | ||
| 227 | else{ | ||
| 228 | $_request = POST($_url, %$header_params); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | elsif ($method eq 'PUT') { | ||
| 232 | # multipart | ||
| 233 | $header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ? | ||
| 234 | 'form-data' : $header_params->{'Content-Type'}; | ||
| 235 | |||
| 236 | if($_body_data){ | ||
| 237 | $_request = PUT($_url, %$header_params, Content => $_body_data); | ||
| 238 | } | ||
| 239 | else{ | ||
| 240 | $_request = PUT($_url, %$header_params); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | elsif ($method eq 'GET') { | ||
| 244 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 245 | if($_body_data){ | ||
| 246 | $_request = GET($_url, %$header_params, Content => $_body_data); | ||
| 247 | } | ||
| 248 | else{ | ||
| 249 | $_request = GET($_url, %$header_params); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | elsif ($method eq 'HEAD') { | ||
| 253 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 254 | $_request = HEAD($_url,%$header_params); | ||
| 255 | } | ||
| 256 | elsif ($method eq 'DELETE') { #TODO support form data | ||
| 257 | my $headers = HTTP::Headers->new(%$header_params); | ||
| 258 | if($_body_data){ | ||
| 259 | $_request = DELETE($_url, %$header_params, Content => $_body_data); | ||
| 260 | } | ||
| 261 | else{ | ||
| 262 | $_request = DELETE($_url, %$header_params); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | elsif ($method eq 'PATCH') { #TODO | ||
| 266 | } | ||
| 267 | else { | ||
| 268 | } | ||
| 269 | #proxy#################################################################### | ||
| 270 | #$self->{ua}=LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 },); | ||
| 271 | #$self->{ua}->proxy(['https'], "http://127.0.0.1:8888"); | ||
| 272 | #printf $self->{ua}->ssl_opts;############################################ | ||
| 273 | $self->{ua}->timeout($self->{http_timeout} || $self->{config}{http_timeout}); | ||
| 274 | $self->{ua}->agent($self->{http_user_agent} || $self->{config}{http_user_agent}); | ||
| 275 | |||
| 276 | $log->debugf("REQUEST: %s", $_request->as_string); | ||
| 277 | my $_response = $self->{ua}->request($_request); | ||
| 278 | $log->debugf("RESPONSE: %s", $_response->as_string); | ||
| 279 | |||
| 280 | unless ($_response->is_success) { | ||
| 281 | croak(sprintf "API Exception(%s): %s\n%s", $_response->code, $_response->message, $_response->content); | ||
| 282 | } | ||
| 283 | |||
| 284 | return $_response->content; | ||
| 285 | |||
| 286 | } | ||
| 287 | |||
| 288 | # Take value and turn it into a string suitable for inclusion in | ||
| 289 | # the path, by url-encoding. | ||
| 290 | # @param string $value a string which will be part of the path | ||
| 291 | # @return string the serialized object | ||
| 292 | sub to_path_value { | ||
| 293 | my ($self, $value) = @_; | ||
| 294 | my $newpath = uri_escape($self->to_string($value)); | ||
| 295 | my $form = "%2F"; | ||
| 296 | my $to ="/"; | ||
| 297 | $newpath =~ s/$form/$to/g; | ||
| 298 | return $newpath; | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | # Take value and turn it into a string suitable for inclusion in | ||
| 303 | # the query, by imploding comma-separated if it's an object. | ||
| 304 | # If it's a string, pass through unchanged. It will be url-encoded | ||
| 305 | # later. | ||
| 306 | # @param object $object an object to be serialized to a string | ||
| 307 | # @return string the serialized object | ||
| 308 | sub to_query_value { | ||
| 309 | my ($self, $object) = @_; | ||
| 310 | if (ref($object) eq 'ARRAY') { | ||
| 311 | return join(',', @$object); | ||
| 312 | } else { | ||
| 313 | return $self->to_string($object); | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | # Take value and turn it into a string suitable for inclusion in | ||
| 319 | # the header. If it's a string, pass through unchanged | ||
| 320 | # If it's a datetime object, format it in ISO8601 | ||
| 321 | # @param string $value a string which will be part of the header | ||
| 322 | # @return string the header string | ||
| 323 | sub to_header_value { | ||
| 324 | my ($self, $value) = @_; | ||
| 325 | return $self->to_string($value); | ||
| 326 | } | ||
| 327 | |||
| 328 | # Take value and turn it into a string suitable for inclusion in | ||
| 329 | # the http body (form parameter). If it's a string, pass through unchanged | ||
| 330 | # If it's a datetime object, format it in ISO8601 | ||
| 331 | # @param string $value the value of the form parameter | ||
| 332 | # @return string the form string | ||
| 333 | sub to_form_value { | ||
| 334 | my ($self, $value) = @_; | ||
| 335 | return $self->to_string($value); | ||
| 336 | } | ||
| 337 | |||
| 338 | # Take value and turn it into a string suitable for inclusion in | ||
| 339 | # the parameter. If it's a string, pass through unchanged | ||
| 340 | # If it's a datetime object, format it in ISO8601 | ||
| 341 | # @param string $value the value of the parameter | ||
| 342 | # @return string the header string | ||
| 343 | sub to_string { | ||
| 344 | my ($self, $value) = @_; | ||
| 345 | if (ref($value) eq "DateTime") { # datetime in ISO8601 format | ||
| 346 | return $value->datetime(); | ||
| 347 | } | ||
| 348 | else { | ||
| 349 | return $value; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | # Deserialize a JSON string into an object | ||
| 354 | # | ||
| 355 | # @param string $class class name is passed as a string | ||
| 356 | # @param string $data data of the body | ||
| 357 | # @return object an instance of $class | ||
| 358 | sub deserialize | ||
| 359 | { | ||
| 360 | my ($self, $class, $data) = @_; | ||
| 361 | $log->debugf("deserializing %s for %s", $data, $class); | ||
| 362 | |||
| 363 | if (not defined $data) { | ||
| 364 | return undef; | ||
| 365 | } elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash | ||
| 366 | if ($class =~ /^HASH\[(.*),(.*)\]$/) { | ||
| 367 | my ($key_type, $type) = ($1, $2); | ||
| 368 | my %hash; | ||
| 369 | my $decoded_data = decode_json $data; | ||
| 370 | foreach my $key (keys %$decoded_data) { | ||
| 371 | if (ref $decoded_data->{$key} eq 'HASH') { | ||
| 372 | $hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key}); | ||
| 373 | } else { | ||
| 374 | $hash{$key} = $self->deserialize($type, $decoded_data->{$key}); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | return \%hash; | ||
| 378 | } else { | ||
| 379 | #TODO log error | ||
| 380 | } | ||
| 381 | |||
| 382 | } elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data | ||
| 383 | return $data if $data eq '[]'; # return if empty array | ||
| 384 | |||
| 385 | my $_sub_class = substr($class, 6, -1); | ||
| 386 | my $_json_data = decode_json $data; | ||
| 387 | my @_values = (); | ||
| 388 | foreach my $_value (@$_json_data) { | ||
| 389 | if (ref $_value eq 'ARRAY') { | ||
| 390 | push @_values, $self->deserialize($_sub_class, encode_json $_value); | ||
| 391 | } else { | ||
| 392 | push @_values, $self->deserialize($_sub_class, $_value); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | return \@_values; | ||
| 396 | } elsif ($class eq 'DateTime') { | ||
| 397 | return DateTime->from_epoch(epoch => str2time($data)); | ||
| 398 | } elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) { | ||
| 399 | return $data; | ||
| 400 | } else { # model | ||
| 401 | my $_instance = use_module("AsposeCellsCloud::Object::$class")->new; | ||
| 402 | if (ref $data eq "HASH") { | ||
| 403 | return $_instance->from_hash($data); | ||
| 404 | } else { # string, need to json decode first | ||
| 405 | return $_instance->from_hash(decode_json $data); | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | } | ||
| 410 | |||
| 411 | # return 'Accept' based on an array of accept provided | ||
| 412 | # @param [Array] header_accept_array Array fo 'Accept' | ||
| 413 | # @return String Accept (e.g. application/json) | ||
| 414 | sub select_header_accept | ||
| 415 | { | ||
| 416 | my ($self, @header) = @_; | ||
| 417 | |||
| 418 | if (@header == 0 || (@header == 1 && $header[0] eq '')) { | ||
| 419 | return undef; | ||
| 420 | } elsif (grep(/^application\/json$/i, @header)) { | ||
| 421 | return 'application/json'; | ||
| 422 | } else { | ||
| 423 | return join(',', @header); | ||
| 424 | } | ||
| 425 | |||
| 426 | } | ||
| 427 | |||
| 428 | # return the content type based on an array of content-type provided | ||
| 429 | # @param [Array] content_type_array Array fo content-type | ||
| 430 | # @return String Content-Type (e.g. application/json) | ||
| 431 | sub select_header_content_type | ||
| 432 | { | ||
| 433 | my ($self, @header) = @_; | ||
| 434 | |||
| 435 | if (@header == 0 || (@header == 1 && $header[0] eq '')) { | ||
| 436 | return 'application/json'; # default to application/json | ||
| 437 | } elsif (grep(/^application\/json$/i, @header)) { | ||
| 438 | return 'application/json'; | ||
| 439 | } else { | ||
| 440 | return join(',', @header); | ||
| 441 | } | ||
| 442 | |||
| 443 | } | ||
| 444 | |||
| 445 | # Get API key (with prefix if set) | ||
| 446 | # @param string key name | ||
| 447 | # @return string API key with the prefix | ||
| 448 | sub get_api_key_with_prefix | ||
| 449 | { | ||
| 450 | my ($self, $key_name) = @_; | ||
| 451 | |||
| 452 | my $api_key = $self->{config}{api_key}{$key_name}; | ||
| 453 | |||
| 454 | return unless $api_key; | ||
| 455 | |||
| 456 | my $prefix = $self->{config}{api_key_prefix}{$key_name}; | ||
| 457 | return $prefix ? "$prefix $api_key" : $api_key; | ||
| 458 | } | ||
| 459 | |||
| 460 | # update header and query param based on authentication setting | ||
| 461 | # | ||
| 462 | # @param array $headerParams header parameters (by ref) | ||
| 463 | # @param array $queryParams query parameters (by ref) | ||
| 464 | # @param array $authSettings array of authentication scheme (e.g ['api_key']) | ||
| 465 | sub update_params_for_auth { | ||
| 466 | my ($self, $header_params, $query_params, $auth_settings) = @_; | ||
| 467 | if(!$self->need_auth()){ | ||
| 468 | return; | ||
| 469 | } | ||
| 470 | return $self->_global_auth_setup($header_params, $query_params) | ||
| 471 | unless $auth_settings && @$auth_settings; | ||
| 472 | |||
| 473 | # one endpoint can have more than 1 auth settings | ||
| 474 | foreach my $auth (@$auth_settings) { | ||
| 475 | # determine which one to use | ||
| 476 | if (!defined($auth)) { | ||
| 477 | # TODO show warning about auth setting not defined | ||
| 478 | } | ||
| 479 | elsif ($auth eq 'appsid') { | ||
| 480 | |||
| 481 | my $api_key = $self->get_api_key_with_prefix('appsid'); | ||
| 482 | if ($api_key) { | ||
| 483 | $query_params->{'appsid'} = $api_key; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | elsif ($auth eq 'oauth') { | ||
| 487 | |||
| 488 | if ($self->{config}{access_token}) { | ||
| 489 | $header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token}; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | elsif ($auth eq 'signature') { | ||
| 493 | |||
| 494 | my $api_key = $self->get_api_key_with_prefix('signature'); | ||
| 495 | if ($api_key) { | ||
| 496 | $query_params->{'signature'} = $api_key; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | else { | ||
| 500 | # TODO show warning about security definition not found | ||
| 501 | } | ||
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 | # The endpoint API class has not found any settings for auth. This may be deliberate, | ||
| 506 | # in which case update_params_for_auth() will be a no-op. But it may also be that the | ||
| 507 | # OpenAPI Spec does not describe the intended authorization. So we check in the config for any | ||
| 508 | # auth tokens and if we find any, we use them for all endpoints; | ||
| 509 | sub _global_auth_setup { | ||
| 510 | my ($self, $header_params, $query_params) = @_; | ||
| 511 | |||
| 512 | my $tokens = $self->{config}->get_tokens; | ||
| 513 | return unless keys %$tokens; | ||
| 514 | |||
| 515 | # basic | ||
| 516 | if (my $uname = delete $tokens->{username}) { | ||
| 517 | my $pword = delete $tokens->{password}; | ||
| 518 | $header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword); | ||
| 519 | } | ||
| 520 | # sid key | ||
| 521 | if (my $client_id = delete $tokens->{client_id}) { | ||
| 522 | my $client_secret = delete $tokens->{client_secret}; | ||
| 523 | $query_params->{'client_id'} = $client_id; | ||
| 524 | $query_params->{'client_secret'} = $client_secret; | ||
| 525 | } | ||
| 526 | |||
| 527 | # oauth | ||
| 528 | if (my $access_token = delete $tokens->{access_token}) { | ||
| 529 | $header_params->{'Authorization'} = 'Bearer ' . $access_token; | ||
| 530 | } | ||
| 531 | |||
| 532 | # other keys | ||
| 533 | foreach my $token_name (keys %$tokens) { | ||
| 534 | my $in = $tokens->{$token_name}->{in}; | ||
| 535 | my $token = $self->get_api_key_with_prefix($token_name); | ||
| 536 | if ($in eq 'head') { | ||
| 537 | $header_params->{$token_name} = $token; | ||
| 538 | } | ||
| 539 | elsif ($in eq 'query') { | ||
| 540 | $query_params->{$token_name} = $token; | ||
| 541 | } | ||
| 542 | else { | ||
| 543 | die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')"; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | } | ||
| 547 | |||
| 548 | |||
| 549 | 1; |