Business-Monzo

 view release on metacpan or  search on metacpan

lib/Business/Monzo/Client.pm  view on Meta::CPAN

    required => 1,
);

has api_url => (
    is       => 'ro',
    required => 0,
    default  => sub {
        return $ENV{MONZO_URL} || $Business::Monzo::API_URL;
    },
);

has user_agent => (
    is      => 'ro',
    default => sub {
        # probably want more infoin here, version of perl, platform, and such
        require Business::Monzo;
        return "business-monzo/perl/v" . $Business::Monzo::VERSION;
    }
);

has _ua => (
    is => 'ro',
    lazy => 1,
    builder => '_build_ua',
);

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 );



( run in 1.055 second using v1.01-cache-2.11-cpan-e1769b4cff6 )