WebService-MyGengo

 view release on metacpan or  search on metacpan

lib/WebService/MyGengo/Client.pm  view on Meta::CPAN


=head1 DESCRIPTION

A perl library for accessing the MyGengo (L<http://mygengo.com>) API.

=head1 SYNOPSIS

    use WebService::MyGengo::Client;
    my $client = WebService::MyGengo::Client->new({
        public_key      => 'pubkey'
        , private_key   => 'privkey'
        , use_sandbox   => 1
        });

    # Alternative constructor syntax
    $client = WebService::MyGengo::Client->new('pubkey', 'privkey', $use_sandbox);

    # A WebService::MyGengo::Job
    my $job = $client->get_job( 123 );

    # Seeing what went wrong by inspecting the `last_response`

lib/WebService/MyGengo/Client.pm  view on Meta::CPAN

Your public API key.

=cut
has public_key => (
    is => 'rw'
    , isa => 'Str'
    , required => 1
    , trigger => sub { shift->clear_request_factory }
    );

=head2 private_key (Str)

Your private API key.

=cut
has private_key => (
    is => 'ro'
    , isa => 'Str'
    , required => 1
    , trigger => sub { shift->clear_request_factory }
    );

=head2 use_sandbox (Bool)

A boolean flag that determines whether to use the API sandbox or the live site.

lib/WebService/MyGengo/Client.pm  view on Meta::CPAN

    is => 'ro'
    , isa => 'WebService::MyGengo::RequestFactory'
    , lazy_build => 1
    , init_arg => undef
    );
sub _build_request_factory {
    my ( $self ) = ( shift );

    return WebService::MyGengo::RequestFactory->new({
        public_key      => $self->public_key
        , private_key   => $self->private_key
        , root_uri      => $self->root_uri
        });
}

=head2 last_response (L<WebService::MyGengo::Response>)

The last raw response object received from the API.

=cut
has last_response => (

lib/WebService/MyGengo/Client.pm  view on Meta::CPAN

#
#Support alternative construction syntax.
#
#=cut
around BUILDARGS => sub {
    my ( $orig, $class, $args ) = ( shift, shift, @_ );

    ref($args) eq 'HASH' and return $class->$orig(@_);

    my %args;
    @args{ qw/public_key private_key use_sandbox _user_agent_string/ }
        = @_;

    return \%args;
};

=head2 get_account( )

Returns the L<WebService::MyGengo::Account> associated with your API keys.

Calls L<get_account_stats> and L<get_account_balance> internally to gather

lib/WebService/MyGengo/RequestFactory.pm  view on Meta::CPAN


=head1 DESCRIPTION

Returns various L<WebService::MyGengo::Request> objects to be sent to the API.

=head1 SYNOPSIS

    # Note: Requests are usually created automatically by WebService::MyGengo::Client
    my $req_factory = new WebService::MyGengo::RequestFactory({
        public_key      => $pubkey
        , private_key   => $privkey
        , root_uri      => $api_uri
        });

    my $req = $req_factory->new_request( $method, $endpoint, \%params );

    # Alternate constructor syntax
    my $req_factory = new WebService::MyGengo::RequestFactory(
        $pubkey
        , $privkey
        , $api_uri

lib/WebService/MyGengo/RequestFactory.pm  view on Meta::CPAN


=head1 ATTRIBUTES

All attributes are read-only. If, for some reason, you need to generate
requests for a different keypair or root_uri, just make a new RequestFactory.

=head2 public_key (Str)

Your public API key.

=head2 private_key (Str)

Your private API key

=cut
has [qw/public_key private_key/] => (
    is          => 'ro'
    , isa       => 'Str'
    , required	=> 1
    );

=head2 root_uri (URI)

The URI to be used as the base for all API endpoints.

eg, 'http://api.sandbox.mygengo.com/v1.1'

lib/WebService/MyGengo/RequestFactory.pm  view on Meta::CPAN

#Allow arguments as a list or hashref.
#
#=cut
around BUILDARGS  => sub {
    my ( $orig, $class, $args ) = ( shift, shift, @_ );

    ref($args) eq 'HASH' and return $class->$orig(@_);

    return {
        public_key      => shift
        , private_key   => shift
        , root_uri      => shift->clone
        };
};

=head2 new_request( $request_method, $endpoint, \%params )

Returns an L<HTTP::Request> object for the given API endpoint.

=cut
sub new_request {

lib/WebService/MyGengo/RequestFactory.pm  view on Meta::CPAN

#=head2 _get_uri_and_req_params( $method, $endpoint )
#
#Returns an array of a URI object and a hashref of request parameters
#common to all API requests.
#
#=cut
sub _get_uri_and_req_params {
    my ( $self, $method, $endpoint ) = ( shift, @_ );

    my $pubkey      = $self->public_key;
    my $privkey     = $self->private_key;
    my $uri         = $self->root_uri->clone;

    $uri->path( $uri->path . $endpoint );

    my $time = time();
    my $hmac = Digest::HMAC->new($privkey, "Digest::SHA1");
    $hmac->add($time);

    my $req_params = {
        'api_sig'   => $hmac->hexdigest

t/lib/WebService/MyGengo/Test/Mock/LWP.pm  view on Meta::CPAN


Also see L<WebService::MyGengo::Test::Util::Client> in the t/lib directory.

    # t/001-blah.t
    use WebService::MyGengo::Client;
    use WebService::MyGengo::Test::Mock::LWP;

    # Your normal testing configuration
    my $config = {
        public_key      => 'real-pubkey"
        , private_key   => 'real-privkey'
        , use_sandbox   => 1
        };

    my $client = WebService::MyGengo::Client->new( $config );
    my $ua = WebService::MyGengo::Test::Mock::LWP->new();
    $client->_set_user_agent( $ua ); # For testing purposes only

    # A mocked WebService::MyGengo::Account object
    my $acct = $client->get_account();

t/lib/WebService/MyGengo/Test/Util/Client.pm  view on Meta::CPAN

WebService::MyGengo::Test::Util::Client - Basic access to the WebService::MyGengo::Client with simple mocking

=head1 SYNOPSIS

    # t/some-test.t
    use WebService::MyGengo::Test::Util:Client;:

    # use_sandbox = 0 ~ Production API access. Client will die if you use this.
    # use_sandbox = 1 ~ Sandbox API access.
    # use_sandbox = 2 ~ No API access at all (uses a mocked LWP::UserAgent)
    my %config = { public_key => 'pub', private_key => 'priv', use_sandbox => 2 };
    my $m = client( \%config );
    my $job = $m->getTranslationJob( $id ); # A mock Job

    # You can use the live version as well, just be careful of throttling
    $config{use_sandbox} = 1;
    my $m = client( \%config );
    my $job = $m->getTranslationJob( $id ); # A mock Job

=head1 METHODS

t/lib/WebService/MyGengo/Test/Util/Config.pm  view on Meta::CPAN


=head1 NAME

WebService::MyGengo::Test::Util::Config - Basic API configuration

=head1 SYNOPSIS

    # t/some-test.t
    use WebService::MyGengo::Test::Util:Config;:

    # $config == { public_key => 'pubkey', private_key => 'privkey', use_sandbox => 1 };
    my $config = config();

=head1 DESCRIPTION

=over

=item public_key - Your API public key

=item private_key - Your API private key

=item use_sandbox - 0 = production, 1 = sandbox, 2 = mock

* Defaults to 2

* See L<WebService::MyGengo::Test::Util::Client>

=back

=cut
sub config {
    return {
        public_key      => 'pubkey'
        , private_key   => 'privkey'
        , use_sandbox   => $ENV{WS_MYGENGO_USE_SANDBOX} // 2
        };
}


1;

=head1 TODO

Read config data from a nice, happy config file somewhere sane.



( run in 0.351 second using v1.01-cache-2.11-cpan-a5abf4f5562 )