blib/lib/WebService/Cryptopia.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 150 CC 16 SCAR 42.2
CC
16
Cov
0%
api_private
156.0
api_public
30.0
BEGIN
2.0
CRAP 68.3
1 package WebService::Cryptopia;
2 # ABSTRACT: Interface to Cryptopia
3 use JSON::MaybeXS;
4 use LWP::UserAgent;
5 use Log::Log4perl;
6 use Digest::MD5;
7 use MIME::Base64;
8 use URL::Encode qw/url_encode/;
9 use Moose;
10 use MooseX::Params::Validate;
11 use Try::Tiny;
12 use Digest::SHA qw/hmac_sha256/;
13 use YAML;
14 BEGIN { Log::Log4perl->easy_init() };
15 our $VERSION = 0.018;
16  
17 with "MooseX::Log::Log4perl";
18  
19 =head1 NAME
20  
21 WebService::Cryptopia
22  
23  
24 =head1 DESCRIPTION
25  
26 Query the Cryptopia API
27  
28 https://www.cryptopia.co.nz/Forum/Thread/255
29  
30 =head1 ATTRIBUTES
31  
32 =over 4
33  
34 =item user_agent
35  
36 Optional. A new LWP::UserAgent will be created for you if you don't already have one you'd like to reuse.
37  
38 =cut
39  
40 has 'user_agent' => (
41 is => 'ro',
42 isa => 'LWP::UserAgent',
43 required => 1,
44 lazy => 1,
45 builder => '_build_user_agent',
46 );
47  
48 =item api_key
49  
50 Required for private api
51  
52 =cut
53 has 'api_key' => (
54 is => 'ro',
55 isa => 'Str',
56 );
57  
58 =item api_secret
59  
60 Required for private api
61  
62 =cut
63 has 'api_secret' => (
64 is => 'ro',
65 isa => 'Str',
66 );
67  
68 =item base_url
69  
70 Optional. Default: https://www.cryptopia.co.nz/api/
71  
72 =cut
73 has 'base_url' => (
74 is => 'ro',
75 isa => 'Str',
76 required => 1,
77 default => 'https://www.cryptopia.co.nz/api/',
78 );
79  
80 sub _build_user_agent {
81 my $self = shift;
82 $self->log->debug( "Building useragent" );
83 my $ua = LWP::UserAgent->new(
84 keep_alive => 1
85 );
86 $ua->default_header('Accept' => 'application/json' );
87 return $ua;
88 }
89  
90 =back
91  
92 =head1 METHODS
93  
94 =over 4
95  
96 =item api_public
97  
98 Query the public API
99  
100 =cut
101  
102  
103 sub api_public {
104 my ( $self, %params ) = validated_hash(
105 \@_,
106 method => { isa => 'Str' },
107 parameters => { isa => 'ArrayRef', optional => 1 },
108 );
109  
110 my $url = $self->base_url . $params{method} .
111 ( $params{parameters} ? '/' . join( '/', @{ $params{parameters} } ) : '' );
112 $self->log->debug( "Getting: $url" );
113 my $response = $self->user_agent->get( $url );
114 $self->log->trace( Dump( $response ) ) if $self->log->is_trace;
115
116 if( ! $response->is_success ){
117 $self->log->error( "Failed ($url) with status: " . $response->status_line );
118 $self->log->logdie( "Response:\n" . $response->decoded_content );
119 }
120 my $data = decode_json( $response->decoded_content );
121 if( not $data->{Success} ){
122 $self->log->logdie( $data->{Error} );
123 }
124 return $data->{Data};
125 }
126  
127 sub api_private {
128 my ( $self, %params ) = validated_hash(
129 \@_,
130 method => { isa => 'Str' },
131 parameters => { isa => 'HashRef', optional => 1 },
132 nonce => { isa => 'Int', optional => 1 },
133 );
134 $params{parameters} = {} if( not $params{parameters} );
135 if( not $self->api_key or not $self->api_secret ){
136 $self->log->logdie( "Cannot use api_private without api_key and api_secret" );
137 }
138 my $url = $self->base_url . $params{method};
139 $self->log->trace( "Url: $url" ) if $self->log->is_trace;
140 my $nonce = $params{nonce} || time();
141 $self->log->trace( "nonce: $nonce" ) if $self->log->is_trace;
142 my $post_data = encode_json( $params{parameters} );
143 $self->log->trace( "Post data: $post_data" ) if $self->log->is_trace;
144 my $ctx = Digest::MD5->new();
145 $ctx->add( $post_data );
146 my $request_content_base64_string = encode_base64( $ctx->digest );
147 $self->log->trace( "Request_content_base64: $request_content_base64_string" ) if $self->log->is_trace;
148 my $signature = $self->api_key . "POST" . lc( url_encode( $url ) ) . $nonce . $request_content_base64_string;
149 chomp( $signature );
150 $self->log->trace( "Signature: $signature" ) if $self->log->is_trace;
151 my $hmac_signature = encode_base64( hmac_sha256( $signature, decode_base64( $self->api_secret ) ) );
152 chomp( $hmac_signature );
153 $self->log->trace( "HMAC signature: $hmac_signature" ) if $self->log->is_trace;
154 my $header_value = "amx " . $self->api_key . ':' . $hmac_signature . ':' . $nonce;
155 $self->log->trace( "Authorization: $header_value" ) if $self->log->is_trace;
156 my $request = HTTP::Request->new( 'POST' => $url );
157 $request->header( 'Authorization', $header_value );
158 $request->header( 'Content-Type', 'application/json; charset=utf-8' );
159 $request->content( $post_data );
160 $self->log->trace( "HTTP::Request: \n" . Dump( $request ) );
161 my $response = $self->user_agent->request( $request );
162 $self->log->trace( "Response-content: " . $response->decoded_content ) if $self->log->is_trace;
163 my $data = decode_json( $response->decoded_content );
164 if( not $data->{Success} ){
165 $self->log->logdie( $data->{Error} );
166 }
167 return $data->{Data};
168 }
169  
170  
171 1;
172  
173  
174 =back
175  
176 =head1 COPYRIGHT
177  
178 Copyright 2018, Robin Clarke, Germany
179  
180 =head1 AUTHOR
181  
182 Robin Clarke <perl@robinclarke.net>
183