line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::Mondo::Client; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Business::Mondo::Client |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
This is a class for the lower level requests to the Mondo API. generally |
10
|
|
|
|
|
|
|
there is nothing you should be doing with this. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
671188
|
use strict; |
|
10
|
|
|
|
|
13
|
|
|
10
|
|
|
|
|
232
|
|
15
|
10
|
|
|
10
|
|
30
|
use warnings; |
|
10
|
|
|
|
|
8
|
|
|
10
|
|
|
|
|
174
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
3479
|
use Moo; |
|
10
|
|
|
|
|
65259
|
|
|
10
|
|
|
|
|
34
|
|
18
|
|
|
|
|
|
|
with 'Business::Mondo::Utils'; |
19
|
|
|
|
|
|
|
with 'Business::Mondo::Version'; |
20
|
|
|
|
|
|
|
|
21
|
10
|
|
|
10
|
|
11874
|
use Business::Mondo::Exception; |
|
10
|
|
|
|
|
74
|
|
|
10
|
|
|
|
|
263
|
|
22
|
10
|
|
|
10
|
|
3573
|
use Business::Mondo::Transaction; |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
375
|
|
23
|
10
|
|
|
10
|
|
3775
|
use Business::Mondo::Account; |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
332
|
|
24
|
|
|
|
|
|
|
|
25
|
10
|
|
|
10
|
|
55
|
use Data::Dumper; |
|
10
|
|
|
|
|
12
|
|
|
10
|
|
|
|
|
578
|
|
26
|
10
|
|
|
10
|
|
5272
|
use Mojo::UserAgent; |
|
10
|
|
|
|
|
1026622
|
|
|
10
|
|
|
|
|
90
|
|
27
|
10
|
|
|
10
|
|
340
|
use Mojo::JSON; |
|
10
|
|
|
|
|
14
|
|
|
10
|
|
|
|
|
407
|
|
28
|
10
|
|
|
10
|
|
41
|
use Carp qw/ carp /; |
|
10
|
|
|
|
|
14
|
|
|
10
|
|
|
|
|
6959
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 token |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Your Mondo access token, this is required |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 api_url |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
The Mondo url, which will default to https://api.getmondo.co.uk |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 user_agent |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The user agent string used in requests to the mondo API, defaults to |
43
|
|
|
|
|
|
|
business-mondo/perl/v . $version_of_this_library. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has [ qw/ token / ] => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
required => 1, |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
has api_url => ( |
53
|
|
|
|
|
|
|
is => 'ro', |
54
|
|
|
|
|
|
|
required => 0, |
55
|
|
|
|
|
|
|
default => sub { |
56
|
|
|
|
|
|
|
return $ENV{MONDO_URL} || $Business::Mondo::API_URL; |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has user_agent => ( |
61
|
|
|
|
|
|
|
is => 'ro', |
62
|
|
|
|
|
|
|
default => sub { |
63
|
|
|
|
|
|
|
# probably want more infoin here, version of perl, platform, and such |
64
|
|
|
|
|
|
|
return "business-mondo/perl/v" . $Business::Mondo::VERSION; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _get_transaction { |
69
|
1
|
|
|
1
|
|
6
|
my ( $self,$params ) = @_; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
4
|
my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} ); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $transaction = Business::Mondo::Transaction->new( |
74
|
|
|
|
|
|
|
client => $self, |
75
|
1
|
|
|
|
|
796
|
%{ $data->{transaction} }, |
|
1
|
|
|
|
|
11
|
|
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
25
|
return $transaction; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _get_transactions { |
82
|
1
|
|
|
1
|
|
4
|
return shift->_get_entities( shift,'transaction' ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _get_accounts { |
86
|
1
|
|
|
1
|
|
7
|
return shift->_get_entities( shift,'account' ); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _get_entities { |
90
|
2
|
|
|
2
|
|
3
|
my ( $self,$params,$entity ) = @_; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
3
|
my $plural = $entity . 's'; |
93
|
2
|
|
|
|
|
6
|
my $data = $self->_api_request( 'GET',$plural,$params ); |
94
|
2
|
|
|
|
|
834
|
my $class = "Business::Mondo::" . ucfirst( $entity ); |
95
|
2
|
|
|
|
|
3
|
my @objects; |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
50
|
|
|
2
|
foreach my $e ( @{ $data->{$plural} // [] } ) { |
|
2
|
|
|
|
|
6
|
|
98
|
4
|
|
|
|
|
52
|
push( @objects,$class->new( client => $self,%{ $e } ) ); |
|
4
|
|
|
|
|
76
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
62
|
return @objects; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 METHODS |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
api_get |
107
|
|
|
|
|
|
|
api_post |
108
|
|
|
|
|
|
|
api_delete |
109
|
|
|
|
|
|
|
api_patch |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Make a request to the Mondo API: |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
my $data = $Client->api_get( 'location',\%params ); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
May return a the decoded response data as a hash/array/string depending |
116
|
|
|
|
|
|
|
on the reposonse type |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub api_get { |
121
|
2
|
|
|
2
|
0
|
451
|
my ( $self,$path,$params ) = @_; |
122
|
2
|
|
|
|
|
5
|
return $self->_api_request( 'GET',$path,$params ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub api_post { |
126
|
2
|
|
|
2
|
0
|
513
|
my ( $self,$path,$params ) = @_; |
127
|
2
|
|
|
|
|
6
|
return $self->_api_request( 'POST',$path,$params ); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub api_delete { |
131
|
1
|
|
|
1
|
0
|
444
|
my ( $self,$path,$params ) = @_; |
132
|
1
|
|
|
|
|
3
|
return $self->_api_request( 'DELETE',$path,$params ); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub api_patch { |
136
|
1
|
|
|
1
|
0
|
433
|
my ( $self,$path,$params ) = @_; |
137
|
1
|
|
|
|
|
3
|
return $self->_api_request( 'PATCH',$path,$params ); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub _api_request { |
141
|
0
|
|
|
0
|
|
0
|
my ( $self,$method,$path,$params ) = @_; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
carp( "$method -> $path" ) |
144
|
0
|
0
|
|
|
|
0
|
if $ENV{MONDO_DEBUG}; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
0
|
my $ua = Mojo::UserAgent->new; |
147
|
0
|
|
|
|
|
0
|
$ua->transactor->name( $self->user_agent ); |
148
|
|
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
0
|
$path = $self->_add_query_params( $path,$params ) |
150
|
|
|
|
|
|
|
if $method =~ /GET/; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
my $tx; |
153
|
0
|
|
|
|
|
0
|
$method = lc( $method ); |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
0
|
$path = $path =~ /^http/ ? $path : join( '/',$self->api_url,$path ); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
carp( "PATH: $path" ) |
158
|
0
|
0
|
|
|
|
0
|
if $ENV{MONDO_DEBUG}; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
carp( "PARAMS: " . Dumper $params ) |
161
|
0
|
0
|
|
|
|
0
|
if $ENV{MONDO_DEBUG}; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
my %headers = ( |
164
|
|
|
|
|
|
|
'Authorization' => "Bearer " . $self->token, |
165
|
|
|
|
|
|
|
'Accept' => 'application/json', |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
0
|
if ( $method =~ /POST|PUT|PATCH/i ) { |
169
|
0
|
|
|
|
|
0
|
$tx = $ua->$method( $path => { %headers } => form => $params ); |
170
|
|
|
|
|
|
|
} else { |
171
|
0
|
|
|
|
|
0
|
$tx = $ua->$method( $path => { %headers } ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
|
|
|
0
|
if ( $tx->success ) { |
175
|
|
|
|
|
|
|
carp( "RES: " . Dumper $tx->res->json ) |
176
|
0
|
0
|
|
|
|
0
|
if $ENV{MONDO_DEBUG}; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
0
|
return $tx->res->json; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
else { |
181
|
0
|
|
|
|
|
0
|
my $error = $tx->error; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
carp( "ERROR: " . Dumper $error ) |
184
|
0
|
0
|
|
|
|
0
|
if $ENV{MONDO_DEBUG}; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Business::Mondo::Exception->throw({ |
187
|
|
|
|
|
|
|
message => $error->{message}, |
188
|
|
|
|
|
|
|
code => $error->{code}, |
189
|
0
|
|
|
|
|
0
|
response => $tx->res->body, |
190
|
|
|
|
|
|
|
}); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub _add_query_params { |
195
|
4
|
|
|
4
|
|
5
|
my ( $self,$path,$params ) = @_; |
196
|
|
|
|
|
|
|
|
197
|
4
|
100
|
|
|
|
12
|
if ( my $query_params = $self->normalize_params( $params ) ) { |
198
|
1
|
|
|
|
|
3
|
return "$path?$query_params"; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
3
|
|
|
|
|
4
|
return $path; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Lee Johnson - C<leejo@cpan.org> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under |
209
|
|
|
|
|
|
|
the same terms as Perl itself. If you would like to contribute documentation, |
210
|
|
|
|
|
|
|
features, bug fixes, or anything else then please raise an issue / pull request: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
https://github.com/leejo/business-mondo |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# vim: ts=4:sw=4:et |