| 1 | package Google::API::Method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use URI::Escape qw/uri_escape/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | my (%param) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | for my $required (qw/ua json_parser base_url opt doc/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | die unless $param{$required}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | bless { %param }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my ($self, $arg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my $url = $self->{base_url} . $self->{doc}{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $http_method = uc($self->{doc}{httpMethod}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my %required_param; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | for my $p (@{$self->{doc}{parameterOrder}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | $required_param{$p} = delete $self->{opt}{$p}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ($self->{opt}{body} && $self->{opt}{body}{$p}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"opt"}{"body"} and $self->{"opt"}{"body"}{$p}
MC/DC0%0/2
$self->{"opt"}{"body"} and $self->{"opt"}{"body"}{$p}
$self->{"opt"}{"body"}
$self->{"opt"}{"body"}{$p}
Truth table0%0/3
A and B
A= $self->{"opt"}{"body"}B= $self->{"opt"}{"body"}{$p}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | $required_param{$p} = delete $self->{opt}{body}{$p}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | $url =~ s/{([^}]+)}/uri_escape(delete $required_param{$1})/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($http_method eq 'POST' || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/9
$http_method eq "POST" or $http_method eq "PUT" or $http_method eq "PATCH" or $http_method eq "DELETE"
$http_method eq "POST" or $http_method eq "PUT" or $http_method eq "PATCH"
$http_method eq "POST" or $http_method eq "PUT"
MC/DC0%0/4
$http_method eq "POST" or $http_method eq "PUT" or $http_method eq "PATCH" or $http_method eq "DELETE"
$http_method eq "POST"
$http_method eq "PUT"
$http_method eq "PATCH"
$http_method eq "DELETE"
Truth table0%0/5
A or B or C or D
A= $http_method eq "POST"B= $http_method eq "PUT"C= $http_method eq "PATCH"D= $http_method eq "DELETE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | $http_method eq 'PUT' || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | $http_method eq 'PATCH' || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $http_method eq 'DELETE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $uri->query_form(\%required_param); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $request = HTTP::Request->new($http_method => $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Some API's (ie: admin/directoryv1/groups/delete) require requests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # with an empty body section to be explicitly zero length. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (%{$self->{opt}{body}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $request->content_type('application/json'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $request->content($self->{json_parser}->encode($self->{opt}{body})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $request->content_length(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } elsif ($http_method eq 'GET') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $body = $self->{opt}{body} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'opt'}{'body'} || {}
MC/DC0%0/1
$self->{'opt'}{'body'} || {}
$self->{'opt'}{'body'}
Truth table0%0/2
A
A= $self->{'opt'}{'body'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my %q = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | %required_param, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | %$body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($arg->{key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $q{key} = $arg->{key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $uri->query_form(\%q); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $request = HTTP::Request->new($http_method => $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($arg->{auth_driver}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | $request->header('Authorization', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | sprintf "%s %s", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | $arg->{auth_driver}->token_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $arg->{auth_driver}->access_token); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $response = $self->{ua}->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($response->code == 401 && $arg->{auth_driver}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->code == 401 and $arg->{"auth_driver"}
MC/DC0%0/2
$response->code == 401 and $arg->{"auth_driver"}
$response->code == 401
$arg->{"auth_driver"}
Truth table0%0/3
A and B
A= $response->code == 401B= $arg->{"auth_driver"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $arg->{auth_driver}->refresh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $request->header('Authorization', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | sprintf "%s %s", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $arg->{auth_driver}->token_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $arg->{auth_driver}->access_token); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $response = $self->{ua}->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->_die_with_error($response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($response->code == 204) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | return $response->header('content-type') =~ m!^application/json! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | ? $self->{json_parser}->decode(decode_utf8($response->content)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | : $response->content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub _die_with_error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_die_with_error: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $err_str = $response->status_line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($response->content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->content and $response->header("content-type") =~ m[^application/json]
MC/DC0%0/2
$response->content and $response->header("content-type") =~ m[^application/json]
$response->content
$response->header("content-type") =~ m[^application/json]
Truth table0%0/3
A and B
A= $response->contentB= $response->header("content-type") =~ m[^application/json]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | && $response->header('content-type') =~ m!^application/json!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $content = $self->{json_parser}->decode(decode_utf8($response->content)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $err_str = "$err_str: $content->{error}{message}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | die $err_str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||