Skip to content

Commit dc905cd

Browse files
committed
POD and replace Moose with Moo dependency
also attempt to address #4 with minor validation check addition to Discovery
1 parent 403680c commit dc905cd

File tree

9 files changed

+83
-36
lines changed

9 files changed

+83
-36
lines changed

.perlcriticrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
[TestingAndDebugging::RequireUseStrict]
22
equivalent_modules = Modern::Perl
33

4-
[TestingAndDebugging::RequireUseWarnings]
5-
equivalent_modules = Modern::Perl
6-
7-
severity = 1

CONTRIBUTING.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
- Use the [Github Issues Page](https://github.com/pscott-au/WebService-GoogleAPI-Client/issues)
44

5-
# CONTRIBUTING CODE
5+
## Github Repo Management
66

77
- Aspiring to [Trunk Based Developent](https://paulhammant.com/2013/04/05/what-is-trunk-based-development/)
8+
- Relase branches are created when package is published to CPAN ( starting V1.12 )
9+
10+
11+
# CONTRIBUTING CODE
12+
813
- Use perlcritic and perltidy if my bracer style is offensive
914
- This is my first module using dzilla to package a module - I'm not completely sold on it and may be using it incorrectly - advice on improving usage welcome
1015
- There remain a few architectural bad smells from the original source code this was based on - don't assume that the class structure is sane
1116
- Pull reqeusts preferred but whatever works for you I will try to work with
1217

1318
# HELP WANTED
1419

20+
- Seek reviews from http://prepan.org/module.submit and similar
1521
- refactor to improve test coverage
1622
- clean up the test structure
23+
- survey other Google Perl modules
1724
- explore handling of batch requests
1825
- API worked examples with help functions
1926
- ability to examine CHI cache and introspect on Client instance metrics ( number of HTTP calls, cache size, TTL data sent/received etc )

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build Status](https://travis-ci.org/pscott-au/WebService-GoogleAPI-Client.svg?branch=master)](https://travis-ci.org/pscott-au//WebService-GoogleAPI-Client)
1+
[![Build Status](https://travis-ci.org/pscott-au/WebService-GoogleAPI-Client.svg?branch=master)](https://travis-ci.org/pscott-au/WebService-GoogleAPI-Client)
22
[![Coverage Status](https://coveralls.io/repos/github/pscott-au/WebService-GoogleAPI-Client/badge.svg?branch=master)](https://coveralls.io/github/pscott-au/WebService-GoogleAPI-Client?branch=master)
33
[![CPAN Version](https://img.shields.io/cpan/v/WebService-GoogleAPI-Client.svg)](http://search.cpan.org/~localshop/WebService-GoogleAPI-Client/lib/WebService/GoogleAPI/Client.pm)
44

@@ -11,16 +11,20 @@ WebService::GoogleAPI::Client - Perl Google API Services OAUTH Client.
1111

1212
# VERSION
1313

14-
version 0.10
14+
version 0.12
1515

1616
# SYNOPSIS
1717

18-
Provides client access to All [Google API V.1](https://developers.google.com/discovery/v1) Services using a user-agent that handles OAUTH2 authentication and access control and provides helpers to exploit locally cached API Discovery specifications.
18+
Provides client access to [Google API V.1](https://developers.google.com/discovery/v1) Service End-Points using a user-agent that handles OAUTH2 authentication and access control and provides helpers to cache API Discovery specifications.
1919

2020
The guiding principal is to minimise the conceptual load when using the Client agent for users who want to make calls directly, but also make available functions to help explore unfamiliar API endpoints by offering optional validation etc against the latest published Google API Discovery specifications.
2121

2222

23-
NB: To create or modify an authorization configuration file with scope and user tokens in current folder run _goauth_ CLI tool to interactively create the JSON configuration and launch a local HTTP server to acquire authenticated access permissions with a Google email account. See ````perldoc goauth```` for more detail.
23+
NB: To create or modify an authorization configuration file with scope and user tokens in current folder run _goauth_ CLI tool to interactively create the JSON configuration and launch a local HTTP server to acquire authenticated access permissions with a Google email account.
24+
25+
![goauth screen capture](https://pscott-au.github.io/WebService-Google-Client/goauth-login-cap.gif)
26+
27+
See ````perldoc goauth```` for more detail.
2428

2529

2630
````perl
@@ -32,8 +36,13 @@ NB: To create or modify an authorization configuration file with scope and user
3236

3337
my $gapi_client = WebService::GoogleAPI::Client->new( debug => 1, gapi_json => './gapi.json', user=> '[email protected]' );
3438

39+
3540
## Completely manually constructed API End-Point Request to obtain Perl Data Structure converted from JSON response.
36-
print Dumper $gapi_client->api_get( userId => 'me' )->json;
41+
my $res = $gapi_client->api_query(
42+
method => 'get',
43+
path => 'https://www.googleapis.com/calendar/users/me/calendarList',
44+
)->json;
45+
3746

3847
## using dotted API Endpoint id to invoke helper validation and default value interpolations etc to send email to self
3948
use Email::Simple; ## RFC2822 formatted messages
@@ -54,7 +63,7 @@ NB: To create or modify an authorization configuration file with scope and user
5463
);
5564

5665

57-
66+
## TEXT TO SPEECH EXAMPLE
5867
my $text_to_speech_request_options = {
5968
'input' => {
6069
'text' => 'Using the Web-Services-Google-Client Perl module, it is now a simple matter to access all of the Google API Resources in a consistent manner.'
@@ -136,16 +145,13 @@ NB: To create or modify an authorization configuration file with scope and user
136145
The code in this repository uses [Dist::Zilla](http://dzil.org/) Build System to assist package building and creation of Tarball and CPAN distribution. Curiously the [Github Repo](https://github.com/rjbs/dist-zilla/) describes itself as '*scary tools for building CPAN distributions*'
137146

138147
````shell
139-
sudo dzil install
148+
sudo dzil install
140149
````
141150

142151
## Install from Tarball
143152

144153
````
145-
## Quickest
146-
sudo cpanm https://dev.pscott.com.au/WebService-Google-Client-latest.tar.gz
147-
148-
## Safest
154+
149155
wget https://dev.pscott.com.au/WebService-Google-Client-latest.tar.gz
150156
tar -zxvf WebService-Google-Client-latest.tar.gz
151157
rm WebService-Google-Client-latest.tar.gz
@@ -214,7 +220,7 @@ sudo dzil install
214220

215221
# SEE ALSO
216222

217-
- [Moo::Google](https://metacpan.org/pod/Moo::Google) - The original code base later forked into [WebService::Google](https://metacpan.org/pod/WebService::Google) but is heading in a different path
223+
- [Moo::Google](https://metacpan.org/pod/Moo::Google) - The original code base later forked into [WebService::Google](https://metacpan.org/pod/WebService::Google) but is heading in a different direction
218224
- [Google Swagger API https:](https:///github.com/APIs-guru/google-discovery-to-swagger)
219225

220226
# AUTHORS

dist.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ main_module = lib/WebService/GoogleAPI/Client.pm
2020
;[UploadToCPAN]
2121

2222
[PkgVersion]
23+
[Prereqs]
24+
; Mojo::Message::Response = 7.12 ## can't do this - no version info available for Mojo::Message::Response
25+
Moose = 2.2
26+
Mojolicious::Plugin::OAuth2 = 1.5
2327

2428
[AutoPrereqs]
2529

docs/goauth-login-cap.gif

22.9 KB
Loading

lib/WebService/GoogleAPI/Client.pm

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,49 @@ package WebService::GoogleAPI::Client;
77
88
Access Google API Services Version 1 using an OAUTH2 User Agent
99
10-
11-
12-
## use goauth CLI helper to create the OATH credentials file ( see perldoc goauth )
13-
1410
use WebService::GoogleAPI::Client;
1511
use Data::Dumper;
1612
17-
my $gapi = WebService::GoogleAPI::Client->new(debug => 0); # my $gapi = WebService::GoogleAPI::Client->new(access_token => '');
18-
my $user = '[email protected]'; # full gmail or G-Suite email
19-
20-
$gapi->auth_storage->setup({type => 'jsonfile', path => './gapi.json' }); # by default
21-
22-
$gapi->user($user); ## allows to select user configuration by google auth'd email address
23-
$gapi->do_autorefresh(1); ## refresh auth token with refresh token if auth session has expired
13+
## assumes gapi.json configuration in working directory with scoped project and user authorization
14+
15+
my $gapi_client = WebService::GoogleAPI::Client->new( debug => 1, gapi_json => './gapi.json', user=> '[email protected]' );
16+
17+
## Completely manually constructed API End-Point Request to obtain Perl Data Structure converted from JSON response.
18+
print Dumper $gapi_client->api_get( userId => 'me' )->json;
19+
20+
## using dotted API Endpoint id to invoke helper validation and default value interpolations etc to send email to self
21+
use Email::Simple; ## RFC2822 formatted messages
22+
use MIME::Base64;
23+
my $my_email_address = '[email protected]'
24+
25+
26+
my $raw_email_payload = encode_base64( Email::Simple->create( header => [To => $my_email_address,
27+
From => $my_email_address,
28+
Subject =>"Test email from '$my_email_address' ",],
29+
body => "This is the body of email to '$my_email_address'",
30+
)->as_string
31+
);
32+
33+
$gapi_client->api_query(
34+
api_endpoint_id => 'gmail.users.messages.send',
35+
options => { raw => $raw_email_payload },
36+
);
37+
38+
39+
40+
my $text_to_speech_request_options = {
41+
'input' => {
42+
'text' => 'Using the Web-Services-Google-Client Perl module, it is now a simple matter to access all of the Google API Resources in a consistent manner.'
43+
},
44+
'voice' => {
45+
'languageCode' => 'en-gb',
46+
'name' => 'en-GB-Standard-A',
47+
'ssmlGender' => 'FEMALE'
48+
},
49+
'audioConfig' => {
50+
'audioEncoding'=> 'MP3'
51+
}
52+
};
2453
2554
2655
=head2 OAUTH CREDENTIALS FILE TO ACCESS SERVCICES
@@ -30,7 +59,7 @@ TODO
3059
=cut
3160

3261
use Data::Dumper;
33-
use Moose;
62+
use Moo;
3463
use WebService::GoogleAPI::Client::UserAgent;
3564
use WebService::GoogleAPI::Client::Discovery;
3665
use Carp;

lib/WebService/GoogleAPI/Client/Discovery.pm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ sub get_api_discovery_for_api_id
6161
{
6262
my ( $self, $params ) = @_;
6363
## TODO: warn if user doesn't have the necessary scope .. no should stil be able to examine
64-
## TODO: consolidate the http method calls to a single function - ie - discover_all - simplistic quick fix - assume that if no param then endpoint is as per discover_all
65-
66-
64+
## TODO: consolidate the http method calls to a single function - ie - discover_all - simplistic quick fix - assume that if no param then endpoint is as per discover_all
6765

6866
$params = { api => $params } if ref( $params) eq ''; ## scalar parameter not hashref - so assume is intended to be $params->{api}
6967
## trim any resource, method or version details in api id
@@ -178,13 +176,16 @@ sub discover_all
178176

179177
if ( my $expires_at = $self->chi->get_expires_at( 'https://www.googleapis.com/discovery/v1/apis' ) && not $force )
180178
{
181-
#carp "discovery_data cached data expires in ", scalar($expires_at) - time(), " seconds\n" if ($self->debug > 2);
182-
return $self->chi->get( 'https://www.googleapis.com/discovery/v1/apis' );
179+
carp "discovery_data cached data expires in ", scalar($expires_at) - time(), " seconds\n" if ($self->debug > 2);
180+
my $ret = $self->chi->get( 'https://www.googleapis.com/discovery/v1/apis' );
181+
croak('CHI Discovery should be a hash - got something other') unless ref($ret) eq 'HASH';
182+
return $ret;
183183
}
184184
else ##
185185
{
186186
#return $self->chi->get('https://www.googleapis.com/discovery/v1/apis') if ($self->chi->get('https://www.googleapis.com/discovery/v1/apis'));
187187
my $ret = $self->ua->validated_api_query( 'https://www.googleapis.com/discovery/v1/apis' );
188+
# if ( $ret->is_status_class(200) ) ## older versions of Mojo::Message::Response don't support is_success .. Require V > 7.12
188189
if ( $ret->is_success )
189190
{
190191
my $all = $ret->json;
@@ -293,7 +294,10 @@ client->discover_all which is delegated to Client::Discovery->discover_all
293294
sub available_APIs
294295
{
295296
my ( $self ) = @_;
296-
my $all = $self->discover_all()->{ items };
297+
my $d_all = $self->discover_all(); ##
298+
warn('no items in discovery data') unless defined $d_all->{ items };
299+
return [] unless defined $d_all->{ items };
300+
my $all = $d_all->{ items };
297301

298302
#print Dumper $all;
299303
for my $i ( @$all )

t/01-client-discovery.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ subtest 'Discovery methods with User Configuration' => sub {
186186
note("Running tests with user '$user' using '$default_file' credentials");
187187

188188
ok( $gapi->user($user) eq $user, "\$gapi->user('$user') eq '$user'");
189-
189+
#$ENV{CHI_FILE_PATH} = $ENV{TMPDIR};
190190
plan( skip_all => 'Skipping network impacting tests unless ENV VAR CHI_FILE_PATH is set' ) unless defined $ENV{CHI_FILE_PATH};
191191

192192

t/pod.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ unless ( $ENV{RELEASE_TESTING} ) {
88
plan( skip_all => "Author tests not required for installation" );
99
}
1010

11+
# TODO: Consider Pod::Coverage::Moose
1112
# Ensure a recent version of Test::Pod
1213
my $min_tp = 1.22;
1314
eval "use Test::Pod $min_tp";

0 commit comments

Comments
 (0)