@@ -250,97 +250,4 @@ response = farm_client.term.send('plant_type', {"attributes": {"name": "Corn"}})
250250
251251``` python
252252farm_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+ ```
0 commit comments