AsposeSlidesCloud-SlidesApi
view release on metacpan or search on metacpan
lib/AsposeSlidesCloud/ApiClient.pm view on Meta::CPAN
while ( my ($_key, $_type) = each %{$current_types} ) {
my $_json_attribute = $instance->attribute_map->{$_key};
if ($_type =~ /^array\[/i) { # array
my $_subclass = substr($_type, 6, -1);
my @_array = ();
foreach my $_element (@{$hash->{$_json_attribute}}) {
if (defined $_element) {
push @_array, $self->_deserialize($_subclass, $_element);
} else {
push @_array, undef;
}
}
foreach my $_element (@{$hash->{lcfirst($_json_attribute)}}) {
if (defined $_element) {
push @_array, $self->_deserialize(lcfirst($_subclass), $_element);
} else {
push @_array, undef;
}
}
$instance->{$_key} = \@_array;
} elsif (exists $hash->{$_json_attribute}) { #hash(model), primitive, datetime
$instance->{$_key} = $self->_deserialize($_type, $hash->{$_json_attribute});
} elsif (exists $hash->{lcfirst($_json_attribute)}) { #hash(model), primitive, datetime
$instance->{$_key} = $self->_deserialize($_type, $hash->{lcfirst($_json_attribute)});
}
}
return $instance;
}
# deserialize non-array data
sub _deserialize {
my ($self, $type, $data) = @_;
if ($type eq 'DateTime') {
return DateTime->from_epoch(epoch => str2time($data));
} elsif ( grep( /^$type$/, ('int', 'double', 'string', 'boolean'))) {
return $data;
} else { # hash(model)
my $class = AsposeSlidesCloud::ClassRegistry->get_class_name(ucfirst($type), $data);
my $_instance = use_module("AsposeSlidesCloud::Object::$class")->new();
return $self->from_hash($_instance, $data);
}
}
# Deserialize a JSON string into an object
#
# @param string $class class name is passed as a string
# @param string $data data of the body
# @return object an instance of $class
sub deserialize
{
my ($self, $class, $data) = @_;
if (not defined $data) {
return undef;
} elsif ( (substr($class, 0, 5)) eq 'HASH[') { #hash
if ($class =~ /^HASH\[(.*),(.*)\]$/) {
my ($key_type, $type) = ($1, $2);
my %hash;
my $decoded_data = decode_json $data;
foreach my $key (keys %$decoded_data) {
if (ref $decoded_data->{$key} eq 'HASH') {
$hash{$key} = $self->deserialize($type, encode_json $decoded_data->{$key});
} else {
$hash{$key} = $self->deserialize($type, $decoded_data->{$key});
}
}
return \%hash;
} else {
#TODO log error
}
} elsif ( (substr($class, 0, 6)) eq 'ARRAY[' ) { # array of data
return $data if $data eq '[]'; # return if empty array
my $_sub_class = substr($class, 6, -1);
my $_json_data = decode_json $data;
my @_values = ();
foreach my $_value (@$_json_data) {
if (ref $_value eq 'ARRAY') {
push @_values, $self->deserialize($_sub_class, encode_json $_value);
} else {
push @_values, $self->deserialize($_sub_class, $_value);
}
}
return \@_values;
} elsif ($class eq 'DateTime') {
return DateTime->from_epoch(epoch => str2time($data));
} elsif (grep /^$class$/, ('string')) {
$data =~ s/^"(.*)"$/$1/;
return $data;
} elsif (grep /^$class$/, ('int', 'float', 'bool', 'object', 'File')) {
return $data;
} else { # model
$class = AsposeSlidesCloud::ClassRegistry->get_class_name($class, $data);
my $_instance = use_module("AsposeSlidesCloud::Object::$class")->new;
if (ref $data eq "HASH") {
return $self->from_hash($_instance, $data);
} else { # string, need to json decode first
return $self->from_hash($_instance, decode_json $data);
}
}
}
# return 'Accept' based on an array of accept provided
# @param [Array] header_accept_array Array fo 'Accept'
# @return String Accept (e.g. application/json)
sub select_header_accept
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return undef;
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}
}
# return the content type based on an array of content-type provided
# @param [Array] content_type_array Array fo content-type
# @return String Content-Type (e.g. application/json)
sub select_header_content_type
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return 'application/json'; # default to application/json
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} elsif ($header[0] eq 'multipart/form-data') {
return 'application/octet-stream';
} else {
return $header[0]
}
}
# add auth, user agent and other headers
#
# @param array $headerParams header parameters (by ref)
sub update_headers {
my ($self, $header_params) = @_;
my $custom_headers = $self->{config}{custom_headers};
foreach my $key (keys %$custom_headers) {
$header_params->{$key} = $self->{config}{custom_headers}{$key};
}
$self->update_params_for_auth($header_params);
}
# update header and query param based on authentication setting
#
# @param array $headerParams header parameters (by ref)
sub update_params_for_auth {
my ($self, $header_params) = @_;
if ((defined $self->{config}{app_sid} && $self->{config}{app_sid} ne "") && (!defined $self->{config}{access_token} || $self->{config}{access_token} eq "")) {
my $_url = ($self->{config}{auth_base_url} // $self->{config}{base_url}) . "/connect/token";
my $_request = POST($_url, {}, Content => 'grant_type=client_credentials&client_id='.$self->{config}{app_sid}.'&client_secret='.$self->{config}{app_key});
my $_response = $self->{ua}->request($_request);
unless ($_response->is_success) {
my $content = sprintf("%s", $_response->content);
croak(sprintf "API Exception(%s): %s\n%s", 401, $_response->message, $content);
}
my $decoded_data = decode_json $_response->content;
$self->{config}{access_token} = $decoded_data->{access_token};
}
if (defined $self->{config}{access_token} && $self->{config}{access_token} ne "") {
$header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
}
}
sub _log {
my ($self, $message) = @_;
if (defined $self->{config}{logger}) {
$self->{config}{logger}->($message);
} else {
$log->debug($message);
}
}
1;
( run in 2.380 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )