AsposeSlidesCloud-SlidesApi
view release on metacpan or search on metacpan
lib/AsposeSlidesCloud/ApiClient.pm view on Meta::CPAN
=end comment
=cut
package AsposeSlidesCloud::ApiClient;
use strict;
use warnings;
use utf8;
use MIME::Base64;
use LWP::UserAgent;
use HTTP::Date;
use HTTP::Headers;
use HTTP::Response;
use HTTP::Request::Common qw(DELETE POST GET HEAD PUT);
use HTTP::Status;
use URI::Query;
use JSON;
use URI::Escape;
use Scalar::Util;
use Carp;
use Module::Runtime qw(use_module);
use Log::Any qw($log);
use Log::Any::Adapter ('Stderr');
use AsposeSlidesCloud::ApiInfo;
use AsposeSlidesCloud::ClassRegistry;
use AsposeSlidesCloud::Configuration;
sub new {
my $class = shift;
my $config;
my %params = @_;
if (defined $params{config}) {
$config = $params{config};
} else {
$config = AsposeSlidesCloud::Configuration->new(@_);
}
my $ua = LWP::UserAgent->new;
if ($config->{allow_insecure_requests}) {
$ua->ssl_opts(SSL_verify_mode => 0);
$ua->ssl_opts(verify_hostname => 0);
}
if (defined $config->{http_request_timeout}) {
$ua->{timeout} = $config->{http_request_timeout};
}
my (%args) = ('ua' => $ua, 'config' => $config);
return bless \%args, $class;
}
# make the HTTP request
# @param string $resourcePath path to method endpoint
# @param string $method method to call
# @param array $queryParams parameters to be place in query URL
# @param array $postParams parameters to be placed in POST body
# @param array $headerParams parameters to be place in request header
# @param array $body_data data to be place in request body
# @param array $files files to be posted
# @return mixed
sub call_api {
my $self = shift;
my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $files) = @_;
my $had_token = defined $self->{config}{access_token} && $self->{config}{access_token} ne "";
my $response = $self->call_api_once($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $files);
my $content = sprintf("%s", $response->content);
if ($had_token && ($response->code eq 401 || ($response->code eq 500 && !$content))) {
$self->{config}{access_token} = "";
$response = $self->call_api_once($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $files);
$content = sprintf("%s", $response->content);
}
unless ($response->is_success) {
croak(sprintf "API Exception(%s): %s\n%s", $response->code, $response->message, $content);
}
return $content;
}
sub call_api_once {
my $self = shift;
my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $files) = @_;
$self->update_headers($header_params);
my $_url = $self->{config}{base_url}."/".$self->{config}{version}.$resource_path;
# build query
if (%$query_params) {
$_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
}
my $_body_data = %$post_params ? $post_params : $self->get_body_data($header_params, $body_data, $files);
# Make the HTTP request
my $_request;
if ($method eq 'POST') {
# multipart
$header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
'form-data' : $header_params->{'Content-Type'};
$_request = POST($_url, %$header_params, Content => $_body_data);
}
elsif ($method eq 'PUT') {
# multipart
$header_params->{'Content-Type'} = lc $header_params->{'Content-Type'} eq 'multipart/form' ?
'form-data' : $header_params->{'Content-Type'};
$_request = PUT($_url, %$header_params, Content => $_body_data);
}
elsif ($method eq 'GET') {
my $headers = HTTP::Headers->new(%$header_params);
$_request = GET($_url, %$header_params);
}
( run in 1.320 second using v1.01-cache-2.11-cpan-5a3173703d6 )