|
6 | 6 | https://creativecommons.org/licenses/by/4.0/ |
7 | 7 | --> |
8 | 8 |
|
9 | | - |
10 | 9 | This document is intended to capture proposed updates to the Grid REST API. |
11 | 10 | This document is a work-in-progress and proposed changes may or may not be |
12 | 11 | implemented as project requirements evolve. |
13 | 12 |
|
14 | | -* [Future REST API Reference](/community/planning/rest_api/api/) |
| 13 | +## Planned functionality |
| 14 | + |
| 15 | +Mainly, the REST API will be updated to support more robust batch handling. |
| 16 | +The proposed functionality will accept transactions in JSON, the request body, |
| 17 | +while arguments for the batch can be submitted in the request headers. Arguments |
| 18 | +for a batch may include a service ID, if Grid is running with Splinter, or a |
| 19 | +user-created identifier for the batch submitted. More specific headers pertaining |
| 20 | +to batch arguments may be added. The array of JSON arguments will be converted |
| 21 | +into the appropriate transactions and signed. As Grid will support batch |
| 22 | +tracking, the REST API will not directly submit batches. Instead, once a batch |
| 23 | +has been signed and built, it will be stored in Grid's database. Other |
| 24 | +components will handle the batch for the rest of its lifecycle and the database |
| 25 | +entry will reflect updates. |
| 26 | + |
| 27 | +The REST API is provided by two components, gridd and griddle. Griddle is a |
| 28 | +client component that will act as both a proxy and signing utility. Grid may |
| 29 | +also perform transaction and batch signing, if configured at run-time. Griddle |
| 30 | +will communicate with gridd over REST API and may be run from a separate |
| 31 | +security domain. Access to the internet is not required by Griddle, which allows |
| 32 | +it to handle sensitive data, such as private key files, without worry this data |
| 33 | +may become compromised. As griddle does not have access to gridd's database, it |
| 34 | +will also proxy batches it creates to gridd's `/batches` endpoint to be stored |
| 35 | +in the database. Database support may be added to griddle in the future. |
| 36 | + |
| 37 | +All smart contract actions that may be performed in Grid will be represented by |
| 38 | +`POST` endpoints. An example of a request for the `POST /organizations` endpoint |
| 39 | +is below: |
| 40 | + |
| 41 | +``` |
| 42 | +POST /locations |
| 43 | +Content-Type: application/json+grid-simple |
| 44 | +Headers: |
| 45 | + data_change_id: <User-defined batch ID> |
| 46 | + // Batch arguments |
| 47 | +Body: |
| 48 | +[ |
| 49 | + { |
| 50 | + "namespace": "GS1", |
| 51 | + "location_id": "1234", |
| 52 | + "properties": [ |
| 53 | + "..." |
| 54 | + ], |
| 55 | + }, |
| 56 | +] |
| 57 | +``` |
| 58 | + |
| 59 | +This JSON is converted by the endpoint into it's respective transaction type, |
| 60 | +the action of this example would be `CreateLocation`. There may be multiple |
| 61 | +transactions in the JSON body. Endpoints receiving these requests must account |
| 62 | +for the list of transactions when deserializing the request body. More complex |
| 63 | +batches may include transactions with different action types. The `/batches` |
| 64 | +endpoint allows for this situation. An example request to this endpoint follows: |
| 65 | + |
| 66 | +``` |
| 67 | +POST /batches |
| 68 | +Content-Type: application/json |
| 69 | +Headers: |
| 70 | + data_change_id: <User-defined batch ID> |
| 71 | + // Batch arguments |
| 72 | +Body: |
| 73 | +[ |
| 74 | + { |
| 75 | + "target": "POST /locations" |
| 76 | + "namespace": "GS1", |
| 77 | + "location_id": "1234", |
| 78 | + "properties": [ |
| 79 | + "..." |
| 80 | + ], |
| 81 | + }, |
| 82 | + { |
| 83 | + "target": "PUT /locations" |
| 84 | + "namespace": "GS1", |
| 85 | + "location_id": "5678", |
| 86 | + "properties": [ |
| 87 | + "..." |
| 88 | + ], |
| 89 | + }, |
| 90 | +] |
| 91 | +``` |
| 92 | + |
| 93 | +This request will create a batch containing two transactions, one for creating |
| 94 | +a location and one for updating another location. The endpoint handling this |
| 95 | +request will deserialize the request body into transactions, depending on |
| 96 | +value of the `target` field. If the endpoint is unable to recognize the targeted |
| 97 | +transaction, it will return `400 Bad Request`. A signer object is passed to |
| 98 | +each endpoint and is required to build the batch. If the endpoint does not have |
| 99 | +access to a signer, it will return `400 Bad Request`. |
| 100 | + |
| 101 | +Once a batch has been built from the request body and signed, it can be stored |
| 102 | +in gridd's database. As endpoints may be provided by both gridd and griddle, |
| 103 | +the signed batch should be handled by an implementation-specific object. |
| 104 | +Endpoints have access to a trait object, `BatchSubmissionHandler`, which |
| 105 | +submits the batch once it is signed. This trait implements a `submit_batches` |
| 106 | +method, which takes in pertinent batch information to store. If an endpoint is |
| 107 | +provided by griddle, the submission handler is required to send the batch |
| 108 | +to gridd to be stored. An endpoint provided by gridd is able to directly store |
| 109 | +the batch. |
| 110 | + |
| 111 | +If a batch is submitted successfully, the endpoint will respond with |
| 112 | +status code `202 Accepted` and a JSON body of the identifiers for the batch |
| 113 | +stored. An example response body to a request that included a Splinter service |
| 114 | +ID and a user-defined ID. |
| 115 | + |
| 116 | +``` |
| 117 | +{ |
| 118 | + [ |
| 119 | + { |
| 120 | + "dlt_batch_id": <Batch header signature>, |
| 121 | + "data_change_id": <User-defined ID>, |
| 122 | + "service_id": <Splinter service ID>, |
| 123 | + }, |
| 124 | + ] |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +If Grid is not running with Splinter services, the `service_id` field will not |
| 129 | +be returned. Similarly, if no `data_change_id` was submitted in the original |
| 130 | +request, it will also not be returned. |
| 131 | + |
| 132 | +If a batch is not submitted successfully, the endpoint will respond with a |
| 133 | +status code to describe the error and a JSON body with any additional data. For |
| 134 | +example, an error is returned at the point griddle attempts to send a batch |
| 135 | +to gridd, a `SendError` with a message describing what went wrong. The griddle |
| 136 | +endpoint may return a `402 Bad Gateway` if it is unable to connect to gridd. |
| 137 | + |
| 138 | +All previous design documents for the REST API are linked in the following |
| 139 | +section. |
15 | 140 |
|
16 | 141 | ## Changes |
17 | 142 |
|
|
0 commit comments