| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Net::Google::Drive::Simple::Core; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use HTTP::Request (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use File::MMagic (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use IO::File (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use OAuth::Cmdline::CustomFile (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use OAuth::Cmdline::GoogleDrive (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use Net::Google::Drive::Simple::Item (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use JSON qw( from_json to_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # used in V3.pm too | ||||||||||||||||||||||||||||
| 24 | use constant { | ||||||||||||||||||||||||||||
| 25 | 10 | 'HTTP_CODE_OK' => 200, | |||||||||||||||||||||||||||
| 26 | 'HTTP_CODE_RESUME' => 308, | ||||||||||||||||||||||||||||
| 27 | 10 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 10 | our $VERSION = '3.01'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ########################################### | ||||||||||||||||||||||||||||
| 32 | sub new { | ||||||||||||||||||||||||||||
| 33 | ########################################### | ||||||||||||||||||||||||||||
| 34 | 10 | ▶ | my ( $class, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 10 | my $oauth; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | if ( exists $options{custom_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | $oauth = OAuth::Cmdline::CustomFile->new( custom_file => $options{custom_file} ); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 10 | $oauth = OAuth::Cmdline::GoogleDrive->new(); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 10 | my $self = { | |||||||||||||||||||||||||||
| 46 | init_done => undef, | ||||||||||||||||||||||||||||
| 47 | oauth => $oauth, | ||||||||||||||||||||||||||||
| 48 | error => undef, | ||||||||||||||||||||||||||||
| 49 | %options, | ||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 10 | return bless $self, $class; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ########################################### | ||||||||||||||||||||||||||||
| 56 | sub error { | ||||||||||||||||||||||||||||
| 57 | ########################################### | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my ( $self, $set ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( defined $set ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $self->{error} = $set; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | return $self->{error}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | ########################################### | ||||||||||||||||||||||||||||
| 68 | sub init { | ||||||||||||||||||||||||||||
| 69 | ########################################### | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my ( $self, $path ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( $self->{init_done} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | return 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | DEBUG "Testing API"; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( !$self->api_test() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | LOGDIE "api_test failed"; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $self->{init_done} = 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | return 1; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | ########################################### | ||||||||||||||||||||||||||||
| 87 | sub api_test { | ||||||||||||||||||||||||||||
| 88 | ########################################### | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
api_test: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $url = $self->file_url( { maxResults => 1 } ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||
| 96 | GET => $url->as_string, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | 0 | $req->header( $self->{oauth}->authorization_headers() ); | |||||||||||||||||||||||||||
| 99 | 0 | DEBUG "Fetching $url"; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $resp = $ua->request($req); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $resp->is_success() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | DEBUG "API tested OK"; | |||||||||||||||||||||||||||
| 105 | 0 | return 1; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $self->error( $resp->message() ); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ERROR "API error: ", $resp->message(); | |||||||||||||||||||||||||||
| 111 | 0 | return 0; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ########################################### | ||||||||||||||||||||||||||||
| 115 | sub data_factory { | ||||||||||||||||||||||||||||
| 116 | ########################################### | ||||||||||||||||||||||||||||
| 117 | 2 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
data_factory: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | return Net::Google::Drive::Simple::Item->new($data); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | ########################################### | ||||||||||||||||||||||||||||
| 123 | sub http_loop { | ||||||||||||||||||||||||||||
| 124 | ########################################### | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my ( $self, $req, $noinit ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
http_loop: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 128 | 0 | my $resp; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $RETRIES = 3; | |||||||||||||||||||||||||||
| 131 | 0 | my $SLEEP_INTERVAL = 10; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | # refresh token if necessary | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ( !$noinit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $self->init(); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | DEBUG "Fetching ", $req->url->as_string(); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $resp = $ua->request($req); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # We want to check for success but resume is not an error | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ( !$resp->is_success() && $resp->code() != HTTP_CODE_RESUME() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $resp->is_success and $resp->code != 308
MC/DC0%0/2
not $resp->is_success and $resp->code != 308
not $resp->is_success
$resp->code != 308
Truth table0%0/3
not(A) and B
A= not $resp->is_successB= $resp->code != 308
| |||||||||||||||||||||||||||||
| 145 | 0 | $self->error( $resp->message() ); | |||||||||||||||||||||||||||
| 146 | 0 | warn "Failed with ", $resp->code(), ": ", $resp->message(), "\n"; | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( --$RETRIES >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | ERROR "Retrying in $SLEEP_INTERVAL seconds"; | |||||||||||||||||||||||||||
| 149 | 0 | sleep $SLEEP_INTERVAL; | |||||||||||||||||||||||||||
| 150 | 0 | $self->{oauth}->token_expire(); | |||||||||||||||||||||||||||
| 151 | 0 | $req->header( $self->{oauth}->authorization_headers() ); | |||||||||||||||||||||||||||
| 152 | 0 | redo; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||
| 155 | 0 | ERROR "Out of retries."; | |||||||||||||||||||||||||||
| 156 | 0 | return $resp; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | DEBUG "Successfully fetched ", length( $resp->content() ), " bytes."; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return $resp; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | ########################################### | ||||||||||||||||||||||||||||
| 167 | sub _generate_request { | ||||||||||||||||||||||||||||
| 168 | ########################################### | ||||||||||||||||||||||||||||
| 169 | 4 | ▶ | my ( $self, $url, $info ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_generate_request: called CC=9 SCAR=24.1
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # default verb and headers | ||||||||||||||||||||||||||||
| 172 | 4 | my $verb = $info->{'http_method'}; | |||||||||||||||||||||||||||
| 173 | my @headers = ( | ||||||||||||||||||||||||||||
| 174 | $self->{'oauth'}->authorization_headers(), | ||||||||||||||||||||||||||||
| 175 | 4 | ▶ | @{ $info->{'extra_headers'} || [] }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 4 | my $post_data; | |||||||||||||||||||||||||||
| 179 | 4 | ▶ | if ( $info->{'body_parameters'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 2 | $post_data = to_json( $info->{'body_parameters'} ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 2 | ▶ | if ( !$info->{'multipart'} && !$info->{'resumable'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info->{"multipart"} or $info->{"resumable"}
MC/DC0%0/2
$info->{"multipart"} or $info->{"resumable"}
$info->{"multipart"}
$info->{"resumable"}
Truth table33%1/3
A or B
A= $info->{"multipart"}B= $info->{"resumable"}
| |||||||||||||||||||||||||||||
| 183 | 2 | push @headers, 'Content-Type', 'application/json'; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # We might still have file content, with or without post data | ||||||||||||||||||||||||||||
| 188 | # Handle GET / DELETE ("content" key might not actually existed) | ||||||||||||||||||||||||||||
| 189 | 4 | my $content; | |||||||||||||||||||||||||||
| 190 | 4 | ▶ | if ( $verb !~ /^( GET | DELETE )$/xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Try to copy over content | ||||||||||||||||||||||||||||
| 193 | 2 | $content = $info->{'body_content'}; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # If this is not multipart, we can either have content or post_data | ||||||||||||||||||||||||||||
| 196 | # but since we have no content, we use post_data and clear the var instead | ||||||||||||||||||||||||||||
| 197 | 2 | ▶ | if ( !$content && !$info->{'multipart'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$content or $info->{"multipart"}
MC/DC0%0/2
$content or $info->{"multipart"}
$content
$info->{"multipart"}
Truth table33%1/3
A or B
A= $contentB= $info->{"multipart"}
| |||||||||||||||||||||||||||||
| 198 | 2 | $content = $post_data; | |||||||||||||||||||||||||||
| 199 | 2 | undef $post_data; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 4 | ▶ | if ( $info->{'multipart'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # We have both $content and $post_data | ||||||||||||||||||||||||||||
| 206 | # The $content is the file content | ||||||||||||||||||||||||||||
| 207 | # The $post_data is the JSON content | ||||||||||||||||||||||||||||
| 208 | # We need to create a new body from them | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | my $part1 = "Content-type: application/json; charset=UTF-8\r\n\r\n" . $post_data; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | my $part2 = "Content-type: $info->{'body_parameters'}{'mimeType'}\r\nContent-Transfer-Encoding: base64\r\n\r\n" . MIME::Base64::encode_base64($content); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | my $body = "--my-boundary\r\n$part1\r\n" . "--my-boundary\r\n$part2\r\n" . "--my-boundary--\r\n"; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 10 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | 0 | push @headers, 'Content-type' => 'multipart/related; boundary="my-boundary"', | |||||||||||||||||||||||||||
| 218 | 'Content-Length' => length $body; | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 0 | $content = $body; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 4 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||
| 224 | $verb, | ||||||||||||||||||||||||||||
| 225 | $url->as_string(), | ||||||||||||||||||||||||||||
| 226 | \@headers, | ||||||||||||||||||||||||||||
| 227 | $content, | ||||||||||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 4 | return $req; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | ########################################### | ||||||||||||||||||||||||||||
| 234 | sub _make_request { | ||||||||||||||||||||||||||||
| 235 | ########################################### | ||||||||||||||||||||||||||||
| 236 | 0 | ▶ | my ( $self, $req, $should_return_res ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_request: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my $res = $self->http_loop($req); | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ( $res->is_error() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | $self->error( $res->message() ); | |||||||||||||||||||||||||||
| 241 | 0 | ▶ | return $should_return_res ? $res : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # were we asked to just return the response as is? | ||||||||||||||||||||||||||||
| 245 | $should_return_res | ||||||||||||||||||||||||||||
| 246 | 0 | ▶ | and return $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # v3 returns 204 on DELETE for no content | ||||||||||||||||||||||||||||
| 249 | 0 | ▶ | my $data = $res->code() == 204 ? {} : from_json( $res->content() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | return $data; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # This is only for v2, v3 has something more flexible | ||||||||||||||||||||||||||||
| 254 | ########################################### | ||||||||||||||||||||||||||||
| 255 | sub http_json { | ||||||||||||||||||||||||||||
| 256 | ########################################### | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my ( $self, $url, $post_data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
http_json: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | my @headers = ( $self->{'oauth'}->authorization_headers() ); | |||||||||||||||||||||||||||
| 260 | 0 | my $verb = 'GET'; | |||||||||||||||||||||||||||
| 261 | 0 | my $content; | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ($post_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if ( ref $post_data eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | ( $verb, $post_data ) = @{$post_data}; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | else { | ||||||||||||||||||||||||||||
| 267 | 0 | $verb = 'POST'; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ($post_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | push @headers, "Content-Type", "application/json"; | |||||||||||||||||||||||||||
| 272 | 0 | $content = to_json($post_data); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||
| 277 | $verb, | ||||||||||||||||||||||||||||
| 278 | $url->as_string(), | ||||||||||||||||||||||||||||
| 279 | \@headers, | ||||||||||||||||||||||||||||
| 280 | $content, | ||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 0 | my $resp = $self->http_loop($req); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ( $resp->is_error() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | $self->error( $resp->message() ); | |||||||||||||||||||||||||||
| 287 | 0 | return; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | my $data = from_json( $resp->content() ); | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | return $data; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | ########################################### | ||||||||||||||||||||||||||||
| 296 | sub file_mime_type { | ||||||||||||||||||||||||||||
| 297 | ########################################### | ||||||||||||||||||||||||||||
| 298 | 0 | ▶ | my ( $self, $file ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_mime_type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # There don't seem to be great implementations of mimetype | ||||||||||||||||||||||||||||
| 301 | # detection on CPAN, so just use this one for now. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | ▶ | if ( !$self->{magic} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | $self->{magic} = File::MMagic->new(); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | return $self->{magic}->checktype_filename($file); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | ########################################### | ||||||||||||||||||||||||||||
| 311 | sub item_iterator { | ||||||||||||||||||||||||||||
| 312 | ########################################### | ||||||||||||||||||||||||||||
| 313 | 1 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
item_iterator: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 1 | my $idx = 0; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 1 | ▶ | if ( !defined $data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | die "no data in item_iterator"; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | return sub { | ||||||||||||||||||||||||||||
| 322 | { | ||||||||||||||||||||||||||||
| 323 | 3 | ▶ | my $next_item = $data->{items}->[ $idx++ ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 3 | ▶ | return if !defined $next_item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 2 | ▶ | if ( $next_item->{labels}->{trashed} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | DEBUG "Skipping $next_item->{ title } (trashed)"; | |||||||||||||||||||||||||||
| 329 | 0 | redo; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 2 | return $next_item; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | 1 | }; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | ########################################### | ||||||||||||||||||||||||||||
| 338 | sub file_url { | ||||||||||||||||||||||||||||
| 339 | ########################################### | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | my ( $self, $opts ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_url: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $opts = {} if !defined $opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 0 | my $default_opts = { | |||||||||||||||||||||||||||
| 345 | maxResults => 3000, | ||||||||||||||||||||||||||||
| 346 | }; | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | $opts = { | |||||||||||||||||||||||||||
| 349 | %$default_opts, | ||||||||||||||||||||||||||||
| 350 | %$opts, | ||||||||||||||||||||||||||||
| 351 | }; | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | my $url = URI->new( $self->{api_file_url} ); | |||||||||||||||||||||||||||
| 354 | 0 | $url->query_form($opts); | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | return $url; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | ########################################### | ||||||||||||||||||||||||||||
| 360 | sub file_metadata { | ||||||||||||||||||||||||||||
| 361 | ########################################### | ||||||||||||||||||||||||||||
| 362 | 0 | ▶ | my ( $self, $file_id ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
file_metadata: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | LOGDIE 'Deletion requires file_id' if ( !defined $file_id ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | my $url = URI->new( $self->{api_file_url} . "/$file_id" ); | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | return $self->http_json($url); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | ########################################### | ||||||||||||||||||||||||||||
| 372 | sub _content_sub { | ||||||||||||||||||||||||||||
| 373 | ########################################### | ||||||||||||||||||||||||||||
| 374 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_content_sub: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 375 | 0 | my $filename = shift; | |||||||||||||||||||||||||||
| 376 | 0 | my @stat = stat $filename; | |||||||||||||||||||||||||||
| 377 | 0 | my $remaining = $stat[7]; | |||||||||||||||||||||||||||
| 378 | 0 | ▶ | my $blksize = $stat[11] || 4096; | ||||||||||||||||||||||||||
|
Condition0%0/2
$stat[11] || 4096
MC/DC0%0/1
$stat[11] || 4096
$stat[11]
Truth table0%0/2
A
A= $stat[11]
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | ▶ | die "$filename not a readable file with fixed size" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-r $filename and $remaining
MC/DC0%0/2
-r $filename and $remaining
-r $filename
$remaining
Truth table0%0/3
A and B
A= -r $filenameB= $remaining
| |||||||||||||||||||||||||||||
| 381 | unless -r $filename | ||||||||||||||||||||||||||||
| 382 | and $remaining; | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | my $fh = IO::File->new( $filename, 'r' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | or die "Could not open $filename: $!"; | ||||||||||||||||||||||||||||
| 386 | 0 | $fh->binmode; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | return sub { | ||||||||||||||||||||||||||||
| 389 | 0 | ▶ | my $buffer; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | # upon retries the file is closed and we must reopen it | ||||||||||||||||||||||||||||
| 392 | 0 | ▶ | unless ( $fh->opened ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | $fh = IO::File->new( $filename, 'r' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | or die "Could not open $filename: $!"; | ||||||||||||||||||||||||||||
| 395 | 0 | $fh->binmode; | |||||||||||||||||||||||||||
| 396 | 0 | $remaining = $stat[7]; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 0 | ▶ | unless ( my $read = $fh->read( $buffer, $blksize ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 0 | ▶ | die "Error while reading upload content $filename ($remaining remaining) $!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! and $remaining
MC/DC0%0/2
$! and $remaining
$!
$remaining
Truth table0%0/3
A and B
A= $!B= $remaining
| |||||||||||||||||||||||||||||
| 401 | if $! and $remaining; | ||||||||||||||||||||||||||||
| 402 | 0 | ▶ | $fh->close # otherwise, we found EOF | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | or die "close of upload content $filename failed: $!"; | ||||||||||||||||||||||||||||
| 404 | 0 | ▶ | $buffer ||= ''; # LWP expects an empty string on finish, read returns 0 | ||||||||||||||||||||||||||
|
Condition0%0/2
$buffer ||= ""
MC/DC0%0/1
$buffer ||= ""
$buffer
Truth table0%0/2
A
A= $buffer
| |||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 0 | $remaining -= length($buffer); | |||||||||||||||||||||||||||
| 407 | 0 | return $buffer; | |||||||||||||||||||||||||||
| 408 | 0 | }; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 10 | 1; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | __END__ | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =pod | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | This is a baseclass that the V2 and V3 implementations of the module use. | ||||||||||||||||||||||||||||
| 420 | You shouldn't use this class directly. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head1 METHODS | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | These are methods that are shared among L<Net::Google::Drive::Simple::V2> | ||||||||||||||||||||||||||||
| 425 | and L<Net::Google::Drive::Simple::V3>. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | You wouldn't normally use these methods. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =head2 C<new> | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | Parent method to create one L<Net::Google::Drive::Simple> object. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head2 C<error> | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Set and retrieve the current error. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head2 C<init> | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Internal initialization to setup the connection. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head2 C<api_test> | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | Used at init time to check that the connection is correct. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =head2 C<data_factory> | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Set up an object of L<Net::Google::Drive::Simple::Item>. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =head2 C<http_json> | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | Make an HTTP request with a body. | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =head2 C<http_loop> | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | Perform a request. | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =head2 C<file_metadata> | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | my $metadata_hash_ref = $gd->file_metadata($fileId); | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Return metadata about the file with the specified ID from Google Drive. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =head2 C<file_url> | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Retrieve a file URL. | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head2 C<file_mime_type> | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Retrieve the mime type of a file. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =head2 C<item_iterator> | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | Create an iterator over items. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =head2 C<path_resolve> | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | Resolve paths to the folder ID. | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||