Business-Mondo
view release on metacpan or search on metacpan
lib/Business/Mondo/Client.pm view on Meta::CPAN
default => sub {
return $ENV{MONDO_URL} || $Business::Mondo::API_URL;
},
);
has user_agent => (
is => 'ro',
default => sub {
# probably want more infoin here, version of perl, platform, and such
return "business-mondo/perl/v" . $Business::Mondo::VERSION;
}
);
sub _get_transaction {
my ( $self,$params ) = @_;
my $data = $self->_api_request( 'GET',"transactions/" . $params->{id} );
my $transaction = Business::Mondo::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_entities {
my ( $self,$params,$entity ) = @_;
my $plural = $entity . 's';
my $data = $self->_api_request( 'GET',$plural,$params );
my $class = "Business::Mondo::" . 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 Mondo 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{MONDO_DEBUG};
my $ua = Mojo::UserAgent->new;
$ua->transactor->name( $self->user_agent );
$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{MONDO_DEBUG};
carp( "PARAMS: " . Dumper $params )
if $ENV{MONDO_DEBUG};
my %headers = (
'Authorization' => "Bearer " . $self->token,
'Accept' => 'application/json',
);
if ( $method =~ /POST|PUT|PATCH/i ) {
$tx = $ua->$method( $path => { %headers } => form => $params );
} else {
$tx = $ua->$method( $path => { %headers } );
}
if ( $tx->success ) {
carp( "RES: " . Dumper $tx->res->json )
( run in 1.612 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )