AsposeStorageCloud-StorageApi

 view release on metacpan or  search on metacpan

lib/AsposeStorageCloud/ApiClient.pm  view on Meta::CPAN


use strict;
use warnings;
use utf8;

use MIME::Base64;
use LWP::UserAgent;
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 Log::Any qw($log);
use Carp;
use Module::Runtime qw(use_module);
use Digest::HMAC_SHA1;
use MIME::Base64;

use AsposeStorageCloud::Configuration;

sub new
{
	
	if(not defined $AsposeStorageCloud::Configuration::app_sid or $AsposeStorageCloud::Configuration::app_sid eq ''){
		croak("Aspose Cloud App SID key is missing.");
    }
	
	if(not defined $AsposeStorageCloud::Configuration::api_key or $AsposeStorageCloud::Configuration::api_key eq ''){
		croak("Aspose Cloud API key is missing.");
    }
    
    my $class = shift;
    my (%args) = (
        'ua' => LWP::UserAgent->new,
        'base_url' => $AsposeStorageCloud::Configuration::api_server,
        @_
    );
  
    return bless \%args, $class;
}

# Set the user agent of the API client
#
# @param string $user_agent The user agent of the API client
#
sub set_user_agent {
    my ($self, $user_agent) = @_;
    $self->{http_user_agent}= $user_agent;
}

# Set timeout
#
# @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
# 
sub set_timeout {
    my ($self, $seconds) = @_;
    if (!looks_like_number($seconds)) {
        croak('Timeout variable must be numeric.');
    }
    $self->{http_timeout} = $seconds;
}

# 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 $postData parameters to be placed in POST body
# @param array $headerParams parameters to be place in request header
# @return mixed
sub call_api {
    my $self = shift;
    my ($resource_path, $method, $query_params, $post_params, $header_params, $body_data, $auth_settings) = @_;
  
	$resource_path =~ s/\Q{appSid}\E/$AsposeStorageCloud::Configuration::app_sid/g;
  
    my $_url = $self->{base_url} . $resource_path;
  
    # update signature
    my $signature = $self->sign($_url, $AsposeStorageCloud::Configuration::app_sid, $AsposeStorageCloud::Configuration::api_key);
	
	$_url = $_url . '&signature=' . $signature;
	
	if($AsposeStorageCloud::Configuration::debug){
		print "\nFinal URL: ".$method."::".$_url;
	}
	# body data
    $body_data = to_json($body_data->to_hash) if defined $body_data && $body_data->can('to_hash'); # model to json string
    my $_body_data = keys %$post_params > 1 ? $post_params : $body_data;
   
    #my $_body_data = $body_data;
  
    # 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);
    }
    elsif ($method eq 'HEAD') {
        my $headers = HTTP::Headers->new(%$header_params);
        $_request = HEAD($_url,%$header_params); 
    }
    elsif ($method eq 'DELETE') { #TODO support form data



( run in 1.311 second using v1.01-cache-2.11-cpan-39bf76dae61 )