Business-Monzo
view release on metacpan or search on metacpan
lib/Business/Monzo/Client.pm view on Meta::CPAN
sub _build_ua {
my $self = shift;
my $ua = Mojo::UserAgent->new;
$ua->transactor->name( $self->user_agent );
$ua->proxy->detect;
return $ua;
}
sub _get_transaction {
my ( $self,$params ) = @_;
my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} );
my $transaction = Business::Monzo::Transaction->new(
client => $self,
%{ $data->{transaction} },
);
return $transaction;
}
sub _get_transactions {
return shift->_get_entities( shift,'transaction' );
}
sub _get_accounts {
return shift->_get_entities( shift,'account' );
}
sub _get_pots {
return shift->_get_entities( shift,'pot','pots/listV1' );
}
sub _get_entities {
my ( $self,$params,$entity,$endpoint ) = @_;
my $plural = $entity . 's';
$endpoint //= $plural;
my $data = $self->_api_request( 'GET', $endpoint, $params );
my $class = "Business::Monzo::" . ucfirst( $entity );
my @objects;
foreach my $e ( @{ $data->{$plural} // [] } ) {
push( @objects,$class->new( client => $self,%{ $e } ) );
}
return @objects;
}
=head1 METHODS
api_get
api_post
api_delete
api_patch
Make a request to the Monzo API:
my $data = $Client->api_get( 'location',\%params );
May return a the decoded response data as a hash/array/string depending
on the reposonse type
=cut
sub api_get {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'GET',$path,$params );
}
sub api_post {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'POST',$path,$params );
}
sub api_delete {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'DELETE',$path,$params );
}
sub api_patch {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'PATCH',$path,$params );
}
sub _api_request {
my ( $self,$method,$path,$params ) = @_;
carp( "$method -> $path" )
if $ENV{MONZO_DEBUG};
$path = $self->_add_query_params( $path,$params )
if $method =~ /GET/;
my $tx;
$method = lc( $method );
$path = $path =~ /^http/ ? $path : join( '/',$self->api_url,$path );
carp( "PATH: $path" )
if $ENV{MONZO_DEBUG};
carp( "PARAMS: " . Dumper $params )
if $ENV{MONZO_DEBUG};
my %headers = (
'Authorization' => "Bearer " . $self->token,
'Accept' => 'application/json',
);
if ( $method =~ /POST|PUT|PATCH/i ) {
$tx = $self->_ua->$method( $path => { %headers } => form => $params );
} else {
$tx = $self->_ua->$method( $path => { %headers } );
}
my $res = $tx->result;
if ( $res->is_success ) {
carp( "RES: " . Dumper $res->json )
if $ENV{MONZO_DEBUG};
( run in 0.969 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )