# Taskcluster Client for Python
[![Build Status](https://travis-ci.org/taskcluster/taskcluster-client.py.svg?branch=master)](https://travis-ci.org/taskcluster/taskcluster-client.py)
This is a library used to interact with Taskcluster within Python programs. It
presents the entire REST API to consumers as well as being able to generate
URLs Signed by Hawk credentials. It can also generate routing keys for
listening to pulse messages from Taskcluster.
The library builds the REST API methods from the Taskcluster API definitions provided in the [Taskcluster repository](https://github.com/taskcluster/taskcluster).
## Generating Temporary Credentials
If you have non-temporary taskcluster credentials you can generate a set of
temporary credentials as follows. Notice that the credentials cannot last more
than 31 days, and you can only revoke them by revoking the credentials that was
used to issue them (this takes up to one hour).
It is not the responsibility of the caller to apply any clock drift adjustment
to the start or expiry time - this is handled by the auth service directly.
```python
import datetime
start = datetime.datetime.now()
expiry = start + datetime.timedelta(0,60)
scopes = ['ScopeA', 'ScopeB']
name = 'foo'
credentials = taskcluster.createTemporaryCredentials(
# issuing clientId
clientId,
# issuing accessToken
accessToken,
# Validity of temporary credentials starts here, in timestamp
start,
# Expiration of temporary credentials, in timestamp
expiry,
# Scopes to grant the temporary credentials
scopes,
# credential name (optional)
name
)
```
You cannot use temporary credentials to issue new temporary credentials. You
must have `auth:create-client:<name>` to create a named temporary credential,
but unnamed temporary credentials can be created regardless of your scopes.
## API Documentation
The REST API methods are documented in the [reference docs](https://docs.taskcluster.net/docs/reference).
## Query-String arguments
Query string arguments are now supported. In order to use them, you can call
a method like this:
```python
queue.listTaskGroup('JzTGxwxhQ76_Tt1dxkaG5g', query={'continuationToken': outcome.get('continuationToken')})
```
These query-string arguments are only supported using this calling convention
## Sync vs Async
The objects under `taskcluster` (e.g., `taskcluster.Queue`) are
python2-compatible and operate synchronously.
The objects under `taskcluster.aio` (e.g., `taskcluster.aio.Queue`) require
`python>=3.6`. The async objects use asyncio coroutines for concurrency; this
allows us to put I/O operations in the background, so operations that require
the cpu can happen sooner. Given dozens of operations that can run concurrently
(e.g., cancelling a medium-to-large task graph), this can result in significant
performance improvements. The code would look something like
```python
#!/usr/bin/env python
import aiohttp
import asyncio
from taskcluster.aio import Auth
async def do_ping():
with aiohttp.ClientSession() as session:
a = Auth(session=session)
print(await a.ping())
loop = asyncio.get_event_loop()
loop.run_until_complete(do_ping())
```
Other async code examples are available [here](#methods-contained-in-the-client-library).
Here's a slide deck for an [introduction to async python](https://gitpitch.com/escapewindow/slides-sf-2017/async-python).
## Usage
* Here's a simple command:
```python
import taskcluster
index = taskcluster.Index({
'rootUrl': 'https://tc.example.com',
'credentials': {'clientId': 'id', 'accessToken': 'accessToken'},
})
index.ping()
```
* There are four calling conventions for methods:
```python
client.method(v1, v1, payload)
client.method(payload, k1=v1, k2=v2)
client.method(payload=payload, query=query, params={k1: v1, k2: v2})
client.method(v1, v2, payload=payload, query=query)
```
* Options for the topic exchange methods can be in the form of either a single
dictionary argument or keyword arguments. Only one form is allowed
```python
from taskcluster import client
qEvt = client.QueueEvents({rootUrl: 'https://tc.example.com'})
# The following calls are equivalent
qEvt.taskCompleted({'taskId': 'atask'})
qEvt.taskCompleted(taskId='atask')
```
## Generating URLs
It is often necessary to generate the URL for an API method without actually calling the method.
To do so, use `buildUrl` or, for an API method that requires authentication, `buildSignedUrl`.
```python
import taskcluster
index = taskcluster.Index(taskcluster.optionsFromEnvironment())
print(index.buildUrl('findTask', 'builds.v1.latest'))
secrets = taskcluster.Secrets(taskcluster.optionsFromEnvironment())
print(secret.buildSignedUrl('get', 'my-secret'))
```
Note that signed URLs are time-limited; the expiration can be set with the `signedUrlExpiration` option to the client constructor, or with the `expiration` keyword arguement to `buildSignedUrl`, both given in seconds.
## Root URL
This client requires a `rootUrl` argument to identify the Taskcluster
deployment to talk to. As of this writing, the production cluster has rootUrl
`https://taskcluster.net`.
## Environment Variables
As of version 6.0.0, the client does not read the standard `TASKCLUSTER_…`
environment variables automatically. To fetch their values explicitly, use
`taskcluster.optionsFromEnvironment()`:
```python
auth = taskcluster.Auth(taskcluster.optionsFromEnvironment())
```
## Pagination
There are two ways to accomplish pagination easily with the python client. The first is
to implement pagination in your code:
```python
import taskcluster
queue = taskcluster.Queue({'rootUrl': 'https://tc.example.com'})
i = 0
tasks = 0
outcome = queue.listTaskGroup('JzTGxwxhQ76_Tt1dxkaG5g')
while outcome.get('continuationToken'):
print('Response %d gave us %d more tasks' % (i, len(outcome['tasks'])))
if outcome.get('continuationToken'):
outcome = queue.listTaskGroup('JzTGxwxhQ76_Tt1dxkaG5g', query={'continuationToken': outcome.get('continuationToken')})
i += 1
tasks += len(outcome.get('tasks', []))
print('Task Group %s has %d tasks' % (outcome['taskGroupId'], tasks))
```
There's also an experimental feature to support built in automatic pagination
in the sync client. This feature allows passing a callback as the
'paginationHandler' keyword-argument. This function will be passed the
response body of the API method as its sole positional arugment.
This example of the built in pagination shows how a list of tasks could be
built and then counted:
```python
import taskcluster
queue = taskcluster.Queue({'rootUrl': 'https://tc.example.com'})
responses = []
def handle_page(y):
print("%d tasks fetched" % len(y.get('tasks', [])))
responses.append(y)
queue.listTaskGroup('JzTGxwxhQ76_Tt1dxkaG5g', paginationHandler=handle_page)
tasks = 0
for response in responses:
tasks += len(response.get('tasks', []))
print("%d requests fetch %d tasks" % (len(responses), tasks))
```
## Logging
Logging is set up in `taskcluster/__init__.py`. If the special
`DEBUG_TASKCLUSTER_CLIENT` environment variable is set, the `__init__.py`
module will set the `logging` module's level for its logger to `logging.DEBUG`
and if there are no existing handlers, add a `logging.StreamHandler()`
instance. This is meant to assist those who do not wish to bother figuring out
how to configure the python logging module but do want debug messages
## SlugIDs
To generate slugIds (Taskcluster's client-generated unique IDs), use
`taskcluster.slugId()`, which will return a unique slugId on each call.
In some cases it is useful to be able to create a mapping from names to
slugIds, with the ability to generate the same slugId multiple times.
The `taskcluster.stableSlugId()` function returns a callable that does
just this.
```python
gen = taskcluster.stableSlugId()
sometask = gen('sometask')
assert gen('sometas
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共92个文件
py:83个
txt:4个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:taskcluster-24.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
taskcluster-24.1.0.tar.gz (92个子文件)
taskcluster-24.1.0
PKG-INFO 154KB
taskcluster
queue.py 79B
authevents.py 84B
helper.py 5KB
auth.py 78B
hooksevents.py 85B
workermanager.py 87B
utils.py 11KB
githubevents.py 86B
notify.py 80B
queueevents.py 85B
client.py 26KB
ec2manager.py 84B
__init__.py 622B
notifyevents.py 86B
secrets.py 81B
index.py 79B
generated
queue.py 40KB
authevents.py 6KB
auth.py 26KB
hooksevents.py 4KB
workermanager.py 11KB
githubevents.py 8KB
_client_importer.py 794B
notify.py 6KB
queueevents.py 27KB
__init__.py 0B
notifyevents.py 3KB
secrets.py 4KB
index.py 6KB
workermanagerevents.py 3KB
hooks.py 9KB
github.py 6KB
aio
queue.py 41KB
authevents.py 6KB
auth.py 27KB
hooksevents.py 4KB
workermanager.py 11KB
githubevents.py 8KB
_client_importer.py 794B
notify.py 6KB
queueevents.py 27KB
__init__.py 0B
notifyevents.py 3KB
secrets.py 4KB
index.py 6KB
workermanagerevents.py 3KB
hooks.py 9KB
github.py 6KB
purgecache.py 4KB
purgecache.py 4KB
workermanagerevents.py 93B
login.py 79B
hooks.py 79B
github.py 80B
awsprovisioner.py 88B
aio
queue.py 84B
authevents.py 89B
auth.py 83B
hooksevents.py 90B
workermanager.py 92B
githubevents.py 91B
notify.py 85B
queueevents.py 90B
ec2manager.py 89B
__init__.py 483B
asyncutils.py 4KB
notifyevents.py 91B
secrets.py 86B
index.py 84B
asyncclient.py 11KB
workermanagerevents.py 98B
login.py 84B
hooks.py 84B
github.py 85B
awsprovisioner.py 93B
purgecache.py 89B
exceptions.py 1KB
purgecache.py 84B
test
test_utils.py 14KB
test_imports.py 2KB
test_async.py 2KB
test_client.py 32KB
taskcluster.egg-info
PKG-INFO 154KB
requires.txt 234B
not-zip-safe 1B
SOURCES.txt 3KB
top_level.txt 12B
dependency_links.txt 1B
setup.cfg 85B
setup.py 3KB
README.md 123KB
共 92 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功