File Coverage

blib/lib/Net/PaccoFacile.pm
Criterion Covered Total %
statement 23 49 46.9
branch 0 18 0.0
condition 0 3 0.0
subroutine 8 12 66.6
pod 0 2 0.0
total 31 84 36.9


line stmt bran cond sub pod time code
1             package Net::PaccoFacile {
2 1     1   116612 use Moo;
  1         6692  
  1         4  
3 1     1   1867 use Mojo::UserAgent;
  1         689971  
  1         12  
4 1     1   87 use Carp qw/croak confess/;
  1         2  
  1         78  
5 1     1   7 use List::Util qw/first/;
  1         3  
  1         73  
6 1     1   23 use Mojo::Util qw/url_escape url_unescape/;
  1         3  
  1         95  
7 1     1   689 use namespace::clean;
  1         22098  
  1         8  
8 1     1   939 use version;
  1         2462  
  1         7  
9 1     1   115 use v5.36;
  1         12  
10              
11             our $VERSION = qv("v0.1.0");
12              
13             has endpoint_uri => ( is => 'ro', lazy => 1, default => sub {
14             $_[0]->mode eq 'sandbox' ? $_[0]->endpoint_uri_sandbox : $_[0]->endpoint_uri_live
15             } );
16             has endpoint_uri_sandbox => ( is => 'ro', default => sub { 'https://paccofacile.tecnosogima.cloud/sandbox/v1/service/' } );
17             has endpoint_uri_live => ( is => 'ro', default => sub { 'https://paccofacile.tecnosogima.cloud/live/v1/service/' } );
18             has mode => ( is => 'ro' );
19             has token => ( is => 'ro' );
20             has api_key => ( is => 'ro' );
21             has account_number => ( is => 'ro' );
22             has request_timeout => ( is => 'ro', default => sub { 10 } );
23             has connect_timeout => ( is => 'ro', default => sub { 7 } );
24             has ua => ( is => 'ro', lazy => 1, default => sub {
25             Mojo::UserAgent->new()->connect_timeout($_[0]->connect_timeout)->inactivity_timeout($_[0]->request_timeout)
26             } );
27              
28             sub BUILD {
29 0     0 0   my ($self, $args) = @_;
30              
31 0 0         croak 'Please provide token' if !exists $args->{token};
32 0 0         croak 'Please provide api_key' if !exists $args->{api_key};
33 0 0         croak 'Please provide account_number' if !exists $args->{account_number};
34             croak 'Please provide mode (sandbox or live)'
35 0 0 0       if $args->{mode} ne 'sandbox' && $args->{mode} ne 'live';
36             }
37              
38 0     0 0   sub request($self, $path, $method, $args = {}) {
  0            
  0            
  0            
  0            
  0            
39 0 0         croak 'Please provide path' if !defined $path;
40 0 0         croak 'Invalid path' if $path !~ m/\w+/xs;
41 0           $method = $self->_validate_method($method);
42              
43 0           my $reqargs = {
44             %$args,
45             };
46              
47 0 0         my $datatransport = $method eq 'get' ? 'form' : 'json';
48              
49             # die $self->endpoint_uri . "$path";
50             # use Data::Dump qw/dump/; die dump($reqargs);
51 0           my $res = $self->ua->$method( $self->endpoint_uri . "$path" =>
52             {
53             Authorization => 'Bearer ' . $self->token,
54             'Account-Number' => $self->account_number,
55             'api-key' => $self->api_key,
56             },
57             $datatransport => $reqargs
58             )->result;
59 0 0         croak $res->message .': ' . $res->body if !$res->is_success;
60              
61 0           return $res->json;
62             }
63              
64 0     0     sub _validate_method($self, $method) {
  0            
  0            
  0            
65 0 0   0     confess 'Invalid-method' if !defined first { $_ eq uc($method) } qw/GET POST PUT DELETE/;
  0            
66 0           return lc $method;
67             }
68             }
69              
70             1;
71              
72             =head1 NAME
73              
74             Net::PaccoFacile - Perl library with MINIMAL interface to use PaccoFacile API.
75              
76             =head1 SYNOPSIS
77              
78             use Net::PaccoFacile;
79             use Data::Dump qw/dump/;
80              
81             my $pf = Net::PaccoFacile->new(
82             mode => 'live',
83             token => 'xxxx',
84             api_key => 'yyy',
85             account_number => '01234',
86             );
87              
88             my $res;
89              
90             $res = $pf->request('carriers', 'get');
91             say dump($res);
92              
93             $res = $pf->request('address-book', 'get');
94             say dump($res);
95              
96             $res = $pf->request('shipment/quote', 'post', {
97             "shipment_service" => {
98             "parcels" => [{
99             "shipment_type" => 1,
100             "dim1" => 10,
101             "dim2" => 11,
102             "dim3" => 12,
103             "weight" => 2
104             }],
105             "accessories" => [],
106             "package_content_type" => "GOODS"
107             },
108             "pickup" => {
109             "iso_code" => "IT",
110             "postal_code" => "04011",
111             "city" => "Aprilia",
112             "StateOrProvinceCode" => "LT"
113             },
114             "destination" => {
115             "iso_code" => "IT",
116             "postal_code" => "00135",
117             "city" => "Roma",
118             "StateOrProvinceCode" => "RM"
119             },
120             });
121              
122             say dump($res);
123              
124             =head1 DESCRIPTION
125              
126             This is HIGHLY EXPERIMENTAL and in the works, do not use for now.
127              
128             =head1 AUTHOR
129              
130             Michele Beltrame, C
131              
132             =head1 LICENSE
133              
134             This library is free software under the Artistic License 2.0.
135              
136             =cut