@@ -280,7 +280,18 @@ from farmOS import farmOS
280
280
from farmOS.subrequests import Action, Subrequest, SubrequestsBlueprint, Format
281
281
282
282
client = farmOS(" http://localhost" , scope = " farm_manager" , version = 2 )
283
- client.authorize(username, password)
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)
284
295
285
296
plant = {
286
297
" data" : {
@@ -289,9 +300,19 @@ plant = {
289
300
" name" : " My new plant" ,
290
301
" description" : " Created in the first request." ,
291
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
+ }
292
313
}
293
314
}
294
- new_asset = Subrequest(action = Action.create, requestId = " create-asset" , endpoint = " api/asset/plant" , body = plant)
315
+ new_asset = Subrequest(action = Action.create, requestId = " create-asset" , waitFor = [ " create-plant-type " ], endpoint = " api/asset/plant" , body = plant)
295
316
296
317
log = {
297
318
" data" : {
@@ -315,15 +336,11 @@ log = {
315
336
new_log = Subrequest(action = Action.create, requestId = " create-log" , waitFor = [" create-asset" ], endpoint = " api/log/seeding" , body = log)
316
337
317
338
# Create a blueprint object
318
- blueprint = SubrequestsBlueprint.parse_obj([new_asset, new_log])
339
+ blueprint = SubrequestsBlueprint.parse_obj([new_plant_type, new_asset, new_log])
319
340
320
341
# OR provide a list of Subrequest objects.
321
- blueprint = [new_asset, new_log]
342
+ blueprint = [new_plant_type, new_asset, new_log]
322
343
323
344
# Send the blueprint.
324
345
response = client.subrequests.send(blueprint, format = Format.json)
325
-
326
- # New resource ids.
327
- print (response[' create-asset' ][' data' ][' id' ])
328
- print (response[' create-log' ][' data' ][' id' ])
329
346
```
0 commit comments