blib/lib/Finance/Robinhood/Utils/Client.pm

filter: Statementstmt 61.0% 36 / 59 Branchbran 40.9% 9 / 22 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 3 totaltotal 49.1% 58 / 118 CC 16 SCAR 25.4
CC
16
Cov
53%
_http
26.3
__urlencode
12.0
get
3.7
CRAP 12.6
« blib/lib/Finance/Robinhood/Utils/Credentials.pm ↑ Summary blib/lib/Finance/Robinhood.pm »
1 package Finance::Robinhood::Utils::Client;
2 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use JSON::Tiny qw[decode_json encode_json];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 with 'MooX::Singleton';
6 3 ▶use Finance::Robinhood::Utils::Credentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Finance::Robinhood::Utils::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #
9 3 our %Endpoints = (
10 'api-token-auth' => 'https://api.robinhood.com/api-token-auth/',
11 'api-token-logout' => 'https://api.robinhood.com/api-token-logout/',
12 'accounts' => 'https://api.robinhood.com/accounts/',
13 'ach/deposit_schedules' => 'https://api.robinhood.com/ach/deposit_schedules/',
14 'ach/relationships' => 'https://api.robinhood.com/ach/relationships/',
15 'instruments' => 'https://api.robinhood.com/instruments/',
16 'instruments/{id}' => 'https://api.robinhood.com/instruments/%s/',
17 'password_reset/request' => 'https://api.robinhood.com/password_reset/request/',
18 'password_reset' => 'https://api.robinhood.com/password_reset/',
19 'quotes/{symbol}' => 'https://api.robinhood.com/quotes/%s/',
20 'oauth2/migrate_token' => 'https://api.robinhood.com/oauth2/migrate_token/',
21 'options/chains' => 'https://api.robinhood.com/options/chains/',
22 'options/instruments' => 'https://api.robinhood.com/options/instruments/',
23 );
24 has 'http' => (
25 is => 'ro',
26 builder => sub {
27 0 ▶ HTTP::Tiny->new(
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_http: called CC=1 SCAR=1.2
28 agent => 'Finance::Robinhood/' . $Finance::Robinhood::VERSION,
29 default_headers =>
30 { 'X-Robinhood-API-Version' => '1.197.0', 'X-Midlands-API-Version' => '1.48.1' }
31 );
32 },
33 3 lazy => 1
34 );
35 has 'credentials' => (
36 is => 'rw',
37 0 ▶ builder => sub { Finance::Robinhood::Utils::Credentials->instance },
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_credentials: called CC=1 SCAR=1.2
38 3 handles => [qw(headers)],
39 lazy => 1
40 );
41 has 'account' => (
42 is => 'rw',
43 builder => sub {
44 my $acct = Finance::Robinhood::Utils::Paginated->new(
45 class => 'Finance::Robinhood::Account',
46 0 ▶ next => $Finance::Robinhood::Endpoints{'accounts'}
Subroutine _build_account: not called CC=1 SCAR=6.9
47 )->next;
48 },
49 3 lazy => 1
50 );
51  
52 sub _http {
53 6 ▶ my ( $s, $method, $url, $args, $headers ) = @_;
Subroutine _http: called CC=12 SCAR=32.7
54 ###
55 #warn $url;
56 #use Data::Dump;
57 #ddx [caller(1)];
58 #ddx \@_;
59 6 ▶ %$headers = ( %{ $s->headers // () }, $headers ? %$headers : () );
Branch: 1 branch
BranchTrueFalseTotal
$headers ? : 0 6 6
Condition33%1/3
$s->headers // ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$s->headers // ()
$s->headers ()
Truth table33%1/3
A // B
A= $s->headersB= ()
ABresult
1X1
011
000
60  
61 #@$headers{keys %$args} = values %$args;
62 6 ▶ delete $headers->{'Authorization'} if $args->{'skip_authorization'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"skip_authorization"} 0 6 6
63 6 ▶ if ( $method eq 'POST' && $args->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" and $args->{"content"}) 0 6 6
Condition33%1/3
$method eq "POST" and $args->{"content"}
!ll&&!rl&&r
600
MC/DC0%0/2
$method eq "POST" and $args->{"content"}
$method eq "POST" $args->{"content"}
Truth table33%1/3
A and B
A= $method eq "POST"B= $args->{"content"}
ABresult
0X0
100
111
64 0 if (0) {
65 $headers->{'Content-Type'} = 'application/x-www-form-urlencoded';
66 $args->{content} = __urlencode( $args->{content} );
67 }
68 else {
69 0 $headers->{'Content-Type'} = 'application/json';
70 0 $args->{content} = encode_json( $args->{content} );
71 }
72 }
73  
74 #ddx [ $method, $url, { %$args, headers => ($headers) }];
75 6 my $response = $s->http->request( $method, $url, { %$args, headers => ($headers) } );
76 6 ▶ if ( $response->{status} == 429 && $method eq 'GET' && !$headers->{'BOUNCER-FORCE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"status"} == 429 and $method eq "GET" and not $headers->{"BOUNCER-FORCE"}) 0 6 6
Condition33%2/6
$response->{"status"} == 429 and $method eq "GET" and not $headers->{"BOUNCER-FORCE"}
!ll&&!rl&&r
600
$response->{"status"} == 429 and $method eq "GET"
!ll&&!rl&&r
600
MC/DC0%0/3
$response->{"status"} == 429 and $method eq "GET" and not $headers->{"BOUNCER-FORCE"}
$response->{"status"} == 429 $method eq "GET" not $headers->{"BOUNCER-FORCE"}
Truth table25%1/4
A and B and not(C)
A= $response->{"status"} == 429B= $method eq "GET"C= not $headers->{"BOUNCER-FORCE"}
ABCresult
0XX0
10X0
1100
1111
77 0 $headers->{'X-BOUNCER-FORCE'} = 'true';
78 0 return shift->_http(@_);
79 }
80  
81 #die "Failed!\n" unless $response->{success};
82 #print "$response->{status} $response->{reason}\n";
83 #while ( my ( $k, $v ) = each %{ $response->{headers} } ) {
84 # for ( ref $v eq 'ARRAY' ? @$v : $v ) {
85 # print "$k: $_\n";
86 # }
87 #}
88 #ddx$response;
89 6 ▶ my $content = length $response->{content} ? decode_json( $response->{content} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $response->{'content'} ? : 6 0 6
90  
91 #warn $response->{content} if length $response->{content};
92 #use Path::Tiny;
93 # creating Path::Tiny objects
94 #my $dir = path("/tmp");
95 #my $name = $url;
96 #$name =~ s[[^a-z\d]+][_]g;
97 #warn $name;
98 #my $bar = $dir->child( $method . '_' . $name . '.txt' );
99 #$bar->spew( $response->{content} );
100 $content
101 = Finance::Robinhood::Utils::Error->new( status => $response->{status}, data => $content )
102 6 ▶ if $response->{status} > 201;
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} > 201 1 5 6
103  
104 #ddx [$response->{status}, $content];
105 6 ▶ wantarray ? ( $response->{status}, $content ) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
106 }
107  
108 sub __urlencode {
109 0 ▶ my $data = shift;
Subroutine __urlencode: not called CC=3 SCAR=24.8
110 0 ▶ if ( ref $data eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 0 0 0
111 0 return join '&', sort { $a cmp $b } map {
112 0 __urlencode($_) . '=' . join ',',
113 0 map { join ',', __urlencode($_) }
114 0 ▶ @{ ref $data->{$_} eq 'ARRAY' ? $data->{$_} : [ $data->{$_} ] }
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$_} eq 'ARRAY' ? : 0 0 0
115 } keys %$data;
116 }
117 0 $data =~ s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;
118 0 $data =~ s/ /+/g;
119 0 return $data;
120 }
121  
122 sub get {
123 6 ▶ my ( $s, $url, $args ) = @_;
Subroutine get: called CC=3 SCAR=13.1
Pod undocumented
124 6 ▶ $url = $url . ( $url =~ m[\?] ? '&' : '?' ) . __urlencode($args) if keys %$args;
Branch: 2 branches
BranchTrueFalseTotal
if keys %$args 0 6 6
$url =~ /\?/ ? : 0 0 0
125 6 return $s->_http( 'GET', $url );
126 }
127  
128 sub post {
129 0 ▶ my ( $s, $url, $data ) = @_;
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
130 0 return $s->_http( 'POST', $url, { content => $data } );
131 }
132  
133 sub delete {
134 0 ▶ my ( $s, $url ) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
135 0 return $s->_http( 'DELETE', $url );
136 }
137 3 1;
« blib/lib/Finance/Robinhood/Utils/Credentials.pm ↑ Summary blib/lib/Finance/Robinhood.pm »