| 1 | package PX::API; | ||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use version; our $VERSION = qv('0.0.3'); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use base qw/Class::Accessor::Fast/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | __PACKAGE__->mk_accessors( qw/api_key secret method args do_sign api_sig ua/ ); | ||
| 10 | |||
| 11 | use LWP::UserAgent; | ||
| 12 | use PX::API::Request; | ||
| 13 | use PX::API::Response; | ||
| 14 | use Digest::MD5 qw(md5_hex); | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my $args = shift; | ||
| 19 | |||
| 20 | $class = ref($class) || $class; | ||
| 21 | my $self = bless {}, $class; | ||
| 22 | |||
| 23 | my $ua = LWP::UserAgent->new(agent => $self->_agent, timeout => 60); | ||
| 24 | $self->ua($ua); | ||
| 25 | $self->api_key($args->{'api_key'}); | ||
| 26 | $self->secret($args->{'secret'}); | ||
| 27 | warn "No API key present!!" unless ($self->api_key); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub call { | ||
| 33 | my $self = shift; | ||
| 34 | my $meth = shift; | ||
| 35 | my $args = shift; | ||
| 36 | |||
| 37 | $self->args($args); | ||
| 38 | my $req = PX::API::Request->new({ | ||
| 39 | method => $meth, | ||
| 40 | args => $args, | ||
| 41 | }); | ||
| 42 | $self->execute_request($req); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub execute_request { | ||
| 46 | my $self = shift; | ||
| 47 | my $req = shift; | ||
| 48 | |||
| 49 | $req->{'args'}->{'api_key'} = $self->api_key; | ||
| 50 | $req->{'args'}->{'api_sig'} = $self->_sign_args($req->{'args'}) if $self->do_sign; | ||
| 51 | |||
| 52 | my $uri = $req->uri; | ||
| 53 | $uri .= "?" . $self->_query_string($req->{'args'}); | ||
| 54 | $req->uri($uri); | ||
| 55 | my $resp = $self->ua->request($req); | ||
| 56 | bless $resp, 'PX::API::Response'; | ||
| 57 | $resp->_init({format => $req->{'args'}->{'format'}}); | ||
| 58 | |||
| 59 | if ($resp->{'_rc'} != 200) { | ||
| 60 | $resp->fault(0,"API returned status code ($resp->{_rc})"); | ||
| 61 | return $resp; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $parser = $resp->{'parser'}; | ||
| 65 | my $ref = $parser->parse($resp->{'_content'}); | ||
| 66 | $resp->fault(0,"Error parsing server response") if (!defined $ref->{response}); | ||
| 67 | |||
| 68 | my $stat = $ref->{response}->{status} if defined $ref->{response}->{status}; | ||
| 69 | if ($stat eq "error") { | ||
| 70 | my $error = $ref->{response}->{content}->{error}; | ||
| 71 | $resp->fault(0,"($error->{err_code}) $error->{err_string}"); | ||
| 72 | return $resp; | ||
| 73 | } | ||
| 74 | |||
| 75 | if ($ref->{response}->{status} eq "ok") { | ||
| 76 | $resp->success($ref->{response}); | ||
| 77 | return $resp; | ||
| 78 | } | ||
| 79 | |||
| 80 | $resp->fault(0,"API returned an invalid status code"); | ||
| 81 | return $resp; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub auth_url { | ||
| 85 | my $self = shift; | ||
| 86 | my $perms = shift; | ||
| 87 | return undef unless defined $self->secret; | ||
| 88 | |||
| 89 | my $args = { | ||
| 90 | api_key => $self->api_key, | ||
| 91 | perms => $perms, | ||
| 92 | }; | ||
| 93 | |||
| 94 | my $sig = $self->_sign_args($args); | ||
| 95 | $args->{api_sig} = $sig; | ||
| 96 | |||
| 97 | my $q = $self->_query_string($args); | ||
| 98 | my $uri = "http://services.peekshows.com/auth?" . $q; | ||
| 99 | return $uri; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _sign_args { | ||
| 103 | my $self = shift; | ||
| 104 | my $args = shift; | ||
| 105 | |||
| 106 | my $sig = $self->secret; | ||
| 107 | foreach my $k(sort {$a cmp $b} keys %{$args}) { | ||
| 108 | $sig .= $k . $args->{$k}; | ||
| 109 | } | ||
| 110 | return md5_hex($sig); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _query_string { | ||
| 114 | my $self = shift; | ||
| 115 | my $args = shift; | ||
| 116 | |||
| 117 | my @pairs; | ||
| 118 | foreach my $k(keys %{$args}) { | ||
| 119 | my $kv = $k . "=" . $args->{$k}; | ||
| 120 | push(@pairs,$kv); | ||
| 121 | } | ||
| 122 | my $q = join("&",@pairs); | ||
| 123 | return $q; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _agent { __PACKAGE__ . "/" . $VERSION } | ||
| 127 | |||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ | ||