line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Easypost::Request; |
2
|
|
|
|
|
|
|
$Net::Easypost::Request::VERSION = '0.21'; |
3
|
6
|
|
|
6
|
|
35
|
use Moo; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
68
|
|
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
1773
|
use Carp qw(croak); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
271
|
|
6
|
6
|
|
|
6
|
|
2883
|
use HTTP::Tiny; |
|
6
|
|
|
|
|
256320
|
|
|
6
|
|
|
|
|
303
|
|
7
|
6
|
|
|
6
|
|
61
|
use JSON::MaybeXS qw(decode_json); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
2373
|
|
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
|
1030
|
my ($self, $operation, $params) = @_; |
21
|
|
|
|
|
|
|
|
22
|
53
|
|
|
|
|
410
|
my $http_response = $self->user_agent->post_form( |
23
|
|
|
|
|
|
|
$self->_build_url($operation), $params, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
53
|
50
|
|
|
|
36507489
|
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
|
|
|
|
|
4112
|
return decode_json $http_response->{content}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get { |
37
|
11
|
|
|
11
|
1
|
96
|
my ($self, $endpoint) = @_; |
38
|
|
|
|
|
|
|
|
39
|
11
|
100
|
|
|
|
106
|
$endpoint = $self->_build_url($endpoint) |
40
|
|
|
|
|
|
|
unless $endpoint =~ m/https?/; |
41
|
|
|
|
|
|
|
|
42
|
11
|
|
|
|
|
343
|
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
|
|
|
|
4993277
|
: $http_response->{content}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _build_url { |
49
|
62
|
|
|
62
|
|
226
|
my ($self, $operation) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return 'https://' . $ENV{EASYPOST_API_KEY} . ':@' . $self->endpoint . $operation |
52
|
62
|
50
|
|
|
|
895
|
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__ |