| 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
Condition33%1/3
$self->client_id and $self->client_secret
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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$params->{"instance"} //= $self->instance
$params->{"instance"}
$self->instance
Truth table33%1/3
A //= B
A= $params->{"instance"}B= $self->instance
| |||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$args->{"headers"} // {}
$args->{"headers"}
Truth table50%1/2
A
A= $args->{"headers"}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->can("access_token") and $self->access_token
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
| |||||||||||||||||||||||||||||
| 114 | $headers = { | ||||||||||||||||||||||||||||
| 115 | Authorization => 'Bearer ' . $self->access_token, | ||||||||||||||||||||||||||||
| 116 | 0 | %{$headers}, | |||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 22 | ▶ | if ($log->is_trace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | 22 | @args = (@args, %{$headers}); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 22 | require HTTP::Request::Common; | |||||||||||||||||||||||||||
| 135 | 22 | ▶ | my $type = ($method eq 'PATCH') ? 'POST' : $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 153 | 22 | ▶ | if ($url !~ /(?:apps|oauth)/ and $self->coerce_entities) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $url =~ /(?:apps|oauth)/ and $self->coerce_entities
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
| |||||||||||||||||||||||||||||
| 154 | $payload = (ref $payload eq 'ARRAY') | ||||||||||||||||||||||||||||
| 155 | 18 | ? [ map { to_Entity({ %{$_}, _client => $self }) } @{$payload} ] | |||||||||||||||||||||||||||
| 156 | 22 | ▶ | : to_Entity({ %{$payload}, _client => $self }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 22 | ▶ | if (ref $payload eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | 17 | ▶ | die $payload->{error} if any { defined $_->{error} } @{$payload}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | elsif (ref $payload eq 'HASH') { | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | die $payload->{error} if defined $payload->{error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 //= {}
MC/DC0%0/1
$data //= {}
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | my $val = $data->{$key}; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | $data->{$key . '[]'} = delete($data->{$key}) if ref $val eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 //= {}
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 22 | ▶ | croak 'Cannot make a request without a URL' unless $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 22 | ▶ | unless (ref $url eq 'URI') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 22 | ▶ | my $base = $url =~ m{^/oauth/} ? '/' : '/api/v' . $self->api_version . '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 209 | 2 | else { $url->query_param($key => $val) } | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 22 | return $url; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 5 | 1; | |||||||||||||||||||||||||||