autodrive.connection
¶
Module Contents¶
Classes¶
Use FileUploads when you need more complicated file upload instructions. |
|
Provides a connection to the Google Drive api, and methods to send requests |
|
Provides a connection to the Google Sheets api, and methods to send requests |
- class autodrive.connection.FileUpload(path, to_folder_id=None, convert=False)¶
Use FileUploads when you need more complicated file upload instructions.
- Parameters
path (Path, str) – The pathlike to the file.
to_folder_id (str, optional) – A folder id, if you want to upload this file to a specific folder in Google Drive. Defaults to None.
convert (bool, optional) – Set to True if you want to convert the file to a Google Drive format. The format will be automatically determined based on Google Drive’s rules for conversion. Defaults to False.
- property do_conv(self)¶
- Return type
bool
- class autodrive.connection.DriveConnection(*, auth_config=None, api_version='v3')¶
Provides a connection to the Google Drive api, and methods to send requests to it.
This class is documented primarily for reference, if you want to connect to Google Drive, it’s easier to just use a
Drive
instance.- Parameters
auth_config (AuthConfig, optional) – Optional custom AuthConfig object, defaults to None.
api_version (str, optional) – The version of the Drive api to connect to, defaults to “v3”.
- google_obj_types¶
- get_import_formats(self)¶
- Returns
- The mapping between standard MIMEtypes and the
MIMEtypes for Google Drive’s types (Docs, Sheets, etc) as provided by the Google Drive API.
- Return type
Dict[str, str]
- find_object(self, obj_name, obj_type=None, shared_drive_id=None)¶
Searches for a Google Drive Object via the connected api.
- Parameters
obj_name (str) – The name of the object, or part of its name.
obj_type (Literal["sheet", "folder", "file"], optional) – The type of object to restrict the search to.
shared_drive_id (str, optional) – The id of a Shared Drive to search within, if desired, defaults to None.
- Returns
A list of object properties, if any matches are found.
- Return type
List[Dict[str, Any]]
- create_object(self, obj_name, obj_type, parent_id=None)¶
Creates a file or folder via the Google Drive connection.
- Parameters
obj_name (str) – The desired name of the object to create.
obj_type (Literal["sheet", "folder"]) – The type of object to create.
parent_id (str, optional) – The id of the folder or shared drive to create the object within, defaults to None.
- Returns
The new id of the created object.
- Return type
str
- delete_object(self, object_id)¶
Deletes the passed Google object id from the connected Google Drive.
- Parameters
object_id (str) – A Google object id.
- Return type
None
- upload_files(self, *filepaths)¶
Uploads files to the root drive or to a folder.
Note
This method will cause a request to be posted to the relevant Google API immediately.
- Parameters
*filepaths (Path | str | FileUpload) – An arbitrary number of Path objects, path strings, or FileUpload objects (for more complex cases).
filepaths (Path | str | FileUpload) –
- Returns
- The names of the uploaded files and their new ids in
Google Drive.
- Return type
Dict[str, str]
- detect_conv_format(self, p)¶
Detects the MIMEtype for the passed filepath and determines its corresponding Google Drive format.
- Parameters
p (Path) – Any filepath.
- Raises
ValueError – If the MIMEtype cannot be determined from the path at all.
ValueError – If the MIMEtype cannot be converted to a Google Drive format MIMEtype.
- Returns
The Google Drive format MIMEtype for the file.
- Return type
str
- property auth(self)¶
- Returns
The AuthConfig passed to this Connection object, or the AuthConfig that was generated automatically upon this Connection’s instantiation.
- Return type
- static get_creds_from_env()¶
Checks if necessary credential and token information has been specified in environment variables and uses that information to generate a Credentials object. Can be used to bypass needing credentials files saved locally.
- Returns
A prepped Credentials object, ready to be slotted into more specific connections, if appropriate environment variables were found, otherwise None.
- Return type
Credentials, optional
- class autodrive.connection.SheetsConnection(*, auth_config=None, api_version='v4')¶
Provides a connection to the Google Sheets api, and methods to send requests to it.
This class is documented primarily for reference, if you want to connect to Google Sheets, it’s easier to just use a View.
- Parameters
auth_config (AuthConfig, optional) – Optional custom AuthConfig object, defaults to None.
api_version (str, optional) – The version of the Sheets api to connect to, defaults to “v4”.
- google_obj_types¶
- execute_requests(self, spreadsheet_id, requests)¶
Sends the passed list of request dictionaries to the Sheets api to be applied to the spreadsheet_id via batch update.
- Parameters
spreadsheet_id (str) – The id of the Google Sheet to update.
requests (List[Dict[str, Any]]) – A list of dictionaries formatted as requests.
- Returns
The resulting response from the Sheets api as a dictionary.
- Return type
Dict[str, Any]
- get_properties(self, spreadsheet_id)¶
Gets the metadata properties of the indicated Google Sheet.
- Parameters
spreadsheet_id (str) – The id of the Google Sheet to collect properties from.
- Returns
A dictionary of the Google Sheet’s properties.
- Return type
Dict[str, Any]
- get_data(self, spreadsheet_id, ranges=None)¶
Collects data from cells in the passed spreadsheet.
- Parameters
spreadsheet_id (str) – The id of the Google Sheet to collect data from.
ranges (List[str], optional) – A list of range strings (e.g. Sheet1!A1:C3), defaults to None, which prompts get_data to fetch all data from all cells.
- Returns
The collected data from the spreadsheet, raw and unparsed.
- Return type
Dict[str, Any]
- property auth(self)¶
- Returns
The AuthConfig passed to this Connection object, or the AuthConfig that was generated automatically upon this Connection’s instantiation.
- Return type
- static get_creds_from_env()¶
Checks if necessary credential and token information has been specified in environment variables and uses that information to generate a Credentials object. Can be used to bypass needing credentials files saved locally.
- Returns
A prepped Credentials object, ready to be slotted into more specific connections, if appropriate environment variables were found, otherwise None.
- Return type
Credentials, optional