| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Google::API::Method; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 21 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 91 |  | 
| 4 | 3 |  |  | 3 |  | 15 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 75 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  | 3 |  | 1640 | use Encode; | 
|  | 3 |  |  |  |  | 46271 |  | 
|  | 3 |  |  |  |  | 270 |  | 
| 7 | 3 |  |  | 3 |  | 1457 | use HTTP::Request; | 
|  | 3 |  |  |  |  | 62916 |  | 
|  | 3 |  |  |  |  | 103 |  | 
| 8 | 3 |  |  | 3 |  | 23 | use URI; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 74 |  | 
| 9 | 3 |  |  | 3 |  | 15 | use URI::Escape qw/uri_escape/; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 2296 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new { | 
| 12 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 13 | 0 |  |  |  |  |  | my (%param) = @_; | 
| 14 | 0 |  |  |  |  |  | for my $required (qw/ua json_parser base_url opt doc/) { | 
| 15 | 0 | 0 |  |  |  |  | die unless $param{$required}; | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 0 |  |  |  |  |  | bless { %param }, $class; | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub execute { | 
| 21 | 0 |  |  | 0 | 1 |  | 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}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 26 | 0 |  |  |  |  |  | $required_param{$p} = delete $self->{opt}{$p}; | 
| 27 | 0 | 0 | 0 |  |  |  | if ($self->{opt}{body} && $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; | 
|  | 0 |  |  |  |  |  |  | 
| 32 | 0 |  |  |  |  |  | my $uri = URI->new($url); | 
| 33 | 0 |  |  |  |  |  | my $request; | 
| 34 | 0 | 0 | 0 |  |  |  | if ($http_method eq 'POST' || | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 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 | 0 |  |  |  |  | if (%{$self->{opt}{body}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 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 |  | 0 |  |  |  | my $body = $self->{opt}{body} || {}; | 
| 50 | 0 |  |  |  |  |  | my %q = ( | 
| 51 |  |  |  |  |  |  | %required_param, | 
| 52 |  |  |  |  |  |  | %$body, | 
| 53 |  |  |  |  |  |  | ); | 
| 54 | 0 | 0 |  |  |  |  | if ($arg->{key}) { | 
| 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 | 0 |  |  |  |  | if ($arg->{auth_driver}) { | 
| 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 | 0 | 0 |  |  |  | if ($response->code == 401 && $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 | 0 |  |  |  |  | unless ($response->is_success) { | 
| 76 | 0 |  |  |  |  |  | $self->_die_with_error($response); | 
| 77 |  |  |  |  |  |  | } | 
| 78 | 0 | 0 |  |  |  |  | if ($response->code == 204) { | 
| 79 | 0 |  |  |  |  |  | return 1; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | return $response->header('content-type') =~ m!^application/json! | 
| 82 | 0 | 0 |  |  |  |  | ? $self->{json_parser}->decode(decode_utf8($response->content)) | 
| 83 |  |  |  |  |  |  | : $response->content | 
| 84 |  |  |  |  |  |  | ; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub _die_with_error { | 
| 88 | 0 |  |  | 0 |  |  | my ($self, $response) = @_; | 
| 89 | 0 |  |  |  |  |  | my $err_str = $response->status_line; | 
| 90 | 0 | 0 | 0 |  |  |  | if ($response->content | 
| 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 |  |  |  |  |  |  | 1; | 
| 99 |  |  |  |  |  |  | __END__ |