line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Easypost::Request; |
2
|
|
|
|
|
|
|
$Net::Easypost::Request::VERSION = '0.20'; |
3
|
6
|
|
|
6
|
|
48
|
use Moo; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
41
|
|
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
2006
|
use Carp qw(croak); |
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
266
|
|
6
|
6
|
|
|
6
|
|
3549
|
use HTTP::Tiny; |
|
6
|
|
|
|
|
254720
|
|
|
6
|
|
|
|
|
280
|
|
7
|
6
|
|
|
6
|
|
63
|
use JSON::MaybeXS qw(decode_json); |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
2560
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has 'user_agent' => ( |
10
|
|
|
|
|
|
|
is => 'ro', |
11
|
|
|
|
|
|
|
default => sub { HTTP::Tiny->new( agent => 'Net-Easypost ' ) }, |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'endpoint' => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
default => 'api.easypost.com/v2', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub post { |
20
|
53
|
|
|
53
|
1
|
802
|
my ($self, $operation, $params) = @_; |
21
|
|
|
|
|
|
|
|
22
|
53
|
|
|
|
|
334
|
my $http_response = $self->user_agent->post_form( |
23
|
|
|
|
|
|
|
$self->_build_url($operation), $params, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
53
|
50
|
|
|
|
36100178
|
unless ( $http_response->{success} ) { |
27
|
0
|
|
|
|
|
0
|
my ($err, $code) = map { $http_response->{$_} } qw(reason response); |
|
0
|
|
|
|
|
0
|
|
28
|
0
|
0
|
|
|
|
0
|
croak $code |
29
|
|
|
|
|
|
|
? "FATAL: " . $self->endpoint . $operation . " returned $code: '$err'" |
30
|
|
|
|
|
|
|
: "FATAL: " . $self->endpoint . $operation . " returned '$err'"; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
53
|
|
|
|
|
3350
|
return decode_json $http_response->{content}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get { |
37
|
11
|
|
|
11
|
1
|
43
|
my ($self, $endpoint) = @_; |
38
|
|
|
|
|
|
|
|
39
|
11
|
100
|
|
|
|
109
|
$endpoint = $self->_build_url($endpoint) |
40
|
|
|
|
|
|
|
unless $endpoint =~ m/https?/; |
41
|
|
|
|
|
|
|
|
42
|
11
|
|
|
|
|
256
|
my $http_response = $self->user_agent->get( $endpoint ); |
43
|
|
|
|
|
|
|
return lc $http_response->{headers}->{'content-type'} =~ m|^\Qapplication/json\E| |
44
|
|
|
|
|
|
|
? decode_json $http_response->{content} |
45
|
11
|
100
|
|
|
|
5006042
|
: $http_response->{content}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _build_url { |
49
|
62
|
|
|
62
|
|
223
|
my ($self, $operation) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return 'https://' . $ENV{EASYPOST_API_KEY} . ':@' . $self->endpoint . $operation |
52
|
62
|
50
|
|
|
|
793
|
if exists $ENV{EASYPOST_API_KEY}; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
croak 'Cannot find API key in access_code attribute of Net::Easypost' |
55
|
|
|
|
|
|
|
. ' or in an environment variable name EASYPOST_API_KEY'; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |