File Coverage

blib/lib/WWW/ShipStation.pm
Criterion Covered Total %
statement 23 104 22.1
branch 0 36 0.0
condition 0 11 0.0
subroutine 8 26 30.7
pod 14 17 82.3
total 45 194 23.2


line stmt bran cond sub pod time code
1             package WWW::ShipStation;
2              
3 1     1   22962 use strict;
  1         2  
  1         38  
4 1     1   30 use 5.008_005;
  1         5  
5             our $VERSION = '0.05';
6              
7 1     1   3093 use LWP::UserAgent;
  1         67245  
  1         32  
8 1     1   825 use JSON;
  1         28169  
  1         6  
9 1     1   211 use Carp 'croak';
  1         2  
  1         144  
10 1     1   8 use URI::Escape qw/uri_escape/;
  1         2  
  1         79  
11 1     1   6 use HTTP::Request;
  1         3  
  1         1482  
12              
13             sub new {
14 0     0 1   my $class = shift;
15 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
16              
17 0 0         $args{user} or croak "user is required.";
18 0 0         $args{pass} or croak "pass is required.";
19              
20 0   0       $args{ua} ||= LWP::UserAgent->new();
21 0   0       $args{json} ||= JSON->new->allow_nonref->utf8;
22              
23 0   0       $args{API_BASE} ||= 'https://ssapi.shipstation.com/';
24              
25 0           bless \%args, $class;
26             }
27              
28             sub getCarriers {
29 0     0 1   (shift)->request('carriers');
30             }
31              
32             sub getCustomer {
33 0     0 1   my ($self, $customerId) = @_;
34 0           $self->request("customers/$customerId");
35             }
36              
37             sub getCustomers {
38 0     0 1   my $self = shift;
39 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
40 0           $self->request('customers', %args);
41             }
42              
43             sub getOrder {
44 0     0 1   my ($self, $orderId) = @_;
45 0           $self->request("orders/$orderId");
46             }
47              
48             sub getOrders {
49 0     0 1   my $self = shift;
50 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
51 0           $self->request('orders', %args);
52             }
53              
54             sub getProduct {
55 0     0 0   my ($self, $productId) = @_;
56 0           $self->request("products/$productId");
57             }
58              
59             sub getProducts {
60 0     0 1   my $self = shift;
61 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
62 0           $self->request('products/', %args);
63             }
64              
65             sub getShipments {
66 0     0 1   my $self = shift;
67 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
68 0           $self->request('shipments', %args);
69             }
70              
71             sub getMarketplaces {
72 0     0 1   my $self = shift;
73 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
74 0           $self->request('stores/marketplaces', %args);
75             }
76              
77             sub getStores {
78 0     0 1   my $self = shift;
79 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
80 0           $self->request('stores', %args);
81             }
82              
83             sub getStore {
84 0     0 0   my ($self, $storeId) = @_;
85 0           $self->request("stores/$storeId");
86             }
87              
88             sub getWarehouses {
89 0     0 1   my $self = shift;
90 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
91 0           $self->request('warehouses', %args);
92             }
93              
94             sub getWarehouse {
95 0     0 0   my ($self, $warehouseId) = @_;
96 0           $self->request("warehouses/$warehouseId");
97             }
98              
99             sub request {
100 0     0 1   my ($self, $url, %params) = @_;
101              
102 0 0 0       if (%params and keys %params) {
103 0           $url .= '?' . join('&', map { join('=', $_, uri_escape($params{$_})) } keys %params);
  0            
104             }
105              
106 0           my $req = HTTP::Request->new(GET => $self->{API_BASE} . $url);
107 0           $req->authorization_basic($self->{user}, $self->{pass});
108 0           $req->header('Accept', 'application/json'); # JSON is better
109 0           my $res = $self->{ua}->request($req);
110 1     1   1072 use Data::Dumper; print STDERR Dumper(\$res);
  1         10374  
  1         1020  
  0            
111 0 0         if ($res->header('Content-Type') =~ m{application/json}) {
112 0           return $self->{json}->decode($res->decoded_content);
113             }
114 0 0         unless ($res->is_success) {
115             return {
116 0           'error' => {
117             'code' => '',
118             'message' => {
119             'lang' => 'en-US',
120             'value' => $res->status_line,
121             }
122             }
123             };
124             }
125             }
126              
127             sub createOrder {
128 0     0 1   my $self = shift;
129 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
130 0           $self->__request('POST', 'orders/createorder', $self->{json}->encode(\%args));
131             }
132              
133             sub deleteOrder {
134 0     0 1   my ($self, $orderID) = @_;
135              
136 0           $self->__request('DELETE', "orders/$orderID");
137             }
138              
139             sub __request {
140 0     0     my ($self, $method, $url, $content) = @_;
141              
142 0           my $req = HTTP::Request->new($method => $self->{API_BASE} . $url);
143 0           $req->authorization_basic($self->{user}, $self->{pass});
144 0           $req->header('Accept', 'application/json'); # JSON is better
145 0           $req->header('Accept-Charset' => 'UTF-8');
146 0 0         if ($method eq 'POST') {
147 0           $req->header('Content-Type' => 'application/json');
148             }
149 0 0         $req->content($content) if $content;
150              
151 0           my $res = $self->{ua}->request($req);
152             # use Data::Dumper; print STDERR Dumper(\$res);
153 0 0         if ($res->header('Content-Type') =~ m{application/json}) {
154 0           return $self->{json}->decode($res->decoded_content);
155             }
156 0 0         unless ($res->is_success) {
157             return {
158 0           'error' => {
159             'code' => '',
160             'message' => {
161             'lang' => 'en-US',
162             'value' => $res->status_line
163             }
164             }
165             };
166             }
167 0           return $res->decoded_content;
168             }
169              
170             1;
171             __END__