AsposeCellsCloud-CellsApi

 view release on metacpan or  search on metacpan

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

            return $_instance->from_hash(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';
    } 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;
	}



( run in 2.508 seconds using v1.01-cache-2.11-cpan-9581c071862 )