| 1 | package Google::API::Client; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use 5.008_001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | our $VERSION = '0.13'; | |||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Google::API::Method; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Google::API::Resource; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use constant AUTH_URI => 'https://accounts.google.com/o/oauth2/auth'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use constant TOKEN_URI => 'https://accounts.google.com/o/oauth2/token'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | sub new { | ||||||||||
| 15 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 16 | 1 | my ($param) = @_; | |||||||||
| 17 | 1 | ▶ | unless (defined $param->{ua}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | $param->{ua} = $class->_new_ua; | |||||||||
| 19 | } | ||||||||||
| 20 | 1 | ▶ | unless (defined $param->{json_parser}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | $param->{json_parser} = $class->_new_json_parser; | |||||||||
| 22 | } | ||||||||||
| 23 | 1 | bless { %$param }, $class; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub build { | ||||||||||
| 27 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
build: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 28 | 1 | my ($service, $version, $args) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 1 | my $discovery_service_url = 'https://www.googleapis.com/discovery/v1/apis/{api}/{apiVersion}/rest'; | |||||||||
| 31 | 1 | $discovery_service_url =~ s/{api}/$service/; | |||||||||
| 32 | 1 | $discovery_service_url =~ s/{apiVersion}/$version/; | |||||||||
| 33 | |||||||||||
| 34 | 1 | my $req = HTTP::Request->new(GET => $discovery_service_url); | |||||||||
| 35 | 1 | my $res = $self->{ua}->request($req); | |||||||||
| 36 | 1 | ▶ | unless ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | # throw an error | ||||||||||
| 38 | 0 | die 'could not get service document.' . $res->status_line; | |||||||||
| 39 | } | ||||||||||
| 40 | 1 | my $document = $self->{json_parser}->decode($res->content); | |||||||||
| 41 | 1 | $self->build_from_document($document, $discovery_service_url, $args); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub build_from_document { | ||||||||||
| 45 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
build_from_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 1 | my ($document, $url, $args) = @_; | |||||||||
| 47 | 1 | my $base = $document->{basePath}; | |||||||||
| 48 | 1 | my $base_url = URI->new($url); | |||||||||
| 49 | 1 | $base_url = URI->new_abs($base, $base_url); | |||||||||
| 50 | 1 | my $resource = $self->_create_resource($document, $base_url, $args); | |||||||||
| 51 | 1 | return $resource; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub _create_resource { | ||||||||||
| 55 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_create_resource: called CC=7 SCAR=20.8
| |||||||||||
| 56 | 1 | my ($document, $base_url, $args) = @_; | |||||||||
| 57 | 1 | my $root_resource_obj = Google::API::Resource->new; | |||||||||
| 58 | 1 | for my $resource (keys %{$document->{resources}}) { | |||||||||
| 59 | 3 | my $resource_obj; | |||||||||
| 60 | 3 | ▶ | if ($document->{resources}{$resource}{resources}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $resource_obj = $self->_create_resource($document->{resources}{$resource}, $base_url, $args); | |||||||||
| 62 | } | ||||||||||
| 63 | 3 | ▶ | if ($document->{resources}{$resource}{methods}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 3 | ▶ | unless ($resource_obj) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 3 | $resource_obj = Google::API::Resource->new; | |||||||||
| 66 | } | ||||||||||
| 67 | 3 | for my $method (keys %{$document->{resources}{$resource}{methods}}) { | |||||||||
| 68 | $resource_obj->set_attr($method, sub { | ||||||||||
| 69 | 0 | ▶ | my (%param) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 70 | return Google::API::Method->new( | ||||||||||
| 71 | ua => $self->{ua}, | ||||||||||
| 72 | json_parser => $self->{json_parser}, | ||||||||||
| 73 | base_url => $base_url, | ||||||||||
| 74 | 0 | doc => $document->{resources}{$resource}{methods}{$method}, | |||||||||
| 75 | opt => \%param, | ||||||||||
| 76 | ); | ||||||||||
| 77 | 9 | }); | |||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | 3 | ▶ | $root_resource_obj->set_attr($resource, sub { $resource_obj } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 81 | } | ||||||||||
| 82 | 1 | ▶ | if ($document->{auth}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 1 | $root_resource_obj->{auth_doc} = $document->{auth}; | |||||||||
| 84 | } | ||||||||||
| 85 | 1 | return $root_resource_obj; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub _new_ua { | ||||||||||
| 89 | 2 | ▶ | my $class = shift; | ||||||||
|
Subroutine
_new_ua: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 2 | require LWP::UserAgent; | |||||||||
| 91 | 2 | my $ua = LWP::UserAgent->new; | |||||||||
| 92 | 2 | return $ua; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | sub _new_json_parser { | ||||||||||
| 96 | 2 | ▶ | my $class = shift; | ||||||||
|
Subroutine
_new_json_parser: called CC=1 SCAR=0.0
| |||||||||||
| 97 | 2 | require JSON; | |||||||||
| 98 | 2 | my $parser = JSON->new; | |||||||||
| 99 | 2 | return $parser; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 2 | 1; | |||||||||
| 103 | __END__ | ||||||||||