Forum Discussion

Denis_T's avatar
2 years ago
Solved

API request doesn't return all objects..

I am trying the API request with the following (skipping authentication header below and other non relevant parts):

....
company = d['company']
access_id = d['access-id']
access_key = d['access-key']

#Request data for alerts
httpVerb ='GET'
resourcePath = '/alert/alerts'
queryParams ='?offset=0&size=1000'
data=''

#construct URL
url = 'https://' + company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParams
....

 

Authentication is going fine and I get "200 response".

But JSON returned does not have all objects.

It starts like this:

{
  "status" : 200,
  "errmsg" : "OK",
  "data" : {
    "total" : 238,
    "items" : [ {
      "id" : "DS5957680",
      "type" : "dataSourceAlert",
      "internalId" : "LMD659437",
      "startEpoch" : 1660495929,
      "endEpoch" : 0,
      "acked" : false,
      "ackedEpoch" : 0,
      "ackedBy" : "",
      "ackComment" : "",

So , I am expecting 238 objects, but it show only 4 and interrupted on the 5th

 } ],
      "resourceId" : 23853,
      "resourceTemplateId" : 650,
      "resourceTemplateType" : "DS",
      "resourceTempl

 

Have anybody seen this stuff? What could be wrong here? 

Python request module usage requires additional tuning?

 

  • Output the URL and your headers to your screen and verify the same behavior in postman. If it does the same thing in postman, it's a problem with the API. If postman fetches it just fine, it's your script. 

    If you're using python, consider using the SDK. Much easier:

    alerts = lm.get_alert_list(size=1000,offset=0).items

     

    If you need pagination (because there are more than 1000 items):

    alerts = []
    end_found = False
    offset = 0
    size = 1000
    while not end_found:
      current = lm.get_alert_list(size=size, offset=offset).items
      alerts += current
      offset += len(current)
      end_found = len(current) != size

     

7 Replies

  • Output the URL and your headers to your screen and verify the same behavior in postman. If it does the same thing in postman, it's a problem with the API. If postman fetches it just fine, it's your script. 

    If you're using python, consider using the SDK. Much easier:

    alerts = lm.get_alert_list(size=1000,offset=0).items

     

    If you need pagination (because there are more than 1000 items):

    alerts = []
    end_found = False
    offset = 0
    size = 1000
    while not end_found:
      current = lm.get_alert_list(size=size, offset=offset).items
      alerts += current
      offset += len(current)
      end_found = len(current) != size

     

  • Coolio. the method you want is .get_alert_list(size=1000,offset=0).items.

    That documentation page is for v3 of the SDK which is newly released. I'm not sure they've noticed the navigation problems. For me, none of the links in the left frame even work. Here's the v2 documentation, which is pretty solid and probably matches up with v3 for the most part. While LM did release APIv3, I haven't noticed any of the endpoints I know exist (and use; call me a renegade) but that weren't available in v2. So, i haven't found a compelling reason to move to v3 of the api/sdk.

  • Also, I followed the advice from Stuart and put the Authentication header in Postman.

    Return was good, 587K of JSON.

    At least I know nothing is wrong with the API :)/emoticons/smile@2x.png 2x" title=":)" width="20" />

  • 14 hours ago, Denis T said:

    Thanks Stuart.

    Regarding SDK:

      I was trying to look into SDK, but it requires some authentication:

      in here: https://www.logicmonitor.com/swagger-ui-master/api-v3/Python-SDK/lm-sdkv3-docs.html

      it is sending me there:

      https://bamboo.logicmonitor.com/browse/LM-SS-172/artifact/shared/Docs/index.html#api-Alerts-getAlertList

     

     

    I figured out that we can access the material by clicking in the middle of the page and scrolling (or using page search).

  • And finally I found what was "the issue".. It is just VS Code not printing the whole request reply if it's too long:). 

  • Fancy IDEs and code editors are no match for notepad and terminal at your side.