| 1 | package WWW::Yotpo; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use 5.008_005; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our $VERSION = '0.01'; | ||
| 6 | |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use MIME::Base64; | ||
| 11 | use HTTP::Request; | ||
| 12 | use vars qw/$errstr/; | ||
| 13 | |||
| 14 | sub errstr { $errstr } | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my %args = @_ % 2 ? %{$_[0]} : @_; | ||
| 19 | |||
| 20 | $args{client_id} or croak 'client_id is required.'; | ||
| 21 | $args{client_secret} or croak 'client_secret is required.'; | ||
| 22 | |||
| 23 | $args{ua} ||= LWP::UserAgent->new; | ||
| 24 | |||
| 25 | return bless \%args, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub oauth_token { | ||
| 29 | my $self = shift; | ||
| 30 | my %params = @_ % 2 ? %{$_[0]} : @_; | ||
| 31 | $self->request('oauth/token', 'POST', { | ||
| 32 | "client_id" => $self->{client_id}, | ||
| 33 | "client_secret" => $self->{client_secret}, | ||
| 34 | "grant_type" => "client_credentials" | ||
| 35 | }); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub mass_create { | ||
| 39 | my $self = shift; | ||
| 40 | my %params = @_ % 2 ? %{$_[0]} : @_; | ||
| 41 | $self->request('apps/' . $self->{client_id} . '/purchases/mass_create', 'POST', { | ||
| 42 | Content => encode_json(\%params), | ||
| 43 | utoken => $params{utoken} | ||
| 44 | }); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub purchases { | ||
| 48 | my $self = shift; | ||
| 49 | my %params = @_ % 2 ? %{$_[0]} : @_; | ||
| 50 | $self->request('apps/' . $self->{client_id} . '/purchases', 'GET', \%params); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub request { | ||
| 54 | my ($self, $url, $method, $params) = @_; | ||
| 55 | |||
| 56 | $url = 'https://api.yotpo.com/' . $url; | ||
| 57 | |||
| 58 | $params ||= {}; | ||
| 59 | my $content = delete $params->{Content}; | ||
| 60 | |||
| 61 | my $uri = URI->new($url); | ||
| 62 | $uri->query_form($params) if keys %$params; | ||
| 63 | |||
| 64 | my $req = HTTP::Request->new($method, $uri); | ||
| 65 | if ($content) { | ||
| 66 | $req->content($content); | ||
| 67 | $req->content_type('application/json'); | ||
| 68 | } | ||
| 69 | my $res = $self->{ua}->request($req); | ||
| 70 | # print Dumper(\$res); use Data::Dumper; | ||
| 71 | if (not $res->header('Content-Type') =~ /json/) { | ||
| 72 | $errstr = $res->status_line; | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | return decode_json($res->decoded_content); | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | __END__ | ||