Wrapper for Google's Python API.
Project description
googleapiutils2
Utilities for Google's v2 Python API. Currently supports sections of the following resources:
- Drive:
DriveResource,FilesResource,PermissionsResource,RepliesResource,... - Sheets:
SpreadsheetsResource,ValuesResource,... - Geocoding
Quickstart
This project requires Python ^3.10 to run.
Several dependencies are needed, namely the aforesaid Google Python API, but also
Google's oauth library, and requests. Pre-bundled for ease of use are the fairly
monolithic google-api-stubs, which greatly improves the usage experience.
via poetry
Install poetry, then run
poetry install
And you're done.
Overview
The library was written to be consistent with Google's own Python API - just a little
easier to use. Most Drive and Sheets operations are supported using explicit
parameters. But most functions thereof take a **kwargs parameter (used for parameter
forwarding) to allow for the more granular usage of the underlying API.
A note on IDs: anytime a resource ID is needed, one can be provide the actual resource ID, or the URL to the resource. If a URL is provided, this mapping is cached for future use.
Authentication
Before using a Drive or Sheets object, one must first authenticate. This is done via
the google.oauth2 library, creating a Credentials object.
The library supports two methods of authentication:
With a service account, one can programmatically access resources without user input. This is by far the easiest route, but requires a bit of setup.
If not using a service account, the library will attempt to open a browser window to authenticate using the provided credentials. This authentication is cached for future usage (though it does expire on some interval) - so an valid token path is required.
See the get_oauth2_creds function for more information.
Drive
Example: copy a file to a folder.
creds = get_oauth2_creds(config_obj)
drive = Drive(creds)
filename = "Heyy"
file = drive.get_name(filename, parents=[FOLDER_URL])
if file is not None:
drive.delete(file["id"])
file = drive.copy(file_id=FILE_ID, to_filename=filename, to_folder_id=FOLDER_URL)
What the above does is:
- Get the OAuth2 credentials from the
config_objobject (JSON object representing the requisite credentials, see here for more information). - create a
Driveobject thereupon. - Get the file with the given name, and delete it if it exists.
- Copy the file with the given ID to the given folder, and return the new file.
Sheets
Example: update a range of cells in a sheet.
creds = get_oauth2_creds(config_path)
sheets = Sheets(creds)
Sheet1 = SheetsValueRange(sheets, SHEET_ID, sheet_name="Sheet1")
rows = [
{
"Heyy": "99",
}
]
Sheet1[2:3, ...].update(rows)
What the above does is:
- Get the OAuth2 credentials from the
config_pathfile (see here for more information). - create a
Sheetsobject thereupon. - Create a
SheetsValueRangeobject, which is a wrapper around thespreadsheets.valuesAPI. - Update the range
Sheet1!A2:B3with the given rows.
Note the slicing syntax, which will feel quite familiar for any user of Numpy or Pandas.
SheetSlice
A SheetsValueRange object can be sliced in a similar manner to that of a Numpy array.
The syntax is as follows:
slc = Sheet[rows, cols]
Wherein rows and cols are either integers, slices of integers (stride is not
supported), strings (in A1 notation), or ellipses (...).
Note that Google's implementation of A1 notation is 1-indexed; 0 is invalid (e.g., 1
maps to A, 2 to B, etc.)
ix = SheetSlice["Sheet1", 1:3, 2:4] # "Sheet1!B2:D4"
ix = SheetSlice["Sheet1", "A1:B2"] # "Sheet1!A1:B2"
ix = SheetSlice[1:3, 2:4] # "Sheet1!B2:D4"
ix = SheetSlice["A1:B2"] # "Sheet1!A1:B2"
ix = SheetSlice[..., 1:3] # "Sheet1!A1:Z3"
values = {
SheetSlice["A1:B2"]: [
["Heyy", "99"],
["Heyy", "99"],
],
} # "Sheet1!A1:B2" = [["Heyy", "99"], ["Heyy", "99"]]
A SheetSlice can also be used as a key into a SheetsValueRange, or a dictionary (to
use in updating a sheet's range via .update(), for example). Further, a
SheetsValueRange can be sliced in a similar manner to that of a SheetSlice.
Sheet1[2:3, ...].update(rows)
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file googleapiutils2-0.5.8.tar.gz.
File metadata
- Download URL: googleapiutils2-0.5.8.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.2 Darwin/22.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd8796f29a279863bc95ec8b947506714ef3fae9bbe7312d1d21f3a5c9814218
|
|
| MD5 |
8680c12895a34634499822713b51b4f5
|
|
| BLAKE2b-256 |
e6afdbc20083e44c826b4d8e92aa1e1240aa11b4702b35ab4213855d81c03f1a
|
File details
Details for the file googleapiutils2-0.5.8-py3-none-any.whl.
File metadata
- Download URL: googleapiutils2-0.5.8-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.2 Darwin/22.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3613266399ffbd9f9199d263ff198ae93f9ef6f36224718e1151e3b292324fe3
|
|
| MD5 |
f7acc548615cb24b41792354b34a1053
|
|
| BLAKE2b-256 |
37730ad3d0a2de2256739fe67857b4ef1b9d13043ca4b398b75e626485f2fa1f
|