blib/lib/Finance/CoinbasePro/Lite.pm

filter: Statementstmt 27.2% 24 / 88 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 58.3% 7 / 12 Podpod 100% 3 / 3 totaltotal 21.5% 34 / 158 CC 24 SCAR 52.0
CC
24
Cov
18%
_request
342.0
new
30.0
_get_json
12.0
CRAP 181.5
↑ Summary
1 package Finance::CoinbasePro::Lite;
2  
3 1 our $DATE = '2018-11-29'; # DATE
4 1 our $VERSION = '0.004'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Digest::SHA qw(hmac_sha256_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $url_prefix = "https://api.pro.coinbase.com";
16  
17 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
18 0 my ($class, %args) = @_;
19  
20 0 my $self = {};
21 0 ▶ if (my $key = delete $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = delete $args{"key"}) 0 0 0
22 0 $self->{key} = $key;
23 }
24 0 ▶ if (my $secret = delete $args{secret}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $secret = delete $args{"secret"}) 0 0 0
25 0 $self->{secret} = $secret;
26 }
27 0 ▶ if (my $passphrase = delete $args{passphrase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $passphrase = delete $args{"passphrase"}) 0 0 0
28 0 $self->{passphrase} = $passphrase;
29 }
30 0 ▶ if (keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 0 0
31 0 die "Unknown argument(s): ".join(", ", sort keys %args);
32 }
33  
34 0 require HTTP::Tiny;
35 0 $self->{_http} = HTTP::Tiny->new;
36  
37 0 require JSON::XS;
38 0 $self->{_json} = JSON::XS->new;
39  
40 0 require URI::Encode;
41 0 $self->{_urienc} = URI::Encode->new;
42  
43 0 bless $self, $class;
44 }
45  
46 0 ▶sub _get_json {
Subroutine _get_json: not called CC=3 SCAR=24.8
47 0 my ($self, $url) = @_;
48  
49 0 log_trace("JSON API request: %s", $url);
50  
51 0 my $res = $self->{_http}->get($url);
52 die "Can't retrieve $url: $res->{status} - $res->{reason}"
53 0 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
54 0 my $decoded;
55 0 eval { $decoded = $self->{_json}->decode($res->{content}) };
56 0 ▶ die "Can't decode response from $url: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
57  
58 0 log_trace("JSON API response: %s", $decoded);
59  
60 0 $decoded;
61 }
62  
63 0 ▶sub _request {
Subroutine _request: not called CC=18 SCAR=58.3
64 0 my ($self, $is_private, $method, $request_path, $params) = @_;
65  
66 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
67  
68 0 ▶ if ($is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_private) 0 0 0
69 0 ▶ $self->{key} or die "Please supply API key in new()";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"key"} or die "Please supply API key in new()" 0 0 0
70 0 ▶ $self->{secret} or die "Please supply API secret in new()";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"secret"} or die "Please supply API secret in new()" 0 0 0
71 0 ▶ $self->{passphrase} or die "Please supply API passphrase in new()";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"passphrase"} or die "Please supply API passphrase in new()" 0 0 0
72 }
73  
74 0 ▶ my $time = $ENV{FINANCE_COINBASEPRO_LITE_DEBUG_TIME} // sprintf "%.3f", time();
Condition0%0/2
$ENV{"FINANCE_COINBASEPRO_LITE_DEBUG_TIME"} // sprintf("%.3f", &Finance::CoinbasePro::Lite::time())
l!l
00
MC/DC0%0/1
$ENV{"FINANCE_COINBASEPRO_LITE_DEBUG_TIME"} // sprintf("%.3f", &Finance::CoinbasePro::Lite::time())
$ENV{"FINANCE_COINBASEPRO_LITE_DEBUG_TIME"}
Truth table0%0/2
A
A= $ENV{"FINANCE_COINBASEPRO_LITE_DEBUG_TIME"}
Aresult
11
00
75 0 ▶ log_trace("API %s request [%s]: %s %s %s",
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 0 0 0
76 $is_private ? "private" : "public",
77 $time, $method, $request_path, $params);
78  
79 0 my $url = "$url_prefix$request_path";
80  
81 0 my $body;
82 0 my $encoded_body = '';
83 0 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 0 0
84 0 $body = $encoded_body = $self->{_json}->encode($params);
85 } else {
86 0 ▶ if (keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$params) 0 0 0
87 my $qs = '?' . join(
88 "&",
89 0 map { $self->{_urienc}->encode($_ // ''). "=" .
90 0 ▶ $self->{_urienc}->encode($params->{$_} // '') }
Condition0%0/4
$_ // ""
l!l
00
$params->{$_} // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
MC/DC0%0/1
$params->{$_} // ""
$params->{$_}
Truth table0%0/2
A
A= $_
Aresult
11
00
Truth table0%0/2
A
A= $params->{$_}
Aresult
11
00
91 sort keys(%$params),
92 );
93 0 $url .= $qs;
94 0 $encoded_body = $qs;
95 }
96 }
97  
98 0 my $signature;
99 0 ▶ if ($is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_private) 0 0 0
100 0 my $what = $time . $method . $request_path . $encoded_body;
101 0 $signature = hmac_sha256_base64($what, decode_base64($self->{secret}));
102 0 ▶ while (length($signature) % 4) { $signature .= '=' }
Branch: 1 branch
BranchTrueFalseTotal
if (length($signature) % 4) 0 0 0
103 }
104  
105 my $options = {
106 headers => {
107 ("CB-ACCESS-KEY" => $self->{key}) x !!$is_private,
108 ("CB-ACCESS-SIGN" => $signature ) x !!$is_private,
109 ("CB-ACCESS-TIMESTAMP" => $time ) x !!$is_private,
110 0 ("CB-ACCESS-PASSPHRASE" => $self->{passphrase}) x !!$is_private,,
111 "Content-Type" => "application/json",
112 "Accept" => "application/json",
113 },
114 (content => $body) x !!defined($body),
115 };
116  
117 0 my $res = $self->{_http}->request($method, $url, $options);
118  
119 0 ▶ if ($res->{headers}{'content-type'} =~ m!application/json!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"headers"}{"content-type"} =~ m[application/json]) 0 0 0
120 0 $res->{content} = $self->{_json}->decode($res->{content});
121 }
122  
123 0 log_trace("API response [%s]: %s", $time, $res->{content});
124  
125 0 my $reason = $res->{reason};
126 0 ▶ if ($res->{status} != 200 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} != 200 and ref $res->{"content"} eq "HASH" and $res->{"content"}{"message"}) 0 0 0
Condition0%0/6
$res->{"status"} != 200 and ref $res->{"content"} eq "HASH" and $res->{"content"}{"message"}
!ll&&!rl&&r
000
$res->{"status"} != 200 and ref $res->{"content"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$res->{"status"} != 200 and ref $res->{"content"} eq "HASH" and $res->{"content"}{"message"}
$res->{"status"} != 200 ref $res->{"content"} eq "HASH" $res->{"content"}{"message"}
Truth table0%0/4
A and B and C
A= $res->{"status"} != 200B= ref $res->{"content"} eq "HASH"C= $res->{"content"}{"message"}
ABCresult
0XX0
10X0
1100
1111
127 ref($res->{content}) eq 'HASH' && $res->{content}{message}) {
128 0 $reason .= ": $res->{content}{message}";
129 }
130 0 [$res->{status}, $reason, $res->{content}];
131 }
132  
133 0 ▶sub public_request {
Subroutine public_request: not called CC=1 SCAR=6.9
Pod documented
134 0 my $self = shift;
135 0 $self->_request(0, @_);
136 }
137  
138 0 ▶sub private_request {
Subroutine private_request: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 $self->_request(1, @_);
141 }
142  
143 1 1;
144 # ABSTRACT: Client API library for Coinbase Pro (lite edition)
145  
146 __END__
↑ Summary