File Coverage

blib/lib/Business/Mondo/Client.pm
Criterion Covered Total %
statement 59 78 75.6
branch 2 18 11.1
condition 1 2 50.0
subroutine 19 20 95.0
pod 0 4 0.0
total 81 122 66.3


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 11     11   441248 use strict;
  11         17  
  11         235  
15 11     11   29 use warnings;
  11         11  
  11         193  
16              
17 11     11   3515 use Moo;
  11         71627  
  11         38  
18             with 'Business::Mondo::Utils';
19             with 'Business::Mondo::Version';
20              
21 11     11   12235 use Business::Mondo::Exception;
  11         91  
  11         371  
22 11     11   3863 use Business::Mondo::Transaction;
  11         55  
  11         342  
23 11     11   4309 use Business::Mondo::Account;
  11         28  
  11         343  
24              
25 11     11   54 use Data::Dumper;
  11         16  
  11         594  
26 11     11   5730 use Mojo::UserAgent;
  11         1109489  
  11         91  
27 11     11   375 use Mojo::JSON;
  11         16  
  11         348  
28 11     11   42 use Carp qw/ carp /;
  11         16  
  11         7349  
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         3 my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} );
72              
73             my $transaction = Business::Mondo::Transaction->new(
74             client => $self,
75 1         812 %{ $data->{transaction} },
  1         11  
76             );
77              
78 1         30 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   4 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         830 my $class = "Business::Mondo::" . ucfirst( $entity );
95 2         3 my @objects;
96              
97 2   50     2 foreach my $e ( @{ $data->{$plural} // [] } ) {
  2         8  
98 4         56 push( @objects,$class->new( client => $self,%{ $e } ) );
  4         77  
99             }
100              
101 2         65 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 333 my ( $self,$path,$params ) = @_;
122 2         5 return $self->_api_request( 'GET',$path,$params );
123             }
124              
125             sub api_post {
126 2     2 0 292 my ( $self,$path,$params ) = @_;
127 2         5 return $self->_api_request( 'POST',$path,$params );
128             }
129              
130             sub api_delete {
131 1     1 0 267 my ( $self,$path,$params ) = @_;
132 1         4 return $self->_api_request( 'DELETE',$path,$params );
133             }
134              
135             sub api_patch {
136 1     1 0 260 my ( $self,$path,$params ) = @_;
137 1         4 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 0         0 return $tx->res->json;
176             }
177             else {
178 0         0 my $error = $tx->error;
179              
180             carp( "ERROR: " . Dumper $error )
181 0 0       0 if $ENV{MONDO_DEBUG};
182              
183             Business::Mondo::Exception->throw({
184             message => $error->{message},
185             code => $error->{code},
186 0         0 response => $tx->res->body,
187             });
188             }
189             }
190              
191             sub _add_query_params {
192 4     4   5 my ( $self,$path,$params ) = @_;
193              
194 4 100       10 if ( my $query_params = $self->normalize_params( $params ) ) {
195 1         4 return "$path?$query_params";
196             }
197              
198 3         3 return $path;
199             }
200              
201             =head1 AUTHOR
202              
203             Lee Johnson - C<leejo@cpan.org>
204              
205             This library is free software; you can redistribute it and/or modify it under
206             the same terms as Perl itself. If you would like to contribute documentation,
207             features, bug fixes, or anything else then please raise an issue / pull request:
208              
209             https://github.com/leejo/business-mondo
210              
211             =cut
212              
213             1;
214              
215             # vim: ts=4:sw=4:et