| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Easypost::Request; | 
| 2 |  |  |  |  |  |  | $Net::Easypost::Request::VERSION = '0.23'; | 
| 3 | 6 |  |  | 6 |  | 34 | use Moo; | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 40 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 6 |  |  | 6 |  | 1522 | use Carp qw(croak); | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 247 |  | 
| 6 | 6 |  |  | 6 |  | 3375 | use HTTP::Tiny; | 
|  | 6 |  |  |  |  | 241619 |  | 
|  | 6 |  |  |  |  | 224 |  | 
| 7 | 6 |  |  | 6 |  | 42 | use JSON::MaybeXS qw(decode_json); | 
|  | 6 |  |  |  |  | 11 |  | 
|  | 6 |  |  |  |  | 2206 |  | 
| 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 | 682 | my ($self, $operation, $params) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 53 |  |  |  |  | 326 | my $http_response = $self->user_agent->post_form( | 
| 23 |  |  |  |  |  |  | $self->_build_url($operation), $params, | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 53 | 50 |  |  |  | 37423248 | 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 |  |  |  |  | 3583 | return decode_json $http_response->{content}; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub get { | 
| 37 | 11 |  |  | 11 | 1 | 49 | my ($self, $endpoint) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 11 | 100 |  |  |  | 92 | $endpoint = $self->_build_url($endpoint) | 
| 40 |  |  |  |  |  |  | unless $endpoint =~ m/https?/; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 11 |  |  |  |  | 237 | 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 |  |  |  | 4012355 | : $http_response->{content}; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub _build_url { | 
| 49 | 62 |  |  | 62 |  | 193 | my ($self, $operation) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | return 'https://' . $ENV{EASYPOST_API_KEY} . ':@' . $self->endpoint . $operation | 
| 52 | 62 | 50 |  |  |  | 667 | 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__ |