Role/UserAgent.pm

filter: Statementstmt 86.1% 118 / 137 Branchbran 50.0% 19 / 38 Conditioncond 44.4% 8 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 84.6% 22 / 26 Podpod 0.0% 0 / 5 totaltotal 71.4% 168 / 235 CC 29 SCAR 18.3
CC
29
Cov
75%
__ANON__
13.9
_prepare_data
9.7
_prepare_params
7.1
CRAP 6.2
« Client.pm ↑ Summary Entity/Account.pm »
1 package Mastodon::Role::UserAgent;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.018'; # TRIAL
7  
8 5 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 my $log = Log::Any->get_logger( category => 'Mastodon' );
13  
14 5 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 ArrayRef
18 Dict
19 HashRef
20 Maybe
21 Num
22 Optional
23 Str
24 Undef
25 slurpy
26 5 );
27 5 ▶use Mastodon::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 HTTPResponse
29 Instance
30 URI
31 UserAgent
32 to_Entity
33 5 );
34 5 ▶use Type::Params qw( compile );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 has instance => (
38 is => 'rw',
39 isa => Instance,
40 default => 'https://mastodon.social',
41 coerce => 1,
42 );
43  
44 5 has api_version => (
45 is => 'ro',
46 isa => Num,
47 default => 1,
48 );
49  
50 5 has redirect_uri => (
51 is => 'ro',
52 isa => Str,
53 lazy => 1,
54 default => 'urn:ietf:wg:oauth:2.0:oob',
55 );
56  
57 has user_agent => (
58 is => 'ro',
59 isa => UserAgent,
60 19 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 19 require HTTP::Thin;
62 19 HTTP::Thin->new;
63 },
64 5 );
65  
66 5 has latest_response => (
67 is => 'ro',
68 isa => Maybe[HTTPResponse],
69 init_args => undef,
70 );
71  
72 2 ▶sub authorization_url {
Subroutine authorization_url: called CC=4 SCAR=14.8
Pod undocumented
73 2 my $self = shift;
74  
75 2 ▶ unless ($self->client_id and $self->client_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client_id and $self->client_secret) 0 2 2
Condition33%1/3
$self->client_id and $self->client_secret
!ll&&!rl&&r
002
MC/DC0%0/2
$self->client_id and $self->client_secret
$self->client_id $self->client_secret
Truth table33%1/3
A and B
A= $self->client_idB= $self->client_secret
ABresult
0X0
100
111
76 0 croak $log->fatal(
77 'Cannot get authorization URL without client_id and client_secret'
78 );
79 }
80  
81 2 state $check = compile( slurpy Dict [ access_code => Optional [Instance] ] );
82 2 my ($params) = $check->(@_);
83  
84 2 ▶ $params->{instance} //= $self->instance;
Condition33%1/3
$params->{"instance"} //= $self->instance
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"instance"} //= $self->instance
$params->{"instance"} $self->instance
Truth table33%1/3
A //= B
A= $params->{"instance"}B= $self->instance
ABresult
1X1
011
000
85  
86 2 my $uri = URI->new('/oauth/authorize')->abs($params->{instance}->uri);
87 2 $uri->query_param(redirect_uri => $self->redirect_uri);
88 2 $uri->query_param(response_type => 'code');
89 2 $uri->query_param(client_id => $self->client_id);
90 2 $uri->query_param(scope => join q{ }, sort(@{$self->scopes}));
91  
92 2 return $uri;
93 }
94  
95 0 ▶sub post { shift->_request( post => shift, data => shift, @_ ) }
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
96 0 ▶sub patch { shift->_request( patch => shift, data => shift, @_ ) }
Subroutine patch: not called CC=1 SCAR=6.9
Pod undocumented
97 22 ▶sub get { shift->_request( get => shift, params => shift, @_ ) }
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
98 0 ▶sub delete { shift->_request( delete => shift, params => shift, @_ ) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
99  
100 22 ▶sub _request {
Subroutine _request: called CC=5 SCAR=17.6
101 22 my $self = shift;
102 22 my $method = shift;
103 22 my $url = shift;
104 22 my $args = { @_ };
105  
106 22 ▶ my $headers = $args->{headers} // {};
Condition50%1/2
$args->{"headers"} // {}
l!l
022
MC/DC0%0/1
$args->{"headers"} // {}
$args->{"headers"}
Truth table50%1/2
A
A= $args->{"headers"}
Aresult
11
00
107 22 my $data = $self->_prepare_data($args->{data});
108  
109 22 $url = $self->_prepare_params($url, $args->{params});
110  
111 22 $method = uc($method);
112  
113 22 ▶ if ($self->can('access_token') and $self->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("access_token") and $self->access_token) 0 22 22
Condition33%1/3
$self->can("access_token") and $self->access_token
!ll&&!rl&&r
0220
MC/DC0%0/2
$self->can("access_token") and $self->access_token
$self->can("access_token") $self->access_token
Truth table33%1/3
A and B
A= $self->can("access_token")B= $self->access_token
ABresult
0X0
100
111
114 $headers = {
115 Authorization => 'Bearer ' . $self->access_token,
116 0 %{$headers},
117 };
118 }
119  
120 22 ▶ if ($log->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_trace) 0 22 22
121 0 require Data::Dumper;
122 0 $log->debugf('Method: %s', $method);
123 0 $log->debugf('URL: %s', $url);
124 0 $log->debugf('Headers: %s', Data::Dumper::Dumper( $headers ));
125 0 $log->debugf('Data: %s', Data::Dumper::Dumper( $data ));
126 }
127  
128 5 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 22 ▶ return try {
Subroutine __ANON__: called CC=12 SCAR=26.3
130 22 my @args = $url;
131 22 ▶ push @args, [%{$data}] unless $method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "GET" 0 22 22
132 22 @args = (@args, %{$headers});
133  
134 22 require HTTP::Request::Common;
135 22 ▶ my $type = ($method eq 'PATCH') ? 'POST' : $method;
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'PATCH' ? : 0 22 22
136 22 my $request = HTTP::Request::Common->can($type)->( @args );
137 22 $request->method($method);
138  
139 22 my $response = $self->user_agent->request( $request );
140  
141 5 ▶ use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
142 5 ▶ use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 # We want to be able to set it, but do not want the user to do so
145 22 $self->{latest_response} = $response;
146  
147 22 ▶ die $response->status_line unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 22 22
148  
149 22 my $payload = decode_json encode('utf8', $response->decoded_content);
150  
151 # Some API calls return empty objects, which cannot be coerced
152 22 ▶ if ($response->decoded_content ne '{}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->decoded_content ne "{}") 22 0 22
153 22 ▶ if ($url !~ /(?:apps|oauth)/ and $self->coerce_entities) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url =~ /(?:apps|oauth)/ and $self->coerce_entities) 22 0 22
Condition33%1/3
not $url =~ /(?:apps|oauth)/ and $self->coerce_entities
!ll&&!rl&&r
0022
MC/DC0%0/2
not $url =~ /(?:apps|oauth)/ and $self->coerce_entities
not $url =~ /(?:apps|oauth)/ $self->coerce_entities
Truth table33%1/3
not(A) and B
A= not $url =~ /(?:apps|oauth)/B= $self->coerce_entities
ABresult
0X0
100
111
154 $payload = (ref $payload eq 'ARRAY')
155 18 ? [ map { to_Entity({ %{$_}, _client => $self }) } @{$payload} ]
156 22 ▶ : to_Entity({ %{$payload}, _client => $self });
Branch: 1 branch
BranchTrueFalseTotal
ref $payload eq 'ARRAY' ? : 17 5 22
157 }
158 }
159  
160 22 ▶ if (ref $payload eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $payload eq 'ARRAY') { } 17 5 22
elsif (ref $payload eq 'HASH') { } 0 5 5
161 17 ▶ die $payload->{error} if any { defined $_->{error} } @{$payload};
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { defined $_->{"error"}; } , @{$payload;}) 0 17 17
162 }
163 elsif (ref $payload eq 'HASH') {
164 0 ▶ die $payload->{error} if defined $payload->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $payload->{"error"} 0 0 0
165 }
166  
167 22 return $payload;
168 }
169 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 my $msg = sprintf 'Could not complete request: %s', $_;
171 0 $log->fatal($msg);
172 0 croak $msg;
173 22 };
174 }
175  
176 22 ▶sub _prepare_data {
Subroutine _prepare_data: called CC=5 SCAR=22.7
177 22 my ($self, $data) = @_;
178 22 ▶ $data //= {};
Condition50%1/2
$data //= {}
l!l
022
MC/DC0%0/1
$data //= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
179  
180 22 foreach my $key (keys %{$data}) {
181 # Array parameters to the API need keys that are marked with []
182 # However, HTTP::Request::Common expects an arrayref to encode files
183 # for transfer, even though the API does not expect that to be an array
184 # So we need to manually skip it, unless we come up with another solution.
185 0 ▶ next if $key eq 'file';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "file" 0 0 0
186  
187 0 my $val = $data->{$key};
188 0 ▶ $data->{$key . '[]'} = delete($data->{$key}) if ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 0 0 0
189 }
190  
191 22 return $data;
192 }
193  
194 22 ▶sub _prepare_params {
Subroutine _prepare_params: called CC=7 SCAR=19.6
195 22 my ($self, $url, $params) = @_;
196 22 ▶ $params //= {};
Condition100%2/2
$params //= {}
l!l
211
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
197  
198 22 ▶ croak 'Cannot make a request without a URL' unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 22 22
199  
200 22 ▶ unless (ref $url eq 'URI') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $url eq "URI") 22 0 22
201 22 ▶ my $base = $url =~ m{^/oauth/} ? '/' : '/api/v' . $self->api_version . '/';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^/oauth/] ? : 0 22 22
202 22 $url = URI->new( $self->instance->uri . $base . $url );
203 }
204  
205 # Adjust query param format to be Ruby-compliant
206 22 foreach my $key (keys %{$params}) {
207 5 my $val = $params->{$key};
208 5 ▶ if (ref $val eq 'ARRAY') { $url->query_param($key . '[]' => @{$val}) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 3 2 5
209 2 else { $url->query_param($key => $val) }
210 }
211  
212 22 return $url;
213 }
214  
215 5 1;
« Client.pm ↑ Summary Entity/Account.pm »