| 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 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 11 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 11 | with 'Business::Monzo::Utils'; | |||||||||||||||||||
| 19 | 11 | with 'Business::Monzo::Version'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 11 | ▶ | use Business::Monzo::Exception; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 11 | ▶ | use Business::Monzo::Transaction; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 11 | ▶ | use Business::Monzo::Account; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 11 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 11 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 11 | ▶ | use Mojo::JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 11 | ▶ | use Carp qw/ carp /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 11 | has [ qw/ token / ] => ( | |||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||
| 49 | required => 1, | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | has api_url => ( | ||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||
| 54 | required => 0, | ||||||||||||||||||||
| 55 | 13 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||
| 56 | 13 | ▶ | return $ENV{MONZO_URL} || $Business::Monzo::API_URL; | ||||||||||||||||||
|
Condition33%1/3
$ENV{'MONZO_URL'} || $Business::Monzo::API_URL
MC/DC0%0/2
$ENV{'MONZO_URL'} || $Business::Monzo::API_URL
$ENV{'MONZO_URL'}
$Business::Monzo::API_URL
Truth table33%1/3
A || B
A= $ENV{'MONZO_URL'}B= $Business::Monzo::API_URL
| |||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||
| 58 | 11 | ); | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | has user_agent => ( | ||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||
| 62 | 15 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | # probably want more infoin here, version of perl, platform, and such | ||||||||||||||||||||
| 64 | 15 | require Business::Monzo; | |||||||||||||||||||
| 65 | 15 | return "business-monzo/perl/v" . $Business::Monzo::VERSION; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 11 | ); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 11 | has _ua => ( | |||||||||||||||||||
| 70 | is => 'ro', | ||||||||||||||||||||
| 71 | lazy => 1, | ||||||||||||||||||||
| 72 | builder => '_build_ua', | ||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | sub _build_ua { | ||||||||||||||||||
|
Subroutine
_build_ua: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | 1 | my $self = shift; | |||||||||||||||||||
| 77 | 1 | my $ua = Mojo::UserAgent->new; | |||||||||||||||||||
| 78 | 1 | $ua->transactor->name( $self->user_agent ); | |||||||||||||||||||
| 79 | 1 | $ua->proxy->detect; | |||||||||||||||||||
| 80 | 1 | return $ua; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 1 | ▶ | sub _get_transaction { | ||||||||||||||||||
|
Subroutine
_get_transaction: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | 1 | my ( $self,$params ) = @_; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 1 | my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} ); | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | my $transaction = Business::Monzo::Transaction->new( | ||||||||||||||||||||
| 89 | client => $self, | ||||||||||||||||||||
| 90 | 1 | %{ $data->{transaction} }, | |||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 1 | return $transaction; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | ▶ | sub _get_transactions { | ||||||||||||||||||
|
Subroutine
_get_transactions: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | 1 | return shift->_get_entities( shift,'transaction' ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | ▶ | sub _get_accounts { | ||||||||||||||||||
|
Subroutine
_get_accounts: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 101 | 1 | return shift->_get_entities( shift,'account' ); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | sub _get_pots { | ||||||||||||||||||
|
Subroutine
_get_pots: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 105 | 0 | return shift->_get_entities( shift,'pot','pots/listV1' ); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | sub _get_entities { | ||||||||||||||||||
|
Subroutine
_get_entities: called CC=4 SCAR=14.1
| |||||||||||||||||||||
| 109 | 2 | my ( $self,$params,$entity,$endpoint ) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 2 | my $plural = $entity . 's'; | |||||||||||||||||||
| 112 | 2 | ▶ | $endpoint //= $plural; | ||||||||||||||||||
|
Condition33%1/3
$endpoint //= $plural
MC/DC0%0/2
$endpoint //= $plural
$endpoint
$plural
Truth table33%1/3
A //= B
A= $endpointB= $plural
| |||||||||||||||||||||
| 113 | 2 | my $data = $self->_api_request( 'GET', $endpoint, $params ); | |||||||||||||||||||
| 114 | 2 | my $class = "Business::Monzo::" . ucfirst( $entity ); | |||||||||||||||||||
| 115 | 2 | my @objects; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 2 | ▶ | foreach my $e ( @{ $data->{$plural} // [] } ) { | ||||||||||||||||||
|
Condition50%1/2
$data->{$plural} // []
MC/DC0%0/1
$data->{$plural} // []
$data->{$plural}
Truth table50%1/2
A
A= $data->{$plural}
| |||||||||||||||||||||
| 118 | 4 | push( @objects,$class->new( client => $self,%{ $e } ) ); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 2 | return @objects; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 METHODS | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | api_get | ||||||||||||||||||||
| 127 | api_post | ||||||||||||||||||||
| 128 | api_delete | ||||||||||||||||||||
| 129 | api_patch | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Make a request to the Monzo API: | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | my $data = $Client->api_get( 'location',\%params ); | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | May return a the decoded response data as a hash/array/string depending | ||||||||||||||||||||
| 136 | on the reposonse type | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 2 | ▶ | sub api_get { | ||||||||||||||||||
|
Subroutine
api_get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 141 | 2 | my ( $self,$path,$params ) = @_; | |||||||||||||||||||
| 142 | 2 | return $self->_api_request( 'GET',$path,$params ); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 2 | ▶ | sub api_post { | ||||||||||||||||||
|
Subroutine
api_post: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 146 | 2 | my ( $self,$path,$params ) = @_; | |||||||||||||||||||
| 147 | 2 | return $self->_api_request( 'POST',$path,$params ); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 1 | ▶ | sub api_delete { | ||||||||||||||||||
|
Subroutine
api_delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 151 | 1 | my ( $self,$path,$params ) = @_; | |||||||||||||||||||
| 152 | 1 | return $self->_api_request( 'DELETE',$path,$params ); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 1 | ▶ | sub api_patch { | ||||||||||||||||||
|
Subroutine
api_patch: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 156 | 1 | my ( $self,$path,$params ) = @_; | |||||||||||||||||||
| 157 | 1 | return $self->_api_request( 'PATCH',$path,$params ); | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 5 | ▶ | sub _api_request { | ||||||||||||||||||
|
Subroutine
_api_request: called CC=10 SCAR=25.4
| |||||||||||||||||||||
| 161 | 5 | my ( $self,$method,$path,$params ) = @_; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | carp( "$method -> $path" ) | ||||||||||||||||||||
| 164 | 5 | ▶ | if $ENV{MONZO_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 5 | ▶ | $path = $self->_add_query_params( $path,$params ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | if $method =~ /GET/; | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 5 | my $tx; | |||||||||||||||||||
| 170 | 5 | $method = lc( $method ); | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 5 | ▶ | $path = $path =~ /^http/ ? $path : join( '/',$self->api_url,$path ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | carp( "PATH: $path" ) | ||||||||||||||||||||
| 175 | 5 | ▶ | if $ENV{MONZO_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | carp( "PARAMS: " . Dumper $params ) | ||||||||||||||||||||
| 178 | 5 | ▶ | if $ENV{MONZO_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 5 | my %headers = ( | |||||||||||||||||||
| 181 | 'Authorization' => "Bearer " . $self->token, | ||||||||||||||||||||
| 182 | 'Accept' => 'application/json', | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 5 | ▶ | if ( $method =~ /POST|PUT|PATCH/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | 1 | $tx = $self->_ua->$method( $path => { %headers } => form => $params ); | |||||||||||||||||||
| 187 | } else { | ||||||||||||||||||||
| 188 | 4 | $tx = $self->_ua->$method( $path => { %headers } ); | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 5 | my $res = $tx->result; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 5 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | carp( "RES: " . Dumper $res->json ) | ||||||||||||||||||||
| 195 | 5 | ▶ | if $ENV{MONZO_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 5 | return $res->json; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||
| 200 | 0 | my $error = $res->message; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | carp( "ERROR: " . Dumper $error ) | ||||||||||||||||||||
| 203 | 0 | ▶ | if $ENV{MONZO_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 0 | Business::Monzo::Exception->throw({ | |||||||||||||||||||
| 206 | message => $error, | ||||||||||||||||||||
| 207 | code => $res->code, | ||||||||||||||||||||
| 208 | response => $error, | ||||||||||||||||||||
| 209 | }); | ||||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 4 | ▶ | sub _add_query_params { | ||||||||||||||||||
|
Subroutine
_add_query_params: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 214 | 4 | my ( $self,$path,$params ) = @_; | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 4 | ▶ | if ( my $query_params = $self->normalize_params( $params ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | 1 | return "$path?$query_params"; | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 3 | return $path; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =head1 AUTHOR | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | Lee Johnson - C<leejo@cpan.org> | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 228 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||||||||||||
| 229 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | https://github.com/leejo/business-monzo | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 11 | 1; | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | # vim: ts=4:sw=4:et | ||||||||||||||||||||