API/Request.pm

filter: Statementstmt 80.1% 89 / 111 Branchbran 72.2% 39 / 54 Conditioncond 83.3% 20 / 24 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 81.2% 13 / 16 Podpod 100% 4 / 4 totaltotal 77.1% 172 / 223 SCAR 39.7
CC
38
Cov
78%
_exec
42.0
_init
33.8
post
2.0
CRAP 52.7
« API.pm ↑ Summary API/Logger.pm »
1 package Binance::API::Request;
2  
3 # MIT License
4 #
5 # Copyright (c) 2017 Lari Taskula <lari@taskula.fi>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in all
15 # copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24  
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 ▶use base 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use Digest::SHA qw( hmac_sha256_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 3 ▶use Binance::Constants qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶use Binance::Exception::Parameter::Required;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head1 NAME
41  
42 Binance::API::Request -- LWP::UserAgent wrapper for L<Binance::API>
43  
44 =head1 DESCRIPTION
45  
46 This module provides a wrapper for LWP::UserAgent. Generates required parameters
47 for Binance API requests.
48  
49 =cut
50  
51 sub new {
52 5 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 5 my %params = @_;
54  
55 my $self = {
56 apiKey => $params{'apiKey'},
57 secretKey => $params{'secretKey'},
58 recvWindow => $params{'recvWindow'},
59 5 logger => $params{'logger'},
60 };
61  
62 5 bless $self, $class;
63 }
64  
65 sub get {
66 9 ▶ my ($self, $url, $params) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
67  
68 9 my ($path, %data) = $self->_init($url, $params);
69 9 return $self->_exec('get', $path, %data);
70 }
71  
72 sub post {
73 0 ▶ my ($self, $url, $params) = @_;
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
74  
75 0 my ($path, %data) = $self->_init($url, $params);
76 0 return $self->_exec('post', $path, %data);
77 }
78  
79 sub delete {
80 0 ▶ my ($self, $url, $params) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
81  
82 0 my ($path, %data) = $self->_init($url, $params);
83 0 return $self->_exec('delete', $path, %data);
84 }
85  
86 sub _exec {
87 0 ▶ my ($self, $method, $url, %data) = @_;
Subroutine _exec: not called CC=6 SCAR=37.4
88  
89 0 $self->{logger}->debug("New request: $url");
90 0 $method = "SUPER::$method";
91 0 my $response;
92 0 ▶ if (keys %data > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %data > 0) { } 0 0 0
93 0 $response = $self->$method($url, %data);
94 } else {
95 0 $response = $self->$method($url);
96 }
97 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
98 0 $response = eval { decode_json($response->decoded_content); };
99 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
100 $self->{logger}->error(
101 0 ▶ "Error decoding response. \nStatus => " . $response->code . ",\n"
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
102 . 'Content => ' . ($response->content ? $response->content : '')
103 );
104 }
105 } else {
106 $self->{logger}->error(
107 0 ▶ "Unsuccessful request. \nStatus => " . $response->code . ",\n"
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
108 . 'Content => ' . ($response->content ? $response->content : '')
109 );
110 }
111 0 return $response;
112 }
113  
114 sub _init {
115 18 ▶ my ($self, $path, $params) = @_;
Subroutine _init: called CC=33 SCAR=35.2
116  
117 18 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 18 18
118 0 Binance::Exception::Parameter::Required->throw(
119 error => 'Parameter "path" required',
120 parameters => ['path']
121 );
122 }
123  
124 18 my $timestamp = $params->{'timestamp'};
125 18 delete $params->{'timestamp'};
126 # Delete undefined query parameters
127 18 my $query = $params->{'query'};
128 18 foreach my $param (keys %$query) {
129 31 ▶ delete $query->{$param} unless defined $query->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query->{$param} 10 21 31
130 }
131  
132 # Delete undefined body parameters
133 18 my $body = $params->{'body'};
134 18 foreach my $param (keys %$body) {
135 14 ▶ delete $body->{$param} unless defined $body->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body->{$param} 0 14 14
136 }
137  
138 18 my $recvWindow;
139 18 ▶ if ($params->{signed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"signed"}) 6 12 18
140 $recvWindow = $query->{'recvWindow'} // $body->{'recvWindow'} //
141 6 ▶ defined $self->{'recvWindow'} ? $self->{'recvWindow'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
($query->{'recvWindow'} // $body->{'recvWindow'}) // defined $self->{'recvWindow'} ? : 6 0 6
Condition83%5/6
($query->{"recvWindow"} // $body->{"recvWindow"}) // defined $self->{"recvWindow"}
l!l&&r!l&&!r
330
$query->{"recvWindow"} // $body->{"recvWindow"}
l!l&&r!l&&!r
123
MC/DC0%0/3
($query->{"recvWindow"} // $body->{"recvWindow"}) // defined $self->{"recvWindow"}
$query->{"recvWindow"} $body->{"recvWindow"} defined $self->{"recvWindow"}
Truth table75%3/4
A // B // C
A= $query->{"recvWindow"}B= $body->{"recvWindow"}C= defined $self->{"recvWindow"}
ABCresult
1XX1
01X1
0011
0000
142 }
143  
144 18 ▶ $timestamp //= int Time::HiRes::time * 1000 if $params->{'signed'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 6 12 18
Condition66%2/3
$timestamp //= int &Time::HiRes::time() * 1000
l!l&&r!l&&!r
330
MC/DC0%0/2
$timestamp //= int &Time::HiRes::time() * 1000
$timestamp int &Time::HiRes::time() * 1000
Truth table66%2/3
A //= B
A= $timestampB= int &Time::HiRes::time() * 1000
ABresult
1X1
011
000
145  
146 18 my $uri = URI->new( BASE_URL . $path );
147 18 my $full_path = $uri->as_string;
148  
149 18 my %data;
150 # Mixed request (both query params & body params)
151 18 ▶ if (keys %$body && keys %$query) {
Branch: 3 branches
BranchTrueFalseTotal
if (keys %$body and keys %$query) { } 3 15 18
elsif (keys %$query or not keys %$query || keys %$body) { } 12 3 15
elsif (keys %$body) { } 3 0 3
Condition100%3/3
keys %$body and keys %$query
!ll&&!rl&&r
1233
MC/DC100%2/2
keys %$body and keys %$query
keys %$body keys %$query
Truth table100%3/3
A and B
A= keys %$bodyB= keys %$query
ABresult
0X0
100
111
152 3 ▶ if (!defined $body->{'recvWindow'} && defined $recvWindow) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $body->{'recvWindow'} and defined $recvWindow) { } 1 2 3
elsif (not defined $query->{'recvWindow'} and defined $recvWindow) { } 1 1 2
Condition100%3/3
not defined $body->{"recvWindow"} and defined $recvWindow
!ll&&!rl&&r
111
MC/DC100%2/2
not defined $body->{"recvWindow"} and defined $recvWindow
not defined $body->{"recvWindow"} defined $recvWindow
Truth table100%3/3
not(A) and B
A= not defined $body->{"recvWindow"}B= defined $recvWindow
ABresult
0X0
100
111
153 1 $query->{'recvWindow'} = $recvWindow;
154 }
155 ▶ elsif (!defined $query->{'recvWindow'} && defined $recvWindow) {
Condition66%2/3
not defined $query->{"recvWindow"} and defined $recvWindow
!ll&&!rl&&r
011
MC/DC50%1/2
not defined $query->{"recvWindow"} and defined $recvWindow
not defined $query->{"recvWindow"} defined $recvWindow
Truth table66%2/3
not(A) and B
A= not defined $query->{"recvWindow"}B= defined $recvWindow
ABresult
0X0
100
111
156 1 $body->{'recvWindow'} = $recvWindow;
157 }
158  
159 # First, generate escaped parameter sets
160 3 my $tmp = $uri->clone;
161 3 $tmp->query_form($query);
162 3 my $query_params = $tmp->query();
163 3 $tmp->query_form($body);
164 3 my $body_params = $tmp->query();
165  
166 # Add timestamp to the end of body
167 3 ▶ $body_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 1 3
168  
169 # Combine query and body parameters so that we can sign it.
170 # Binance documentation states that mixed content signature
171 # generation should not add the '&' character between query
172 # and body parameter sets.
173 3 my $to_sign = $query_params . $body_params;
174  
175 3 $self->{logger}->debug("Generating signature from: '$to_sign'");
176  
177 $body_params .= '&signature='.hmac_sha256_hex(
178 $to_sign, $self->{secretKey}
179 3 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 1 3
180  
181 3 $full_path .= "?$query_params";
182 3 $data{'Content'} = $body_params;
183 }
184 # Query parameters only
185 ▶ elsif (keys %$query || !keys %$query && !keys %$body) {
Condition83%5/6
keys %$query or not keys %$query || keys %$body
l!l&&r!l&&!r
843
keys %$query || keys %$body
l!l&&r!l&&!r
034
MC/DC66%2/3
keys %$query or not keys %$query || keys %$body
keys %$query keys %$query keys %$body
Truth table75%3/4
A or not(B || C)
A= keys %$queryB= keys %$queryC= keys %$body
ABCresult
1XX1
0001
01X0
0010
186 12 ▶ $query->{'recvWindow'} = $recvWindow if $recvWindow;
Branch: 1 branch
BranchTrueFalseTotal
if $recvWindow 2 10 12
187  
188 12 my $tmp = $uri->clone;
189 12 $tmp->query_form($query);
190 12 my $query_params = $tmp->query();
191  
192 # Add timestamp to the end of query
193 12 ▶ $query_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 10 12
194  
195 12 ▶ $self->{logger}->debug("Generating signature from: '$query_params'")
Branch: 1 branch
BranchTrueFalseTotal
if $query_params 8 4 12
196 if $query_params;
197  
198 $query_params .= '&signature='.hmac_sha256_hex(
199 $query_params, $self->{secretKey}
200 12 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 10 12
201  
202 12 ▶ $full_path .= "?$query_params" if $query_params;
Branch: 1 branch
BranchTrueFalseTotal
if $query_params 8 4 12
203 }
204 # Body parameters only
205 elsif (keys %$body) {
206 3 ▶ $body->{'recvWindow'} = $recvWindow if $recvWindow;
Branch: 1 branch
BranchTrueFalseTotal
if $recvWindow 2 1 3
207  
208 3 $full_path = $uri->as_string;
209  
210 3 my $tmp = $uri->clone;
211 3 $tmp->query_form($body);
212 3 my $body_params = $tmp->query();
213  
214 # Add timestamp to the end of body
215 3 ▶ $body_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 1 3
216  
217 3 $self->{logger}->debug("Generating signature from: '$body_params'");
218  
219 $body_params .= '&signature='.hmac_sha256_hex(
220 $body_params, $self->{secretKey}
221 3 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 1 3
222  
223 3 $data{'Content'} = $body_params;
224 }
225  
226 18 ▶ if (defined $self->{apiKey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"apiKey"}) 0 18 18
227 0 $data{'X_MBX_APIKEY'} = $self->{apiKey};
228 }
229  
230 18 return ($full_path, %data);
231 }
232  
233 3 1;
« API.pm ↑ Summary API/Logger.pm »