- SYNOPSIS
  - OAUTH CREDENTIALS FILE TO ACCESS SERVCICES
  - api_query
  - has_scope_to_access_api_endpoint
  - methods_available_for_google_api_id
  - list_of_available_google_api_ids
- KEY FEATURES
  - TODO:
- SEE ALSO

# SYNOPSIS

Module aspires to simplify usage of Google API Services using an OAUTH2'd client. Aim is to streamline endpoint access without forcing an additional opinionated abstraction layer that imposes unecessary congnitive load. Users of the module should be able to access all Google Services and either use helper features or fully construct requests in a way that is as portable to alternative approaches as possible.

        ## use goauth CLI helper to create the OATH credentials file ( see perldoc goauth )

        use WebService::GoogleAPI::Client;
        use Data::Dumper;

        my $gapi = WebService::GoogleAPI::Client->new(debug => 0); # my $gapi = WebService::GoogleAPI::Client->new(access_token => '');
        my $user = 'peter@pscott.com.au'; # full gmail or G-Suite email

        $gapi->auth_storage->setup({type => 'jsonfile', path => './gapi.json' }); # by default

        $gapi->user($user);       ## allows to select user configuration by google auth'd email address
        $gapi->do_autorefresh(1); ## refresh auth token with refresh token if auth session has expired

## OAUTH CREDENTIALS FILE TO ACCESS SERVCICES

While I personally find the goauth tool useful to create local auth credentials and can work with the code, you may find it more useful to do it all yourself using an approach like at https://blog.ludin.org/2017/01/15/using-google-apis-with-perl-part-3-simplicity/

This code still retains a lot of the bad smells of the parents.

## api_query

query Google API with option to validate request before submitting

handles user auth token inclusion in request headers and refreshes token if required and possible

Required params: method, route

Optional params: api_endpoint_id

$self->access_token must be valid

Examples of usage:

      $gapi->api_query({
          method => 'get',
          path => 'https://www.googleapis.com/calendar/users/me/calendarList',
        });

      $gapi->api_query({
          method => 'post',
          path => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
          options => { key => value }
      }

      ## if provide the Google API Endpoint to inform pre-query validation
      say $gapi_agent->api_query(
          api_endpoint_id => 'gmail.users.messages.send',
          options    => { raw => encode_base64(
                                                Email::Simple->create( header => [To => $user, From => $user, Subject =>"Test email from $user",],
                                                                        body   => "This is the body of email from $user to $user", )->as_string
                                              ),
                        },
      )->to_string; ##

      print  $gapi_agent->api_query(
                api_endpoint_id => 'gmail.users.messages.list', ## auto sets method to GET, path to 'https://www.googleapis.com/calendar'
              )->to_string;
      #print Dumper $r;

      NB: including the version in the API Endpoint Spec is not supported .. yet? eg gmail:v1.users.messages.list .. will always use the latest stable version

      if the pre-query validation fails then a 418 - I'm a Teapot error response is returned with the
      body containing the specific description of the errors ( Tea Leaves ;^) ).

Returns Mojo::Message::Response object

## `has_scope_to_access_api_endpoint`

Given an API Endpoint such as 'gmail.users.settings.sendAs.get' returns 1 iff user has scope to access

Returns 0 if scope to access is not available to the user.

warns and returns 0 on error ( eg user or config not specified etc )

## `methods_available_for_google_api_id`

Returns a hashref keyed on the Google service API Endpoint in dotted format. The hashed content contains a structure representing the corresponding discovery specification for that method ( API Endpoint )

        methods_available_for_google_api_id('gmail.users.settings.delegates.get')

TODO: consider ? refactor to allow parameters either as a single api id such as 'gmail' as well as the currently accepted hash keyed on the api and version

SEE ALSO: The following methods are delegated through to Client::Discovery - see perldoc WebService::Client::Discovery for detils

      get_method_meta
      discover_all
      extract_method_discovery_detail_from_api_spec
      get_api_discovery_for_api_id

## `list_of_available_google_api_ids`

Returns an array list of all the available API's described in the API Discovery Resource that is either fetched or cached in CHI locally for 30 days.

# KEY FEATURES

API Discovery with local caching using CHI File
OAUTH app credentials (client_id, client_secret, scope, users access_token and refresh_tokens) stored in local file (default name = gapi.json)
access_token refreshes when expires (if user has refresh_token) saving refreshed token back to json file
helper api_query to streamline request composition without preventing manual construction if preferred.
CLI tool (goauth) with lightweight http server to simplify OAuth2 configuration, sccoping, authorization and obtaining access_ and refresh_ tokensn from users

## TODO:

Refactor AuthStorage and Credentials modules - include capability to handle service accounts credentials which are not currently supported. See https://gist.github.com/gsainio/6322375
standardise terminology in documentation and code
include POD for delegated methods
basic CLI to query API Discovery data ( Potentially with OPEN/Swagger Output option )

# SEE ALSO

- "/www.blog.google/products/google-cloud/" in Google Cloud Blog https:>

- Moo::Google - The original code base later forked into WebService::Google by Steve Dondley. This is where this code started believing that I could clean it up. In hindisght it would have been much easier to start from scratch.

- "/github.com/APIs-guru/google-discovery-to-swagger" in Google Swagger API https:

- OAuth2::Client::Google - Perl 6 OAuth2 Client