|
| 1 | +.. Patchwork - automated patch tracking system |
| 2 | +.. Copyright (C) The Patchwork Contributors (see CONTRIBUTORS) |
| 3 | +.. |
| 4 | +.. SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | +
|
| 6 | +The REST API |
| 7 | +============ |
| 8 | + |
| 9 | +Patchwork provides a REST API. This API can be used to retrieve and modify |
| 10 | +information about patches, projects and more. |
| 11 | + |
| 12 | +This guide provides an overview of how one can interact with the REST API. For |
| 13 | +detailed information on type and response format of the various resources |
| 14 | +exposed by the API, refer to the auto-generated OpenAPI documentation at:: |
| 15 | + |
| 16 | + https://patchwork.example.com/api/docs |
| 17 | + |
| 18 | +where ``patchwork.example.com`` refers to the URL of your Patchwork instance. |
| 19 | + |
| 20 | +.. versionchanged:: 4.0 |
| 21 | + |
| 22 | + The OpenAPI schema is now auto-generated at runtime and served at |
| 23 | + ``/api/docs``. Static schema files are no longer shipped. |
| 24 | + |
| 25 | +.. versionchanged:: 4.0 |
| 26 | + |
| 27 | + The XML-RPC API has been removed. Use the REST API with clients like |
| 28 | + :program:`git-pw` instead. |
| 29 | + |
| 30 | +Getting Started |
| 31 | +--------------- |
| 32 | + |
| 33 | +The easiest way to start experimenting with the API is to visit the |
| 34 | +auto-generated documentation at ``/api/docs``. |
| 35 | + |
| 36 | +REST APIs run over plain HTTP(S), thus, the API can be interfaced using |
| 37 | +applications or libraries that support this widespread protocol. One such |
| 38 | +application is `curl`_, which can be used to both retrieve and send information |
| 39 | +to the REST API. For example, to get the version of the REST API for a |
| 40 | +Patchwork instance hosted at ``patchwork.example.com``, run: |
| 41 | + |
| 42 | +.. code-block:: shell |
| 43 | +
|
| 44 | + $ curl -s 'https://patchwork.example.com/api/1.4/' | python -m json.tool |
| 45 | + { |
| 46 | + "bundles": "https://patchwork.example.com/api/1.4/bundles/", |
| 47 | + "covers": "https://patchwork.example.com/api/1.4/covers/", |
| 48 | + "events": "https://patchwork.example.com/api/1.4/events/", |
| 49 | + "patches": "https://patchwork.example.com/api/1.4/patches/", |
| 50 | + "people": "https://patchwork.example.com/api/1.4/people/", |
| 51 | + "projects": "https://patchwork.example.com/api/1.4/projects/", |
| 52 | + "series": "https://patchwork.example.com/api/1.4/series/", |
| 53 | + "users": "https://patchwork.example.com/api/1.4/users/" |
| 54 | + } |
| 55 | +
|
| 56 | +Tools like `curl` and libraries like `requests` can be used to build anything |
| 57 | +from small utilities to full-fledged clients targeting the REST API. For an |
| 58 | +overview of existing API clients, refer to :doc:`/usage/clients`. |
| 59 | + |
| 60 | +Versioning |
| 61 | +---------- |
| 62 | + |
| 63 | +By default, all requests will receive the latest version of the API: currently |
| 64 | +``1.4``: |
| 65 | + |
| 66 | +.. code-block:: http |
| 67 | +
|
| 68 | + GET /api HTTP/1.1 |
| 69 | +
|
| 70 | +You should explicitly request this version through the URL to prevent API |
| 71 | +changes breaking your application: |
| 72 | + |
| 73 | +.. code-block:: http |
| 74 | +
|
| 75 | + GET /api/1.4 HTTP/1.1 |
| 76 | +
|
| 77 | +Older API versions will be deprecated and removed over time. |
| 78 | + |
| 79 | +.. csv-table:: |
| 80 | + :header: "API Version", "Supported?" |
| 81 | + |
| 82 | + 1.0, yes |
| 83 | + 1.1, yes |
| 84 | + 1.2, yes |
| 85 | + 1.3, yes |
| 86 | + 1.4, yes |
| 87 | + |
| 88 | +Schema |
| 89 | +------ |
| 90 | + |
| 91 | +Responses are returned as JSON. Blank fields are returned as ``null``, rather |
| 92 | +than being omitted. Timestamps use the ISO 8601 format, times are in UTC:: |
| 93 | + |
| 94 | + YYYY-MM-DDTHH:MM:SSZ |
| 95 | + |
| 96 | +Requests should use either query parameters or form-data, depending on the |
| 97 | +method. |
| 98 | + |
| 99 | +The auto-generated OpenAPI schema is available at ``/api/docs`` and describes |
| 100 | +all endpoints, parameters, and response formats. |
| 101 | + |
| 102 | +Parameters |
| 103 | +---------- |
| 104 | + |
| 105 | +For ``GET`` requests, parameters should be passed as HTTP query string |
| 106 | +parameters: |
| 107 | + |
| 108 | +.. code-block:: shell |
| 109 | +
|
| 110 | + $ curl 'https://patchwork.example.com/api/patches?state=under-review' |
| 111 | +
|
| 112 | +For ``POST`` and ``PATCH`` requests, parameters should be encoded as JSON with |
| 113 | +a ``Content-Type`` of ``application/json``: |
| 114 | + |
| 115 | +.. code-block:: shell |
| 116 | +
|
| 117 | + $ curl -X PATCH \ |
| 118 | + --header "Content-Type: application/json" \ |
| 119 | + --data '{"state":"under-review"}' \ |
| 120 | + 'https://patchwork.example.com/api/patches/123/' |
| 121 | +
|
| 122 | +Authentication |
| 123 | +-------------- |
| 124 | + |
| 125 | +Patchwork supports authentication using basic authentication (username and |
| 126 | +password) or token authentication (recommended). |
| 127 | + |
| 128 | +To authenticate with a token, first obtain one from your profile page, then:: |
| 129 | + |
| 130 | + $ curl -H "Authorization: Token ${token}" \ |
| 131 | + 'https://patchwork.example.com/api/' |
| 132 | + |
| 133 | +Not all resources require authentication. Those that do will return |
| 134 | +``404 (Not Found)`` if authentication is not provided. |
| 135 | + |
| 136 | +Pagination |
| 137 | +---------- |
| 138 | + |
| 139 | +Requests that return multiple items are paginated by 30 items by default. You |
| 140 | +can change page using the ``?page`` parameter and set custom page sizes up to |
| 141 | +250 using the ``?per_page`` parameter: |
| 142 | + |
| 143 | +.. code-block:: shell |
| 144 | +
|
| 145 | + $ curl 'https://patchwork.example.com/api/patches?page=2&per_page=100' |
| 146 | +
|
| 147 | +The ``Link`` header includes pagination information:: |
| 148 | + |
| 149 | + Link: <https://patchwork.example.com/api/patches?page=3&per_page=100>; rel="next", |
| 150 | + <https://patchwork.example.com/api/patches?page=50&per_page=100>; rel="last" |
| 151 | + |
| 152 | + |
| 153 | +.. _curl: https://curl.haxx.se/ |
0 commit comments