Developer

last person joined: 2 days ago 

MM API GET filters and POST action examples using Python

This thread has been viewed 19 times
  • 1.  MM API GET filters and POST action examples using Python

    Posted Dec 08, 2020 05:44 PM
    Writing these examples to save people the headaches of trying to figure out how to use a GET object filter or how to perform SET actions using POST. For full details, refer to the API guide under the sections "Object Filter" and "Understanding SET Request and Response".

    If you need a python script to log in and extract the UID, refer to my other post "MM API login techniques with Python and 'show version'".

    If you need a script to build the config_path parameter for you simply by providing a group or controller name, refer to my other post "MM API config_path algorithm".

    Running a GET with a filter on the object int_gig:
    #import requests
    
        # 'uid' has already been stored previously
    
        myUrl="https://mm.example.com/v1/configuration/object/int_gig"
        myHeaders = {'Content-Type': 'application/json','Accept': 'application/json'}
        myParams = {'UIDARUBA': uid,
                    'filter': r'[{"OBJECT":{ "$eq":["int_gig.slot/module/port"]}}]',  # OBJECT filter; refer to API guide section "Object Filter"
                    'config_path':'/md/00:0b:86:bc:0c:c7'
                    }
    
        response = requests.get(url=myUrl,
                               headers=myHeaders,
                               params=myParams)​


    Response data of the GET with the example filter:


    {
      "_data": {
        "int_gig": [
          {
            "slot/module/port": "0/0/0"
          }, 
          {
            "slot/module/port": "0/0/1"
          },
          <snip>
        ]
    }​

    Using POST to 'shut' and 'no shut' an interface:

    #import requests
    
        # 'myUid' has already been stored previously
    
        myUrl = 'https://mm.example.com/v1/configuration/object/int_gig'
        myHeaders = {'Content-Type': 'application/json','Accept': 'application/json'}
        myParams = {'config_path': '/md/00:0b:86:bc:0c:c7','UIDARUBA': myUid}
    
        # shut
        myData = r'{ "slot/module/port":"0/0/21" , "int_gig_shut":{} }'
        response = requests.post(url=myUrl,
                                headers=myHeaders,
                                params=myParams,
                                data=myData
                                )
    
        # no shut - notice the use of _action delete - refer to API guide "Understanding SET Request and Response"
        myData = r'{ "slot/module/port":"0/0/21" , "int_gig_shut":{"_action":"delete"} }'
        response = requests.post(url=myUrl,
                                headers=myHeaders,
                                params=myParams,
                                data=myData
                                )
    ​

    According to the API guide, "we currently do not support HTTP DELETE and HTTP PUT operations". Therefore, the only way to "no" a command is to use the delete action like what I've shown here.

    ------------------------------
    Tim Haynie, CWNE 254, ACMX 508, ACDX 384
    ------------------------------