<commit_before>package Mojocoin::BitcoinClient;

use v5.14.0;

use Moose;
use namespace::autoclean;

use AnyEvent::JSONRPC::HTTP::Client;
use AnyEventX::CondVar;
use AnyEventX::CondVar::Util qw( :all );
use Carp;

use version; our $VERSION = version->declare("v0.0.1"); 

=pod

=head1 Mojocoin::BitcoinClient - Bitcoin client for making JSONRPC calls to bitcoind.

=cut

has _jsonrpc_client => (
    is => 'ro',
    isa => 'AnyEvent::JSONRPC::HTTP::Client',
);

sub BUILDARGS {
    my $self = shift;
    my %args = (
        url => 'http://127.0.0.1:18332',
        username => '',
        password => '',
        @_
    );

    +{ 
        _jsonrpc_client => AnyEvent::JSONRPC::HTTP::Client->new( 
            url => $args{ url },
            username => $args{ username },
            password => $args{ password },
        )
    };
}

# Valid method calls are defined in the bitcoind API call list
# Valid calls are case-agnostic and ignore underscores:
# $self->getnewaddres(...)
# $self->GetNewAddress(...)
# $self->get_new_address(...)
# are all valid and equivalent calls 
sub AUTOLOAD {
    my ( $self, @args ) = @_;
    my ( $method ) = our $AUTOLOAD =~ m/::(\w+)$/;
    $method =~ s/_//g;
    cv_build { 
        $self->_jsonrpc_client->call( lc( $method ), @args )
    };
}

__PACKAGE__->meta->make_immutable;

1;<commit_msg>Handling errors from JSON RPC<commit_after>package Mojocoin::BitcoinClient;

use v5.14.0;

use Moose;
use namespace::autoclean;

use AnyEvent::JSONRPC::HTTP::Client;
use AnyEventX::CondVar;
use AnyEventX::CondVar::Util qw( :all );
use Carp;

use version; our $VERSION = version->declare("v0.0.1"); 

=pod

=head1 Mojocoin::BitcoinClient - Bitcoin client for making JSONRPC calls to bitcoind.

=cut

has _jsonrpc_client => (
    is => 'ro',
    isa => 'AnyEvent::JSONRPC::HTTP::Client',
);

sub BUILDARGS {
    my $self = shift;
    my %args = (
        url => 'http://127.0.0.1:18332',
        username => '',
        password => '',
        @_
    );

    +{ 
        _jsonrpc_client => AnyEvent::JSONRPC::HTTP::Client->new( 
            url => $args{ url },
            username => $args{ username },
            password => $args{ password },
        )
    };
}

# Valid method calls are defined in the bitcoind API call list
# Valid calls are case-agnostic and ignore underscores:
# $self->getnewaddres(...)
# $self->GetNewAddress(...)
# $self->get_new_address(...)
# are all valid and equivalent calls 
sub AUTOLOAD {
    my ( $self, @args ) = @_;
    my ( $method ) = our $AUTOLOAD =~ m/::(\w+)$/;
    $method =~ s/_//g;
    my $cv = AnyEventX::CondVar->new;
    $self->_jsonrpc_client->call( lc( $method ), @args )->cb( sub {
        my @res; eval { @res = shift->recv };
        $cv->send( $@ ? undef : @res );
    });
    $cv;
}

__PACKAGE__->meta->make_immutable;

1;
{'fraction_non_alphanumeric': 0.1724137931034483, 'fraction_numerical': 0.014219694276573054, 'mean_word_length': 2.752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242035', 'n_tokens_mistral': 1010, 'n_tokens_neox': 989, 'n_words': 234}