Need help creating Rules using the RestAPI and Python

Hello,

I’m exploring the new Configuration API included in the 2022R1 release. I’ve had success doing basic tasks like generating new users and pulling information surrounding camera group membership and camera stream settings.

However I am running into issues trying to generate new rules.

Has anyone had any success doing this using Python? I am getting a 400 response code so I know there is an issue with the structure of my request however I am struggling to find any information on what the issue is. I am using the following documentation from Milestone, but if there is additional information I would love to read that as well.

https://doc.developer.milestonesys.com/mipvmsapi/#operation/postrulesById

Any successful sample code would also be greatly appreciated.

Thanks

Tyler Snell

Hello Tyler

Rules have a rather complicated structure! I would suggest that you get a rule which looks like what you want to create. Then modify the received json and post the rule as you want it created. I’ve made a small method which is based on our python sample:

def dublicate_rule(api_gateway: Gateway, session: requests.Session, token: str, ruleId):
    """Get a rule, update name and id, then create a duplicate"""
 
    # Get rule through REST
    orignial_rule = ""
    response = api_gateway.get_single(session,'rules', ruleId, token)
    if response.status_code == 200:
        orignial_rule = response.json()['data']
        print(f"Rule to duplicate result:\n{orignial_rule}\n\n")
    else:
        error = response.json()['error']
        print(error)
        return
 
    # Modify the rule
    orignial_rule["name"] = "Copy of " + orignial_rule["name"];
    orignial_rule["id"] = str(uuid.uuid4())
 
    # Create the duplicate rule
    create_rule_payload = json.dumps(orignial_rule)
    response = api_gateway.create_item(session, "rules", create_rule_payload, token);
    if response.status_code == 201:
        orignial_rule = response.json()['result']
        print(f"Duplicate rule created result:\n{orignial_rule}\n\n")
    else:
        error = response.json()['error']
        print(error)
        return

If you get our python sample, paste this code into it including import uuid. Then you can call this method and see the post working. To find the rule id, use the /rules.

Please let me know if you have issues or problems with the code.