The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Google::RestApi - Connection to Google REST APIs (currently Drive and Sheets).

SYNOPSIS

      use Google::RestApi;
      $rest_api = Google::RestApi->new(
        config_file   => <path_to_config_file>,
        auth          => <object|hashref>,
        timeout       => <int>,
        throttle      => <int>,
        api_callback  => <coderef>,
      );
    
      $response = $rest_api->api(
        uri     => <google_api_url>,
        method  => get|head|put|patch|post|delete,
        headers => [],
        params  => <query_params>,
        content => <data_for_body>,
      );
    
      use Google::RestApi::SheetsApi4;
      $sheets_api = Google::RestApi::SheetsApi4->new(api => $rest_api);
      $sheet = $sheets_api->open_spreadsheet(title => "payroll");
    
      use Google::RestApi::DriveApi3;
      $drive = Google::RestApi::DriveApi3->new(api => $rest_api);
      $file = $drive->file(id => 'xxxx');
      $copy = $file->copy(title => 'my-copy-of-xxx');
    
      print YAML::Any::Dump($rest_api->stats());

DESCRIPTION

Google Rest API is the foundation class used by the included Drive (Google::RestApi::DriveApi3) and Sheets (Google::RestApi::SheetsApi4) APIs. It is used to send API requests to the Google API endpoint on behalf of the underlying API classes.

NAVIGATION

SUBROUTINES

new(%args); %args consists of:
  • config_file <path_to_config_file>: Optional YAML configuration file that can specify any or all of the following args...

  • auth <hash|object>: A hashref to create the specified auth class, or (outside the config file) an instance of the blessed class itself. If this is an object, it must provide the 'params' and 'headers' subroutines to provide the appropriate Google authentication/authorization. See below for more details.

  • api_callback <coderef>: A coderef to call after each API call.

  • throttle <int>: Used in development to sleep the number of seconds specified between API calls to avoid rate limit violations from Google.

You can specify any of the arguments in the optional YAML config file. Any passed-in arguments will override what is in the config file.

The 'auth' arg can specify a pre-blessed class of one of the Google::RestApi::Auth::* classes (e.g. 'OAuth2Client'), or, for convenience sake, you may specify a hash of the required arguments to create an instance of that class:

  auth:
    class: OAuth2Client
    client_id: xxxxxx
    client_secret: xxxxxx
    token_file: <path_to_token_file>

Note that the auth hash itself can also contain a config_file:

  auth:
    class: OAuth2Client
    config_file: <path_to_oauth_config_file>

This allows you the option to keep the auth file in a separate, more secure place.

api(%args);

The ultimate Google API call for the underlying classes. Handles timeouts and retries etc. %args consists of:

  • uri <uri_string>: The Google API endpoint such as https://www.googleapis.com/drive/v3 along with any path segments added.

  • method <http_method_string>: The http method being used get|head|put|patch|post|delete.

  • headers <headers_string_array>: Array ref of http headers.

  • params <query_parameters_hash>: Http query params to be added to the uri.

  • content <payload hash>: The body being sent for post/put etc. Will be encoded to JSON.

You would not normally call this directly unless you were making a Google API call not currently supported by this API framework.

Returns the response hash from Google API.

api_callback(<coderef>);
coderef is user code that will be called back after each call to the Google API.

The last transaction details are passed to the callback. What you do with this information is up to you. For an example of how this is used, see the t/tutorial/sheets/* scripts.

Returns the previous callback, if any.

transaction();

Returns the transaction information from the last Google API call. This is the same information that is provided by the callback above, but can be accessed directly if you have no need to provide a callback.

stats();

Returns some statistics on how many get/put/post etc calls were made. Useful for performance tuning during development.

STATUS

This api is currently in beta status. It is incomplete. There may be design flaws that need to be addressed in later releases. Later releases may break this release. Not all api calls have been implemented.

AUTHORS

  • Robin Murray mvsjes@cpan.org

COPYRIGHT

Copyright (c) 2021, Robin Murray. All rights reserved.

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.