Through some rigorous trail and error trying to implement async/await functionality, I’ve found that it’s incredibly difficult due to unpredictable “race conditions”. I’ve instead turned to using a batch operation instead. A good example of this can be seen here (create multiple items).
I’ve been successful with incorporating this methodology to create multiple groups. Due to scripting in python, in order to pass the data through correctly, it has to be very carefully formatted.
def post_request(data, url=apiUrl, headers=headers, timeout=timeout):
try:
response = requests.post(url=url, headers=headers, data=json.dumps(data), timeout=timeout)
except Exception as e:
print(e)
def create_group_payload(zip_vars: zip):
cnt = 0
payload = "mutation {"
for board_id, group_name in zip_vars:
x = f"""
complexity {{
query
before
after
}}
createGroup{cnt}: create_group (board_id: {board_id} group_name: {json.dumps(group_name)}) {{
id
title
}}
"""
payload += f"\n{x}"
cnt += 1
# Each group mutation has a complexity of ~33_000
# Max complexity of 5_000_000 for each request
# Set batch value to something that can be processed before the timeout value is reached
if cnt == 30:
payload += "\n}"
data = {'query' : payload}
cnt = 0 # reset cnt
payload = "mutation {" # reset payload
yield data
# Last batch likely be less than 30
payload += "\n}"
data = {'query' : payload}
yield data
group_func_vars = zip(board_id_list, unique_projects)
# Post Groups
tasks = list(create_group_payload(group_func_vars))
group_r = [post_request(task) for task in tasks]
To my question, I’ve been having a difficult time creating multiple items in this same fashion. Again, due to scripting in python, I’ve had a difficult time getting the data to format correctly to pass as the correct JSON object. Here is how I can sequentially create items. This method was built through this other monday Q/A I had.
def create_item_payload(board_id, group_id, item_name, column_values):
query = """
mutation ($boardId: ID!, $itemName: String!, $groupId: String, $columnValues: JSON) {
complexity {
query
before
after
}
create_item(
board_id: $boardId
group_id: $groupId
item_name: $itemName
column_values: $columnValues
) {
id
name
}
}
"""
variables = {
'boardId': board_id,
'groupId': group_id,
'itemName': item_name,
'columnValues': json.dumps(column_values, separators=(',', ':'))}
data = {
'query': query,
'variables': variables}
return data
A similar loop append method shown in the def create_group_payload()
function can be used here, however I am confused about how to correctly connect the variables
with each create_item()
.
Using a manual method to test, this is what I assumed the format to look like. Yet when I POST it, I receive a server 500 error, which likely indicates that the query is not formatted correctly.
# Easier visulization of the data
data = {
'query':
"""
mutation ($boardId: ID!, $itemName: String!, $groupId: String, $columnValues: JSON) {
createItem0: create_item(
board_id: $boardId
group_id: $groupId
item_name: $itemName
column_values: $columnValues) {
id
}
)
createItem1: create_item(
board_id: $boardId
group_id: $groupId
item_name: $itemName
column_values: $columnValues) {
id
}
)
}
"""
,
'variables': [
{
'boardId': '123456789',
'groupId': 'group__id__1',
'itemName': 'item_name_1',
'columnValues': '{"phase__1":"some text here 0"}'
},
{
'boardId': '123456789',
'groupId': 'group__id__1',
'itemName': 'itme_name_2',
'columnValues': '{"phase__1":"some text here 1"}'
}
]
}
response = requests.post(url=apiUrl, headers=headers, data=json.dumps(mydata), timeout=timeout)
Unfortunately I am at a complete lost of how the data should look. Could someone adjust the manual data to the proper format/sequence to where it can be passed?