| 1 | package Net::Easypost::Request; | ||||||||||
| 2 | 6 | $Net::Easypost::Request::VERSION = '0.23'; | |||||||||
| 3 | 6 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 6 | ▶ | use JSON::MaybeXS qw(decode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | has 'user_agent' => ( | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | 69 | ▶ | default => sub { HTTP::Tiny->new( agent => 'Net-Easypost ' ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 6 | ); | |||||||||
| 13 | |||||||||||
| 14 | 6 | has 'endpoint' => ( | |||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | default => 'api.easypost.com/v2', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 53 | ▶ | sub post { | ||||||||
|
Subroutine
post: called CC=3 SCAR=15.0
Pod
documented
| |||||||||||
| 20 | 53 | my ($self, $operation, $params) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 53 | my $http_response = $self->user_agent->post_form( | |||||||||
| 23 | $self->_build_url($operation), $params, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 53 | ▶ | unless ( $http_response->{success} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | my ($err, $code) = map { $http_response->{$_} } qw(reason response); | |||||||||
| 28 | 0 | ▶ | croak $code | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | ? "FATAL: " . $self->endpoint . $operation . " returned $code: '$err'" | ||||||||||
| 30 | : "FATAL: " . $self->endpoint . $operation . " returned '$err'"; | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 53 | return decode_json $http_response->{content}; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 11 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 37 | 11 | my ($self, $endpoint) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 11 | ▶ | $endpoint = $self->_build_url($endpoint) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | unless $endpoint =~ m/https?/; | ||||||||||
| 41 | |||||||||||
| 42 | 11 | 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 | ▶ | : $http_response->{content}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 62 | ▶ | sub _build_url { | ||||||||
|
Subroutine
_build_url: called CC=2 SCAR=8.1
| |||||||||||
| 49 | 62 | my ($self, $operation) = @_; | |||||||||
| 50 | |||||||||||
| 51 | return 'https://' . $ENV{EASYPOST_API_KEY} . ':@' . $self->endpoint . $operation | ||||||||||
| 52 | 62 | ▶ | if exists $ENV{EASYPOST_API_KEY}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 6 | 1; | |||||||||
| 59 | |||||||||||
| 60 | __END__ | ||||||||||