API/Request.pm

filter: Statementstmt 81.2% 95 / 117 Branchbran 71.4% 40 / 56 Conditioncond 90.9% 20 / 22 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 81.2% 13 / 16 Podpod 100% 4 / 4 totaltotal 80.1% 182 / 227 CC 39 SCAR 32.9
CC
39
Cov
79%
_exec
42.0
_init
34.6
post
2.0
CRAP 26.8
↑ Summary API.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 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 5 my $class = shift;
53 5 my %params = @_;
54  
55 5 my $self = $class->SUPER::new;
56  
57 5 $self->{apiKey} = $params{'apiKey'};
58 5 $self->{secretKey} = $params{'secretKey'};
59 5 $self->{recvWindow} = $params{'recvWindow'};
60 5 $self->{baseUrl} = $params{'baseUrl'};
61 5 $self->{logger} = $params{'logger'};
62  
63 5 bless $self, $class;
64 }
65  
66 9 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
67 9 my ($self, $url, $params) = @_;
68  
69 9 my ($path, %data) = $self->_init($url, $params);
70 9 return $self->_exec('get', $path, %data);
71 }
72  
73 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, $url, $params) = @_;
75  
76 0 my ($path, %data) = $self->_init($url, $params);
77 0 return $self->_exec('post', $path, %data);
78 }
79  
80 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self, $url, $params) = @_;
82  
83 0 my ($path, %data) = $self->_init($url, $params);
84 0 return $self->_exec('delete', $path, %data);
85 }
86  
87 0 ▶sub _exec {
Subroutine _exec: not called CC=6 SCAR=37.4
88 0 my ($self, $method, $url, %data) = @_;
89  
90 0 $self->{logger}->debug("New request: $url");
91 0 $method = "SUPER::$method";
92 0 my $response;
93 0 ▶ if (keys %data > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %data > 0) { } 0 0 0
94 0 $response = $self->$method($url, %data);
95 } else {
96 0 $response = $self->$method($url);
97 }
98 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
99 0 $response = eval { decode_json($response->decoded_content); };
100 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101 $self->{logger}->error(
102 0 ▶ "Error decoding response. \nStatus => " . $response->code . ",\n"
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
103 . 'Content => ' . ($response->content ? $response->content : '')
104 );
105 }
106 } else {
107 $self->{logger}->error(
108 0 ▶ "Unsuccessful request. \nStatus => " . $response->code . ",\n"
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
109 . 'Content => ' . ($response->content ? $response->content : '')
110 );
111 }
112 0 return $response;
113 }
114  
115 18 ▶sub _init {
Subroutine _init: called CC=34 SCAR=35.4
116 18 my ($self, $path, $params) = @_;
117  
118 18 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 18 18
119 0 Binance::Exception::Parameter::Required->throw(
120 error => 'Parameter "path" required',
121 parameters => ['path']
122 );
123 }
124  
125 18 my $timestamp = $params->{'timestamp'};
126 18 delete $params->{'timestamp'};
127 # Delete undefined query parameters
128 18 my $query = $params->{'query'};
129 18 foreach my $param (keys %$query) {
130 31 ▶ delete $query->{$param} unless defined $query->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query->{$param} 10 21 31
131 }
132  
133 # Delete undefined body parameters
134 18 my $body = $params->{'body'};
135 18 foreach my $param (keys %$body) {
136 14 ▶ delete $body->{$param} unless defined $body->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body->{$param} 0 14 14
137 }
138  
139 18 my $recvWindow;
140 18 ▶ if ($params->{signed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"signed"}) 6 12 18
141 $recvWindow = $query->{'recvWindow'} // $body->{'recvWindow'} //
142 6 ▶ defined $self->{'recvWindow'} ? $self->{'recvWindow'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
($query->{'recvWindow'} // $body->{'recvWindow'}) // defined $self->{'recvWindow'} ? : 6 0 6
Condition100%5/5
($query->{"recvWindow"} // $body->{"recvWindow"}) // defined $self->{"recvWindow"}
l!l
33
$query->{"recvWindow"} // $body->{"recvWindow"}
l!l&&r!l&&!r
123
MC/DC100%2/2
($query->{"recvWindow"} // $body->{"recvWindow"}) // defined $self->{"recvWindow"}
$query->{"recvWindow"} $body->{"recvWindow"}
Truth table100%3/3
A // B
A= $query->{"recvWindow"}B= $body->{"recvWindow"}
ABresult
1X1
011
000
143 }
144  
145 18 ▶ $timestamp //= int Time::HiRes::time * 1000 if $params->{'signed'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 6 12 18
Condition100%2/2
$timestamp //= int &Time::HiRes::time() * 1000
l!l
33
MC/DC100%1/1
$timestamp //= int &Time::HiRes::time() * 1000
$timestamp
Truth table100%2/2
A
A= $timestamp
Aresult
11
00
146  
147 18 ▶ my $base_url = defined $self->{'baseUrl'} ? $self->{'baseUrl'} : BASE_URL;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'baseUrl'} ? : 0 18 18
148 18 my $uri = URI->new( $base_url . $path );
149 18 my $full_path = $uri->as_string;
150  
151 18 my %data;
152 # Mixed request (both query params & body params)
153 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
154 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
155 1 $query->{'recvWindow'} = $recvWindow;
156 }
157 ▶ 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
158 1 $body->{'recvWindow'} = $recvWindow;
159 }
160  
161 # First, generate escaped parameter sets
162 3 my $tmp = $uri->clone;
163 3 $tmp->query_form($query);
164 3 my $query_params = $tmp->query();
165 3 $tmp->query_form($body);
166 3 my $body_params = $tmp->query();
167  
168 # Add timestamp to the end of body
169 3 ▶ $body_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 1 3
170  
171 # Combine query and body parameters so that we can sign it.
172 # Binance documentation states that mixed content signature
173 # generation should not add the '&' character between query
174 # and body parameter sets.
175 3 my $to_sign = $query_params . $body_params;
176  
177 3 $self->{logger}->debug("Generating signature from: '$to_sign'");
178  
179 $body_params .= '&signature='.hmac_sha256_hex(
180 $to_sign, $self->{secretKey}
181 3 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 1 3
182  
183 3 $full_path .= "?$query_params";
184 3 $data{'Content'} = $body_params;
185 }
186 # Query parameters only
187 ▶ 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
188 12 ▶ $query->{'recvWindow'} = $recvWindow if $recvWindow;
Branch: 1 branch
BranchTrueFalseTotal
if $recvWindow 2 10 12
189  
190 12 my $tmp = $uri->clone;
191 12 $tmp->query_form($query);
192 12 my $query_params = $tmp->query();
193  
194 # Add timestamp to the end of query
195 12 ▶ $query_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 10 12
196  
197 12 ▶ $self->{logger}->debug("Generating signature from: '$query_params'")
Branch: 1 branch
BranchTrueFalseTotal
if $query_params 8 4 12
198 if $query_params;
199  
200 $query_params .= '&signature='.hmac_sha256_hex(
201 $query_params, $self->{secretKey}
202 12 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 10 12
203  
204 12 ▶ $full_path .= "?$query_params" if $query_params;
Branch: 1 branch
BranchTrueFalseTotal
if $query_params 8 4 12
205 }
206 # Body parameters only
207 elsif (keys %$body) {
208 3 ▶ $body->{'recvWindow'} = $recvWindow if $recvWindow;
Branch: 1 branch
BranchTrueFalseTotal
if $recvWindow 2 1 3
209  
210 3 $full_path = $uri->as_string;
211  
212 3 my $tmp = $uri->clone;
213 3 $tmp->query_form($body);
214 3 my $body_params = $tmp->query();
215  
216 # Add timestamp to the end of body
217 3 ▶ $body_params .= "&timestamp=$timestamp" if defined $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timestamp 2 1 3
218  
219 3 $self->{logger}->debug("Generating signature from: '$body_params'");
220  
221 $body_params .= '&signature='.hmac_sha256_hex(
222 $body_params, $self->{secretKey}
223 3 ▶ ) if $params->{signed};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"signed"} 2 1 3
224  
225 3 $data{'Content'} = $body_params;
226 }
227  
228 18 ▶ if (defined $self->{apiKey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"apiKey"}) 0 18 18
229 0 $data{'X_MBX_APIKEY'} = $self->{apiKey};
230 }
231  
232 18 return ($full_path, %data);
233 }
234  
235 3 1;
↑ Summary API.pm »