Skip to content

Commit 21ae604

Browse files
committed
Move subrequests to docs/subrequests.
1 parent 69eaf23 commit 21ae604

File tree

3 files changed

+98
-94
lines changed

3 files changed

+98
-94
lines changed

docs/client_2x.md

+1-94
Original file line numberDiff line numberDiff line change
@@ -250,97 +250,4 @@ response = farm_client.term.send('plant_type', {"attributes": {"name": "Corn"}})
250250

251251
```python
252252
farm_client.term.delete('plant_type', "a260441b-2553-4715-83ee-3ac08a6b85f0")
253-
```
254-
255-
## Subrequests
256-
257-
farmOS.py supports the [Subrequests](https://www.drupal.org/project/subrequests) module which can optionally be
258-
installed on the farmOS server.
259-
260-
Subrequests allows multiple requests to be defined in a "blueprint" and sent to the server in a single POST request.
261-
The blueprint can define sequential requests so that the response of one request can be embedded into the body of a
262-
later request. See the subrequests [blueprint specification](https://git.drupalcode.org/project/subrequests/blob/8.x-2.x/SPECIFICATION.md)
263-
for more documentation.
264-
265-
farmOS.py provides some additional features to help use subrequests:
266-
- The default response format is set to `json` by appending a `?_format=json` query parameter automatically. JSON is
267-
much easier to parse than subrequest's default `html` format, but HTML can be requested by specifying `format='html'`.
268-
- Unless otherwise provided, the `Accept` and `Content-Type` headers for each sub-request will be set to
269-
`application/vnd.api+json` for standard JSONAPI requests.
270-
- An `endpoint` can be provided instead of the full `uri` for each sub-request. farmOS.py will build the full `uri`
271-
from the `hostname` already configured with the client.
272-
- The sub-request `body` can be provided as an object that farmOS.py will serialize into a JSON object string.
273-
- Blueprints are validated using Pydantic models. These models can also be used to build individual requests to include
274-
in a blueprint.
275-
276-
An example that creates a new asset followed by a new log that references the asset:
277-
278-
```python
279-
from farmOS import farmOS
280-
from farmOS.subrequests import Action, Subrequest, SubrequestsBlueprint, Format
281-
282-
client = farmOS("http://localhost", scope="farm_manager", version=2)
283-
client.authorize('username', 'password')
284-
285-
plant_type = {
286-
"data": {
287-
"type": "taxonomy_term--plant_type",
288-
"attributes": {
289-
"name": "New plant type"
290-
}
291-
}
292-
}
293-
294-
new_plant_type = Subrequest(action=Action.create, requestId="create-plant-type", endpoint="api/taxonomy_term/plant_type", body=plant_type)
295-
296-
plant = {
297-
"data": {
298-
"type": "asset--plant",
299-
"attributes": {
300-
"name": "My new plant",
301-
"description": "Created in the first request.",
302-
},
303-
"relationships": {
304-
"plant_type": {
305-
"data": [
306-
{
307-
"type": "taxonomy_term--plant_type",
308-
"id": "{{create-plant-type.body@$.data.id}}"
309-
}
310-
]
311-
}
312-
}
313-
}
314-
}
315-
new_asset = Subrequest(action=Action.create, requestId="create-asset", waitFor=["create-plant-type"], endpoint="api/asset/plant", body=plant)
316-
317-
log = {
318-
"data": {
319-
"type": "log--seeding",
320-
"attributes": {
321-
"name": "Seeding my new plant",
322-
"notes": "Created in the second request.",
323-
},
324-
"relationships": {
325-
"asset": {
326-
"data": [
327-
{
328-
"type": "asset--plant",
329-
"id": "{{create-asset.body@$.data.id}}"
330-
}
331-
]
332-
}
333-
}
334-
}
335-
}
336-
new_log = Subrequest(action=Action.create, requestId="create-log", waitFor=["create-asset"], endpoint="api/log/seeding", body=log)
337-
338-
# Create a blueprint object
339-
blueprint = SubrequestsBlueprint.parse_obj([new_plant_type, new_asset, new_log])
340-
341-
# OR provide a list of Subrequest objects.
342-
blueprint = [new_plant_type, new_asset, new_log]
343-
344-
# Send the blueprint.
345-
response = client.subrequests.send(blueprint, format=Format.json)
346-
```
253+
```

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ Now that you know the basics, dive deeper into following topics:
8686

8787
- [Authorizing with farmOS server](authorization.md)
8888
- [Working with farmOS resources](client_2x.md)
89+
- [Using Subrequests](subrequests.md)

docs/subrequests.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Subrequests
2+
3+
## Background
4+
5+
farmOS.py supports the [Subrequests](https://www.drupal.org/project/subrequests) module which can optionally be
6+
installed on the farmOS server.
7+
8+
Subrequests allows multiple requests to be defined in a "blueprint" and sent to the server in a single POST request.
9+
The blueprint can define sequential requests so that the response of one request can be embedded into the body of a
10+
later request. See the subrequests [blueprint specification](https://git.drupalcode.org/project/subrequests/blob/8.x-2.x/SPECIFICATION.md)
11+
for more documentation.
12+
13+
farmOS.py provides some additional features to help use subrequests:
14+
- The default response format is set to `json` by appending a `?_format=json` query parameter automatically. JSON is
15+
much easier to parse than subrequest's default `html` format, but HTML can be requested by specifying `format='html'`.
16+
- Unless otherwise provided, the `Accept` and `Content-Type` headers for each sub-request will be set to
17+
`application/vnd.api+json` for standard JSONAPI requests.
18+
- An `endpoint` can be provided instead of the full `uri` for each sub-request. farmOS.py will build the full `uri`
19+
from the `hostname` already configured with the client.
20+
- The sub-request `body` can be provided as an object that farmOS.py will serialize into a JSON object string.
21+
- Blueprints are validated using Pydantic models. These models can also be used to build individual requests to include
22+
in a blueprint.
23+
24+
## Example
25+
26+
An example that creates a new asset followed by a new log that references the asset:
27+
28+
```python
29+
from farmOS import farmOS
30+
from farmOS.subrequests import Action, Subrequest, SubrequestsBlueprint, Format
31+
32+
client = farmOS("http://localhost", scope="farm_manager", version=2)
33+
client.authorize('username', 'password')
34+
35+
plant_type = {
36+
"data": {
37+
"type": "taxonomy_term--plant_type",
38+
"attributes": {
39+
"name": "New plant type"
40+
}
41+
}
42+
}
43+
44+
new_plant_type = Subrequest(action=Action.create, requestId="create-plant-type", endpoint="api/taxonomy_term/plant_type", body=plant_type)
45+
46+
plant = {
47+
"data": {
48+
"type": "asset--plant",
49+
"attributes": {
50+
"name": "My new plant",
51+
"description": "Created in the first request.",
52+
},
53+
"relationships": {
54+
"plant_type": {
55+
"data": [
56+
{
57+
"type": "taxonomy_term--plant_type",
58+
"id": "{{create-plant-type.body@$.data.id}}"
59+
}
60+
]
61+
}
62+
}
63+
}
64+
}
65+
new_asset = Subrequest(action=Action.create, requestId="create-asset", waitFor=["create-plant-type"], endpoint="api/asset/plant", body=plant)
66+
67+
log = {
68+
"data": {
69+
"type": "log--seeding",
70+
"attributes": {
71+
"name": "Seeding my new plant",
72+
"notes": "Created in the second request.",
73+
},
74+
"relationships": {
75+
"asset": {
76+
"data": [
77+
{
78+
"type": "asset--plant",
79+
"id": "{{create-asset.body@$.data.id}}"
80+
}
81+
]
82+
}
83+
}
84+
}
85+
}
86+
new_log = Subrequest(action=Action.create, requestId="create-log", waitFor=["create-asset"], endpoint="api/log/seeding", body=log)
87+
88+
# Create a blueprint object
89+
blueprint = SubrequestsBlueprint.parse_obj([new_plant_type, new_asset, new_log])
90+
91+
# OR provide a list of Subrequest objects.
92+
blueprint = [new_plant_type, new_asset, new_log]
93+
94+
# Send the blueprint.
95+
response = client.subrequests.send(blueprint, format=Format.json)
96+
```

0 commit comments

Comments
 (0)