RESTful Python Client for Google Cloud
Project description
This project is a collection of Google Cloud client libraries for the REST-only APIs; its raison d’etre is to implement a simple CloudTasks API as well as a more abstract TaskManager.
Installation
$ pip install --upgrade gcloud-rest
Usage
This project currently exposes interfaces to Storage and CloudTasks.
Storage (see bucket.py):
from gcloud.rest.storage import Bucket
bk = Bucket('my-project', 'bucket-name')
# list all objects
objects_in_bucket = bk.list_objects()
# list objects with prefix
objects_in_bucket = bk.list_objects(prefix='in/subdir/')
object = bk.download('object-name')
object_contents = bk.download_as_string('object-name')
TaskQueue (for CloudTasks, see queue.py):
from gcloud.rest.taskqueue import TaskQueue
from gcloud.rest.taskqueue import decode
from gcloud.rest.taskqueue import encode
tq = TaskQueue('my-project', 'taskqueue-name')
# create a task
payload = 'aardvarks-are-awesome'
tq.insert(encode(payload))
# list and get tasks
tasks = tq.list()
random_task = tasks.get('tasks')[42]
random_task_body = tq.get(random_task['name'])
# lease, renew, and ack/cancel/delete tasks
task_leases = tq.lease(num_tasks=3)
tasks = task_lease.get('tasks')
# assert len(tasks) <= 3
for task in tasks:
payload = decode(task['pullMessage']['payload']).decode()
# you'll need to renew the task if you take longer than
# task['scheduleTime'] to process it
tq.renew(task)
# do something with payload
if success:
tq.ack(task)
elif temporary_failure:
tq.cancel(task)
elif permanent_failure:
tq.delete(task['name'])
TaskManager (for CloudTasks, see manager.py):
from gcloud.rest.taskqueue import FailFastError
from gcloud.rest.taskqueue import TaskManager
def worker_method(payloads):
for task in payloads:
# do something with the task
if success:
yield 'anything'
elif temporary_failure:
yield Exception('insert message here')
elif permanent_failure:
yeild FailFastError('insert message here')
tm = TaskManager('my-project', 'taskqueue-name', worker_method)
tm.find_tasks_forever()
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
gcloud-rest-1.3.1.tar.gz
(11.5 kB
view hashes)
Built Distribution
Close
Hashes for gcloud_rest-1.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa7b7783557aba69ed094e7656f01b6fd132c7481fcc16c1a95b59aab30130c3 |
|
MD5 | 249070cdbfe918368a60a2ae2b119fc1 |
|
BLAKE2b-256 | edccbd7611e6d9c6d1d2de41081703cdca1c39311b699ddfb7c9bd0a45feb14c |