Net-Simplify

 view release on metacpan or  search on metacpan

lib/Net/Simplify/SimplifyApi.pm  view on Meta::CPAN

            } else {
                croak(Net::Simplify::BadRequestException->new('', $code, _get_oauth_error("Unknown OAuth error", $error_code, $error_desc)));
            }
        } else {
            croak(Net::Simplify::SystemException->new("An unexpected error has been raised for an OAuth request", $code));
        }
    }
}

sub _get_oauth_error {
    my ($msg, $error_code, $error_desc) = @_;

    my $error = {
        error => {
            code => 'oauth_error',
            message => "${msg}, error code '${error_code}', description '${error_desc}'"
        }
    };
    
    $error;            
}
        

sub get_authentication {
    my ($class, $auth) = @_;

    $auth = Net::Simplify::Authentication->create() unless defined $auth;

    if (! $auth->isa('Net::Simplify::Authentication')) {
        croak(Net::Simplify::IllegalArgumentException->new("authentication object is not a Net::Simplify::Authentication object"));
    }
    
    $auth;
}


sub decode_event {
    my ($class, $params, $auth) = @_;

    _check_auth($auth);
    _check_param($params, 'payload');

    Net::Simplify::Jws->decode($params->{payload}, $params->{url}, $auth);
}


sub is_live_key {
    my ($class, $public_key) = @_;

    _is_live_key($public_key);
}


sub _check_auth {
    my ($auth) = @_;

    if (!defined $auth->{public_key}) {
        croak(Net::Simplify::IllegalArgumentException->new("No public key"));
    }

    if (!defined $auth->{private_key}) {
        croak(Net::Simplify::IllegalArgumentException->new("No private key"));
    }
}


sub _check_param {
    my ($params, $name) = @_;

    if (!defined($params->{$name})) {
        croak(Net::Simplify::IllegalArgumentException->new("Missing paramater '${name}'"));
    }
}
        

sub _build_url {

    my ($domain, $op, $params, $auth) = @_;
    my $url;
    eval{
        $url = $Net::Simplify::api_base_sandbox_url;
        if (_is_live_key($auth->public_key)) {
            $url = $Net::Simplify::api_base_live_url;
        }
        if ($op eq 'find' || $op eq 'update' || $op eq 'delete') {
            my $id = $params->{id};
            $url .= "/${domain}/${id}"
        } else {
            $url .= "/${domain}"
        }

        if ($op eq 'list' && defined $params) {
            my @P = ();

            if (defined $params->{offset}) {
                my $v = $params->{offset};
                push(@P, uri_encode("offset=${v}"));
            }
            if (defined $params->{max}) {
                my $v = $params->{max};
                push(@P, uri_encode("max=${v}"));
            }
            if (defined $params->{sorting}) {
                while (my ($k, $v) = each(%{$params->{sorting}})) {
                    push(@P, uri_encode("sorting[${k}]=${v}"));
                }
            }
            if (defined $params->{filter}) {
                while (my ($k, $v) = each(%{$params->{filter}})) {
                    push(@P, uri_encode("filter[${k}]=${v}"));
                }
            }
            $url .= "?" . join('&', @P) if @P;
        }
    };
    if ($@) {
        croak(Net::Simplify::BadRequestException->new("Error building requests URL from parameters: " . $@));
    }

    $url;
}



( run in 0.559 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )