Poloniex-API

 view release on metacpan or  search on metacpan

lib/Poloniex/API.pm  view on Meta::CPAN

use utf8;

package Poloniex::API;

use Time::HiRes qw(time);
use English qw( -no_match_vars );
use strict;
use warnings;
use Digest::SHA qw(hmac_sha512_hex);
use HTTP::Request;

use constant {
    URL_PUBLIC_API     => "https://poloniex.com/public?command=%s",
    URL_TRADING_API    => "https://poloniex.com/tradingApi",
    DEBUG_API_POLONIEX => $ENV{DEBUG_API_POLONIEX} || 0,
};

BEGIN {
    eval { require LWP::UserAgent, 1; }
      || die('LWP::UserAgent package not found');

    eval { require JSON::XS, 1; }
      || die('JSON::XS package not found');
}

our $VERSION = '0.04';

# singleton and accessor
{
    my $lwp = LWP::UserAgent->new( keep_alive => 1 );
    sub _lwp_agent { return $lwp }

    my $json = JSON::XS->new();
    sub _json { return $json }
}

sub new {
    my ( $class, %options ) = @ARG;
    my $object;

    $object->{APIKey} = $options{APIKey} || undef;
    $object->{Secret} = $options{Secret} || undef;
    $object->{_json}  = _json;
    $object->{_agent} = _lwp_agent;

    return bless $object, $class;
}

sub api_trading {
    my ( $self, $method, $req ) = @ARG;
    $$req{nonce}   = time() =~ s/\.//r;
    $$req{command} = $method;

    my @post_data;
    for ( keys %{$req} ) {
        push @post_data, "$_=$$req{$_}";
    }

    my $param = join( '&', @post_data );
    my $sign = hmac_sha512_hex( $param, $self->{Secret} );
    my %header = (
        Key  => $self->{APIKey},
        Sign => $sign
    );
    my $http = HTTP::Request->new( 'POST', $self->URL_TRADING_API );

    $http->content_type('application/x-www-form-urlencoded');
    $http->header(%header);
    $http->content($param);
    my $respons = $self->{_agent}->request($http);

    $self->_checkResponse($respons);
}

sub api_public {
    my ( $self, $method, $req ) = @ARG;

    my @request;
    for my $value ( keys %{$req} ) {
        push @request, "$value=$$req{$value}";
    }

    my ( $params, $json );
    $params = sprintf "$method&%s", join( '&', @request )
      if (@request);

    my $respons = $self->{_agent}
      ->post( sprintf( URL_PUBLIC_API, ($params) ? $params : $method ) );

    $self->_checkResponse($respons);
}

sub _checkResponse {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.212 second using v1.00-cache-2.02-grep-82fe00e-cpan-eac11a1d038b )