| 1 | package WWW::PayPal::Role::HTTP; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: HTTP + OAuth2 role for the PayPal REST API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | requires 'client_id'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | requires 'secret'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | requires 'base_url'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | has ua => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
_build_ua: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | agent => 'WWW-PayPal/' . $WWW::PayPal::Role::HTTP::VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | has _access_token => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | has _token_expires_at => ( is => 'rw', default => sub { 0 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _fetch_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_token: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | croak "client_id required" unless $self->client_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | croak "secret required" unless $self->secret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $uri = URI->new($self->base_url . '/v1/oauth2/token'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $req = HTTP::Request->new(POST => $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $req->header( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Authorization => 'Basic ' . encode_base64($self->client_id . ':' . $self->secret, ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | Accept => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $req->content('grant_type=client_credentials'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $log->debugf('PayPal OAuth2 token request to %s', $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $res = $self->ua->request($req); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | unless ($res->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $log->errorf('PayPal OAuth2 failed: %s', $res->status_line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | croak 'PayPal OAuth2 failed: ' . $res->status_line . ' ' . $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $data = decode_json($res->decoded_content); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $self->_access_token($data->{access_token}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # refresh 60s before expiry to be safe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->_token_expires_at(time + ($data->{expires_in} // 0) - 60); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"expires_in"} // 0
MC/DC0%0/1
$data->{"expires_in"} // 0
$data->{"expires_in"}
Truth table0%0/2
A
A= $data->{"expires_in"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return $data->{access_token}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub access_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
access_token: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return $self->_access_token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->_access_token and time < $self->_token_expires_at
MC/DC0%0/2
$self->_access_token and time < $self->_token_expires_at
$self->_access_token
time < $self->_token_expires_at
Truth table0%0/3
A and B
A= $self->_access_tokenB= time < $self->_token_expires_at
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | if $self->_access_token && time < $self->_token_expires_at; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $self->_fetch_token; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $method, $path, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $uri = URI->new($self->base_url . $path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $uri->query_form($args{query}) if $args{query}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $req = HTTP::Request->new($method => $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $req->header( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | Authorization => 'Bearer ' . $self->access_token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | Accept => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | for my $k (keys %{$args{headers} || {}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'headers'} || {}
MC/DC0%0/1
$args{'headers'} || {}
$args{'headers'}
Truth table0%0/2
A
A= $args{'headers'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $req->header($k => $args{headers}{$k}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (defined $args{body}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $ct = $args{content_type} || 'application/json'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'content_type'} || 'application/json'
MC/DC0%0/1
$args{'content_type'} || 'application/json'
$args{'content_type'}
Truth table0%0/2
A
A= $args{'content_type'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $req->header('Content-Type' => $ct); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (ref $args{body}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $req->content(encode_json($args{body})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $req->content($args{body}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $log->debugf('PayPal %s %s', $method, $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $res = $self->ua->request($req); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $body = $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (length $body && $body =~ /\A\s*[\{\[]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $body and $body =~ /\A\s*[\{\[]/
MC/DC0%0/2
length $body and $body =~ /\A\s*[\{\[]/
length $body
$body =~ /\A\s*[\{\[]/
Truth table0%0/3
A and B
A= length $bodyB= $body =~ /\A\s*[\{\[]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $data = decode_json($body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | unless ($res->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | my $msg = ref $data eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | ? ($data->{message} || $data->{error_description} || $data->{error} || $res->status_line) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$data->{"message"} or $data->{"error_description"} or $data->{"error"} or $res->status_line
$data->{"message"} or $data->{"error_description"} or $data->{"error"}
$data->{"message"} or $data->{"error_description"}
MC/DC0%0/4
$data->{"message"} or $data->{"error_description"} or $data->{"error"} or $res->status_line
$data->{"message"}
$data->{"error_description"}
$data->{"error"}
$res->status_line
Truth table0%0/5
A or B or C or D
A= $data->{"message"}B= $data->{"error_description"}C= $data->{"error"}D= $res->status_line
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | : $res->status_line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $log->errorf('PayPal API error: %s', $msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | croak "PayPal API error ($method $path): $msg"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||