File Coverage

blib/lib/Business/Monzo/Client.pm
Criterion Covered Total %
statement 64 82 78.0
branch 2 20 10.0
condition 1 2 50.0
subroutine 20 21 95.2
pod 0 4 0.0
total 87 129 67.4


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   882054 use strict;
  10         21  
  10         251  
15 10     10   52 use warnings;
  10         21  
  10         247  
16              
17 10     10   2405 use Moo;
  10         63138  
  10         46  
18             with 'Business::Monzo::Utils';
19             with 'Business::Monzo::Version';
20              
21 10     10   11628 use Business::Monzo::Exception;
  10         69  
  10         379  
22 10     10   3804 use Business::Monzo::Transaction;
  10         48  
  10         553  
23 10     10   3536 use Business::Monzo::Account;
  10         51  
  10         524  
24              
25 10     10   88 use Data::Dumper;
  10         25  
  10         841  
26 10     10   4566 use Mojo::UserAgent;
  10         1677675  
  10         114  
27 10     10   441 use Mojo::JSON;
  10         28  
  10         558  
28 10     10   62 use Carp qw/ carp /;
  10         23  
  10         8707  
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   10 my $self = shift;
77 1         20 my $ua = Mojo::UserAgent->new;
78 1         11 $ua->transactor->name( $self->user_agent );
79 1         33 $ua->proxy->detect;
80 1         93 return $ua;
81             }
82              
83             sub _get_transaction {
84 1     1   11 my ( $self,$params ) = @_;
85              
86 1         7 my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} );
87              
88             my $transaction = Business::Monzo::Transaction->new(
89             client => $self,
90 1         1170 %{ $data->{transaction} },
  1         14  
91             );
92              
93 1         48 return $transaction;
94             }
95              
96             sub _get_transactions {
97 1     1   6 return shift->_get_entities( shift,'transaction' );
98             }
99              
100             sub _get_accounts {
101 1     1   13 return shift->_get_entities( shift,'account' );
102             }
103              
104             sub _get_entities {
105 2     2   7 my ( $self,$params,$entity ) = @_;
106              
107 2         6 my $plural = $entity . 's';
108 2         8 my $data = $self->_api_request( 'GET',$plural,$params );
109 2         1490 my $class = "Business::Monzo::" . ucfirst( $entity );
110 2         4 my @objects;
111              
112 2   50     4 foreach my $e ( @{ $data->{$plural} // [] } ) {
  2         10  
113 4         91 push( @objects,$class->new( client => $self,%{ $e } ) );
  4         85  
114             }
115              
116 2         145 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 804 my ( $self,$path,$params ) = @_;
137 2         7 return $self->_api_request( 'GET',$path,$params );
138             }
139              
140             sub api_post {
141 2     2 0 516 my ( $self,$path,$params ) = @_;
142 2         8 return $self->_api_request( 'POST',$path,$params );
143             }
144              
145             sub api_delete {
146 1     1 0 449 my ( $self,$path,$params ) = @_;
147 1         5 return $self->_api_request( 'DELETE',$path,$params );
148             }
149              
150             sub api_patch {
151 1     1 0 735 my ( $self,$path,$params ) = @_;
152 1         5 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   11 my ( $self,$path,$params ) = @_;
208              
209 4 100       22 if ( my $query_params = $self->normalize_params( $params ) ) {
210 1         7 return "$path?$query_params";
211             }
212              
213 3         9 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