pscott-au / WebService-GoogleAPI-Client

Perl WebService::GoogleAPI::Client Module
https://pscott-au.github.io/WebService-GoogleAPI-Client/
Other
1 stars 2 forks source link

Example using Batch Request #3

Open pscott-au opened 6 years ago

pscott-au commented 6 years ago

Is your feature request related to a problem? Please describe. The Google API docs suggest using batch request endpoints described in the discovery specs to minimise multiple TCP connection overhead when large volumes of calls are necessary.

Describe the solution you'd like Initially an example of how to do it. This might provide guidance for future batching or use in a non-blicking application.

Describe alternatives you've considered provide a dedicated method to construct batched processes.

Additional context https://developers.google.com/calendar/batch https://www.odata.org/documentation/odata-version-3-0/batch-processing/ https://www.perlmonks.org/?node_id=1223595

pscott-au commented 6 years ago

See The Dot Net API interface for ideas when it comes to incorporating into the Library as a helper .. need to further explore async handling with the agent and think about providing hooks for callbacks.

rabbiveesh commented 5 years ago

Here's a working example to use batch request to fetch metadata for a bunch of different files:

use Mojo::UserAgent;
use WebService::GoogleAPI::Client;
use feature 'say';

my $gapi_client = WebService::GoogleAPI::Client->new( debug => 0,
  gapi_json => 'gapi.json', user => 'YOUR_NAME_HERE@gmail.com');

my $ua = Mojo::UserAgent->new();

my @options = qw/ 0Bw-5xRbys32meGhiQ1dHeE14U1k 0Bw-5xRbys32mc1BKUm0yZmNFZW8 
                  0AA-5xRbys32mUk9PVA /;

@options = 
map { { content => $_, 'Content-Type' => 'application/http' } }
#wrap them each with a Content-Type header
  map { $ua->build_tx( $_->{method}, $_->{path}, json => $_->{options})->req->to_string }
  #make said requests, and stringify them to be added to our batch
     map { $gapi_client->_process_params_for_api_endpoint_and_return_errors($_);$_}
     #process it into a hashref that can be used to make our
     #requests
       map { { api_endpoint_id => 'drive.files.get',
           options => { fileId => $_ , 
           fields => 'parents, name' } 
          } 
      } @options;

#refresh the credentials manually
my $cred  = $gapi_client->ua->auth_storage->get_credentials_for_refresh(
  $gapi_client->ua->user );
my $new_token = $gapi_client->ua->refresh_access_token( $cred)->{ access_token };            $gapi_client->ua->access_token( $new_token );

my $wrapped = $ua->build_tx( POST => 'https://www.googleapis.com/batch/drive/v3',
  $gapi_client->ua->header_with_bearer_auth_token,
  multipart =>  \@options );

my $res = $ua->start($wrapped)->res;

say $res->to_string;

Just excuse the pile of maps, it was easier than having to type out a bunch of the same thing (for me at least). To add more requests, just add more to the qw// list.