AsposeCellsCloud-CellsApi

 view release on metacpan or  search on metacpan

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


    my $_method = 'POST';
    my $query_params = {};
    my $header_params = {};
    my $form_params = {};

    # 'Accept' and 'Content-Type' header
    my $_header_accept = $self->select_header_accept('application/json');
    if ($_header_accept) {
        $header_params->{'Accept'} = $_header_accept;
    }
    $header_params->{'Content-Type'} = $self->select_header_content_type('application/x-www-form-urlencoded');

    # form params
    if ( exists $args{'grant_type'} ) {
                $form_params->{'grant_type'} = $self->to_form_value($args{'grant_type'});
    }

    # form params
    if ( exists $args{'client_id'} ) {
                $form_params->{'client_id'} = $self->to_form_value($args{'client_id'});
    }

    # form params
    if ( exists $args{'client_secret'} ) {
                $form_params->{'client_secret'} = $self->to_form_value($args{'client_secret'});
    }

    my $_body_data;
    # authentication setting, if any
    my $auth_settings = [qw()];

    # make the API Call
    my $response = $self->call_api($_resource_path, $_method,
                                           $query_params, $form_params,
                                           $header_params, $_body_data, $auth_settings, 'get_token');
    if (!$response) {
        return;
    }
    my $_response_object = $self->deserialize('AccessTokenResponse', $response);
    $self->{get_access_token_time} = time();
    return $_response_object;
}
# check access token
sub check_access_token {
    my ($self, %args) = @_;
    if(!$self->need_auth()){
        return;
    }
    if($self->{get_access_token_time}){
        my $difference_in_seconds=time() - $self->{get_access_token_time};
        if($difference_in_seconds < 86300){
            return;
        }
    }
    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;
    $self->{config}->{access_token} = $access_token;
}

# 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,$get_token) = @_;

    # update parameters based on authentication settings
    $self->update_params_for_auth($header_params, $query_params, $auth_settings ); 


    my $_url = $self->{config}{base_url}."/" . $resource_path;
    if($get_token){
        $_url = $self->{config}{base_url} . $resource_path;
    }

    # build header

    $header_params->{'x-aspose-client'} = 'perl sdk';
    $header_params->{'x-aspose-client-version'} = '26.3.1';  
    # build query 
    if (%$query_params) {
        $_url = ($_url . '?' . eval { URI::Query->new($query_params)->stringify });
    }


    # 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 = %$post_params ? $post_params : $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'};

        if($_body_data){
            $_request = POST($_url, %$header_params, Content => $_body_data);
        }
        else{
            $_request = POST($_url, %$header_params);
        }  
    }
    elsif ($method eq 'PUT') {
        # multipart
        $header_params->{'Content-Type'}  = lc $header_params->{'Content-Type'} eq 'multipart/form' ? 
            'form-data' : $header_params->{'Content-Type'};

        if($_body_data){
            $_request = PUT($_url, %$header_params, Content => $_body_data);
        }
        else{
            $_request = PUT($_url, %$header_params);
        }
    }
    elsif ($method eq 'GET') {
        my $headers = HTTP::Headers->new(%$header_params);

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

# @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';
    } else {
        return join(',', @header);
    }

}

# Get API key (with prefix if set)
# @param string key name
# @return string API key with the prefix
sub get_api_key_with_prefix
{
	my ($self, $key_name) = @_;

	my $api_key = $self->{config}{api_key}{$key_name};

	return unless $api_key;

	my $prefix = $self->{config}{api_key_prefix}{$key_name};
	return $prefix ? "$prefix $api_key" : $api_key;
}	

# update header and query param based on authentication setting
#  
# @param array $headerParams header parameters (by ref)
# @param array $queryParams query parameters (by ref)
# @param array $authSettings array of authentication scheme (e.g ['api_key'])
sub update_params_for_auth {
    my ($self, $header_params, $query_params, $auth_settings) = @_;
	if(!$self->need_auth()){
        return;
    }    
    return $self->_global_auth_setup($header_params, $query_params) 
    	unless $auth_settings && @$auth_settings;

    # one endpoint can have more than 1 auth settings
    foreach my $auth (@$auth_settings) {
        # determine which one to use
        if (!defined($auth)) {
            # TODO show warning about auth setting not defined
        }
        elsif ($auth eq 'appsid') {

            my $api_key = $self->get_api_key_with_prefix('appsid');
            if ($api_key) {
                $query_params->{'appsid'} = $api_key;
            }
        }
elsif ($auth eq 'oauth') {

            if ($self->{config}{access_token}) {
                $header_params->{'Authorization'} = 'Bearer ' . $self->{config}{access_token};
            }
        }
elsif ($auth eq 'signature') {

            my $api_key = $self->get_api_key_with_prefix('signature');
            if ($api_key) {
                $query_params->{'signature'} = $api_key;
            }
        }
        else {
       	    # TODO show warning about security definition not found
        }
    }
}

# The endpoint API class has not found any settings for auth. This may be deliberate, 
# in which case update_params_for_auth() will be a no-op. But it may also be that the 
# OpenAPI Spec does not describe the intended authorization. So we check in the config for any 
# auth tokens and if we find any, we use them for all endpoints; 
sub _global_auth_setup {
	my ($self, $header_params, $query_params) = @_; 

	my $tokens = $self->{config}->get_tokens;
	return unless keys %$tokens;

	# basic
	if (my $uname = delete $tokens->{username}) {
		my $pword = delete $tokens->{password};
		$header_params->{'Authorization'} = 'Basic '.encode_base64($uname.":".$pword);
	}
	# sid key
	if (my $client_id = delete $tokens->{client_id}) {
		my $client_secret = delete $tokens->{client_secret};
		$query_params->{'client_id'} = $client_id;
        $query_params->{'client_secret'} = $client_secret;
	}

	# oauth
	if (my $access_token = delete $tokens->{access_token}) {
		$header_params->{'Authorization'} = 'Bearer ' . $access_token;
	}

	# other keys
	foreach my $token_name (keys %$tokens) {
		my $in = $tokens->{$token_name}->{in};
		my $token = $self->get_api_key_with_prefix($token_name);
		if ($in eq 'head') {
			$header_params->{$token_name} = $token;
		}
		elsif ($in eq 'query') {
			$query_params->{$token_name} = $token;
		}
		else {
			die "Don't know where to put token '$token_name' ('$in' is not 'head' or 'query')";
		}
	}
}


1;



( run in 1.954 second using v1.01-cache-2.11-cpan-63c85eba8c4 )