Search code examples
pythongoogle-apigoogle-drive-apigoogle-api-python-clientpydrive

Optional query parameters with InsertPermission() in PyDrive


Is it possible to send a value for optional query parameters using InsertPermission() in PyDrive? The PyDrive documentation seems to ignore how to handle toggling optional parameters like sendNotificationEmails.

I need to add permissions to files for specific users, not anyone, without sending a notification email, and sendNotificationEmails is set to True by default.

I've considered modifying files.py's definition for the function to the following, but I'm not sure if this will work and haven't edited a library before:

def InsertPermission(self, new_permission):
  """Insert a new permission. Re-fetches all permissions after call.

  :param new_permission: The new permission to insert, please see the official Google Drive API guide on permissions.insert for details.

  :type new_permission: object

  :return: The permission object.
  :rtype: object
  """
  file_id = self.metadata.get('id') or self['id']
  try:
    permission = self.auth.service.permissions().insert( fileId=file_id, body=new_permission, sendNotificationEmails=False).execute(http=self.http)
  except errors.HttpError as error:
    raise ApiRequestError(error)
  else:
    self.GetPermissions()  # Update permissions field.

  return permission

Solution

  • Specifying optional parameters is now possible as additional parameters to the InsertPermission() function like so:

    file1.InsertPermission({'type': 'user',
                            'value': 'insert@email.here',
                            'role': 'reader'}, sendNotificationEmails=False)
    

    Please see the official docs here for all parameters available.

    Note: You will currently (Apr 2017) need to install the development version of the library for this feature. This version of PyDrive can be installed like so:

    pip install git+https://github.com/googledrive/PyDrive.git@development#egg=PyDrive