Net-Xero
view release on metacpan or search on metacpan
lib/Net/Xero.pm view on Meta::CPAN
has 'request_token' => (is => 'rw');
has 'request_secret' => (is => 'rw');
has 'access_token' => (is => 'rw');
has 'access_secret' => (is => 'rw');
has 'template_path' => (
is => 'rw',
default => ( dist_dir('Net-Xero') ),
);
#has 'template_path' => (is => 'rw', isa => 'Str');
=head1 SYNOPSIS
Quick summary of what the module does.
For a private application you will receive the access_token/secret when you
submit your X509 to xero. You can ignore login/auth in this instance as follows:
use Net::Xero;
my $foo = Net::Xero->new(
access_token => 'YY',
access_secret => 'XX',
);
=head1 EXPORT
A list of functions that can be exported. You can delete this section
if you don't export anything, such as for a purely object-oriented module.
=head1 FUNCTIONS
=cut
=head2 login
This sets up the initial OAuth handshake and returns the login URL. This
URL has to be clicked by the user and the the user then has to accept
the application in xero.
Xero then redirects back to the callback URL defined with
C<$self-E<gt>callback_url>. If the user already accepted the application the
redirect may happen without the user actually clicking anywhere.
=cut
sub login {
my $self = shift;
my $request = Net::OAuth->request("request token")->new(
consumer_key => $self->key,
consumer_secret => $self->secret,
request_url => $self->api_url . '/oauth/RequestToken',
request_method => 'POST',
signature_method => 'RSA-SHA1',
timestamp => time,
nonce => $self->nonce,
callback => $self->callback_url,
);
my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
$request->sign($private_key);
my $res = $self->ua->request(GET $request->to_url);
if ($res->is_success) {
my $response =
Net::OAuth->response('request token')
->from_post_body($res->content);
$self->request_token($response->token);
$self->request_secret($response->token_secret);
print STDERR "Got Request Token ", $response->token, "\n"
if $self->debug;
print STDERR "Got Request Token Secret ", $response->token_secret, "\n"
if $self->debug;
return
$self->api_url
. '/oauth/Authorize?oauth_token='
. $response->token
. '&oauth_callback='
. $self->callback_url;
}
else {
$self->error($res->status_line);
warn "Something went wrong: " . $res->status_line;
}
}
=head2 auth
The auth method changes the initial request token into access token that we need
for subsequent access to the API. This method only has to be called once
after login.
=cut
sub auth {
my $self = shift;
my $request = Net::OAuth->request("access token")->new(
consumer_key => $self->key,
consumer_secret => $self->secret,
request_url => $self->api_url . '/oauth/AccessToken',
request_method => 'POST',
signature_method => 'RSA-SHA1',
timestamp => time,
nonce => $self->nonce,
callback => $self->callback_url,
token => $self->request_token,
token_secret => $self->request_secret,
);
my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
$request->sign($private_key);
my $res = $self->ua->request(GET $request->to_url);
if ($res->is_success) {
my $response =
Net::OAuth->response('access token')->from_post_body($res->content);
$self->access_token($response->token);
$self->access_secret($response->token_secret);
print STDERR "Got Access Token ", $response->token, "\n"
if $self->debug;
print STDERR "Got Access Token Secret ", $response->token_secret, "\n"
if $self->debug;
}
else {
$self->error($res->status_line);
$self->error($res->status_line . "\n" . $res->content);
}
}
=head2 set_cert
=cut
sub set_cert {
my ($self, $path) = @_;
my $cert = io $path;
$self->cert($cert->all);
}
=head2 get_inv_by_ref
=cut
sub get_inv_by_ref {
my ($self, @ref) = @_;
my $path = 'Invoices?where=Reference.ToString()=="' . (shift @ref) . '"';
$path .= ' OR Reference.ToString()=="' . $_ . '"' foreach (@ref);
return $self->_talk($path, 'GET');
}
=head2 get_invoices
=cut
sub get_invoices {
my ($self, $where) = @_;
my $path = 'Invoices';
return $self->_talk($path, 'GET') unless (ref $where eq 'HASH');
$path .= '?where=';
my $conjunction =
(exists $where->{'conjunction'}) ? uc $where->{'conjunction'} : 'OR';
my $first = 1;
foreach my $key (%{$where}) {
$path .= " $conjunction " unless $first;
lib/Net/Xero.pm view on Meta::CPAN
sub get {
my ($self, $command) = @_;
return $self->_talk($command, 'GET');
}
=head2 post
=cut
sub post {
my ($self, $command, $hash) = @_;
return $self->_talk($command, 'POST', $hash);
}
=head2 put
=cut
sub put {
my ($self, $command, $hash) = @_;
return $self->_talk($command, 'PUT', $hash);
}
=head1 INTERNAL API
=head2 _talk
_talk handles the access to the restricted resources. You should
normally not need to access this directly.
=cut
sub _talk {
my ($self, $command, $method, $hash) = @_;
$self->clear_error;
my $path = join('', map(ucfirst, split(/_/, $command)));
my $request_url = $self->api_url . '/api.xro/2.0/' . $path;
my %opts = (
consumer_key => $self->key,
consumer_secret => $self->secret,
request_url => $request_url,
request_method => $method,
signature_method => 'RSA-SHA1',
timestamp => time,
nonce => join('', rand_chars(size => 16, set => 'alphanumeric')),
token => $self->access_token,
token_secret => $self->access_secret,
);
my $content;
if ($method =~ m/^(POST|PUT)$/) {
$hash->{command} ||= $command;
$content = $self->_template($hash);
$opts{extra_params} = { xml => $content } if ($method eq 'POST');
}
my $request = Net::OAuth->request("protected resource")->new(%opts);
my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
$request->sign($private_key);
#my $req = HTTP::Request->new($method, $request->to_url);
my $req = HTTP::Request->new($method, $request_url);
if ($hash and ($method eq 'POST')) {
$req->content($request->to_post_body);
$req->header('Content-Type' =>
'application/x-www-form-urlencoded; charset=utf-8');
}
else {
$req->content($content) if ($hash and ($method eq 'PUT'));
$req->header(Authorization => $request->to_authorization_header);
}
print STDERR $req->as_string if $self->debug;
my $res = $self->ua->request($req);
if ($res->is_success) {
print STDERR "Got Content ", $res->content, "\n" if $self->debug;
return XMLin($res->content);
}
else {
warn "Something went wrong: " . $res->content;
$self->error($res->status_line . " " . $res->content);
}
return;
}
=head2 _template
=cut
sub _template {
my ($self, $hash) = @_;
$hash->{command} .= '.tt';
print STDERR Dumper($hash) if $self->debug;
my $tt;
if ($self->debug) {
$tt = Template->new(
#DEBUG => 'all',
INCLUDE_PATH => [ $self->template_path ],
);
}
else {
$tt = Template->new(INCLUDE_PATH => [ $self->template_path ]);
}
my $template = '';
$tt->process('frame.tt', $hash, \$template)
|| die $tt->error;
utf8::encode($template);
print STDERR $template if $self->debug;
return $template;
}
=head1 AUTHOR
Lenz Gschwendtner, C<< <norbu09 at cpan.org> >>
( run in 0.233 second using v1.01-cache-2.11-cpan-bf8d7bb2d05 )