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