2019-01-22 19:27:56 +03:00
|
|
|
# REST API
|
2016-05-10 17:11:50 +03:00
|
|
|
|
|
|
|
Treeherder provides a REST API which can be used to query for all the
|
2018-02-16 13:21:21 +03:00
|
|
|
push, job, and performance data it stores internally. For a browsable
|
|
|
|
interface, see:
|
2018-07-27 19:41:11 +03:00
|
|
|
<https://treeherder.mozilla.org/docs/>
|
2016-05-10 17:13:34 +03:00
|
|
|
|
2019-01-22 19:27:56 +03:00
|
|
|
## Profiling API endpoint performance
|
2018-10-09 22:38:30 +03:00
|
|
|
|
2019-01-22 19:27:56 +03:00
|
|
|
On our development (vagrant) instance we have [django-debug-toolbar](http://django-debug-toolbar.readthedocs.io/) installed, which can give
|
2018-10-09 22:38:30 +03:00
|
|
|
information on exactly what SQL is run to generate individual API
|
|
|
|
endpoints. Just navigate to an endpoint
|
|
|
|
(example: <http://localhost:8000/api/repository/>) and
|
|
|
|
you should see the toolbar to your right.
|
2016-05-10 17:13:34 +03:00
|
|
|
|
2019-01-22 19:27:56 +03:00
|
|
|
## Python Client
|
2016-05-10 17:13:34 +03:00
|
|
|
|
|
|
|
We provide a library, called treeherder-client, to simplify
|
|
|
|
interacting with the REST API. It is maintained inside the
|
2016-05-30 15:54:16 +03:00
|
|
|
Treeherder repository, but you can install your own copy from PyPI
|
2016-05-10 17:13:34 +03:00
|
|
|
using pip:
|
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
```bash
|
|
|
|
pip install treeherder-client
|
|
|
|
```
|
2016-05-10 17:15:18 +03:00
|
|
|
|
2016-07-27 23:15:31 +03:00
|
|
|
It will install a module called `thclient` that you can access, for example:
|
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
```python
|
|
|
|
from thclient import TreeherderClient
|
|
|
|
```
|
2016-07-27 23:15:31 +03:00
|
|
|
|
2016-06-21 21:15:38 +03:00
|
|
|
By default the production Treeherder API will be used, however this can be
|
|
|
|
overridden by passing a `server_url` argument to the `TreeherderClient`
|
|
|
|
constructor:
|
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
```python
|
|
|
|
# Treeherder production
|
|
|
|
client = TreeherderClient()
|
2016-06-21 21:15:38 +03:00
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
# Treeherder stage
|
|
|
|
client = TreeherderClient(server_url='https://treeherder.allizom.org')
|
2016-06-21 21:15:38 +03:00
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
# Local vagrant instance
|
|
|
|
client = TreeherderClient(server_url='http://localhost:8000')
|
|
|
|
```
|
2016-06-21 21:15:38 +03:00
|
|
|
|
2016-05-30 15:56:26 +03:00
|
|
|
When using the Python client, don't forget to set up logging in the
|
|
|
|
caller so that any API error messages are output, like so:
|
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
```python
|
|
|
|
import logging
|
2016-05-30 15:56:26 +03:00
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
logging.basicConfig()
|
|
|
|
```
|
2016-05-30 15:56:26 +03:00
|
|
|
|
2019-01-22 19:27:56 +03:00
|
|
|
For verbose output, pass `level=logging.DEBUG` to `basicConfig()`.
|
2016-05-30 15:56:26 +03:00
|
|
|
|
2019-01-22 19:27:56 +03:00
|
|
|
## User Agents
|
2016-05-09 15:04:46 +03:00
|
|
|
|
|
|
|
When interacting with Treeherder's API, you must set an appropriate
|
2019-01-22 19:27:56 +03:00
|
|
|
`User Agent` header (rather than relying on the defaults of your
|
2016-05-09 15:04:46 +03:00
|
|
|
language/library) so that we can more easily track API feature usage,
|
|
|
|
as well as accidental abuse. Default scripting User Agents will receive
|
2018-07-27 19:41:11 +03:00
|
|
|
an HTTP 403 response (see [bug 1230222] for more details).
|
2016-05-09 15:04:46 +03:00
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
If you are using the [Python Client](#python-client), an appropriate User Agent
|
2016-05-09 15:04:46 +03:00
|
|
|
is set for you. When using the Python requests library, the User Agent
|
|
|
|
can be set like so:
|
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
```python
|
|
|
|
r = requests.get(url, headers={'User-Agent': ...})
|
|
|
|
```
|
2016-05-09 15:04:46 +03:00
|
|
|
|
2018-07-27 19:41:11 +03:00
|
|
|
[bug 1230222]: https://bugzilla.mozilla.org/show_bug.cgi?id=1230222
|