0

I need your help in getting Watson Assistant Logs. I am getting an error “Rate limit exceeded”. According to API docs, we can specify cursor in param. When cursor is specified we can make 120 requests / min. If cursor is not specified then we can make 40 requests / 30min only. I have passed cursor attribute in “param” object as empty string when it first initiates and then I have updated the cursor value to next_cursor value which was returned by Watson (pagination attribute as a token). However, still its making 40 requests and connection gets closed for 30 min. Could you please tell me what I am doing wrong? I am able to retrieve 40 logs in text file. I am using Node JS. I am looking forward to hearing from you.

Below is the small sample function which I have created:

const getLogs = () => {
           const params = {
             workspace_id: '50f6598a-a369-45df-9cfb-20bdfe617066',
             cursor: ""
           }
           service.listLogs(params)
           .then(res => {
             if(res.pagination.next_cursor) {
               fs.writeFile("temp.txt", JSON.stringify(res, null, 2), err => {
                 if (err) console.log(err);
                 console.log("Successfully Written to File.");
               });
               params.cursor = res.pagination.next_cursor;
               getLogs()
             } else {
               console.log('no more logs')
             }
           })
           .catch(err => console.log(JSON.stringify(err, null, 2)));
         }
         getLogs();

{
  "name": "Too Many Requests",
  "code": 429,
  "message": "Rate limit exceeded",
  "body": "{\"error\":\"Rate limit exceeded\",\"code\":429}",
  "headers": {
    "x-backside-transport": "FAIL FAIL",
    "content-type": "application/json; charset=utf-8",
    "access-control-allow-origin": "*",
    "access-control-allow-methods": "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS",
    "access-control-allow-headers": "Content-Type, Content-Length, Authorization, X-Watson-Authorization-Token, X-WDC-PL-OPT-OUT, X-Watson-UserInfo, X-Watson-Learning-Opt-Out, X-Watson-Metadata",
    "access-control-max-age": "3600",
    "content-security-policy": "default-src 'none'",
    "x-dns-prefetch-control": "off",
    "x-frame-options": "SAMEORIGIN",
    "strict-transport-security": "max-age=31536000;",
    "x-download-options": "noopen",
    "x-content-type-options": "nosniff",
    "x-xss-protection": "1; mode=block",
    "x-ratelimit-limit": "40",
    "x-ratelimit-reset": "1559060571",
    "x-ratelimit-remaining": "0",
    "retry-after": "1143.36",
    "x-global-transaction-id": "7ecac92c5ced5be3440b2991",
    "x-dp-watson-tran-id": "gateway01-1141582225",
    "x-dp-transit-id": "gateway01-1141582225",
    "content-length": "42",
    "x-edgeconnect-midmile-rtt": "256",
    "x-edgeconnect-origin-mex-latency": "210",
    "date": "Tue, 28 May 2019 16:03:47 GMT",
    "connection": "close"
  }
}
Meet
  • 243
  • 2
  • 13
  • If I were to hazard a guess, its because you are recursing and `params.cusor` is being reset to "" at the start of each recursion. For a simplified fix just pass in cursor into `getLogs(cursor)` – chughts May 29 '19 at 08:32

1 Answers1

1

I think this may be recursion related and that params.cursor is set to "" at the start of each recursion. What you need to do is to combine recursion with closure. So your code will look something like:

 const getLogs = () => {
   const params = {
        workspace_id: '50f6598a-a369-45df-9cfb-20bdfe617066',
        cursor: ""
    }
   function doGetLogs() {
     service.listLogs(params)
     .then(res => {
        if(res.pagination.next_cursor) {
                ...
                params.cursor = res.pagination.next_cursor;
                doGetLogs()
              } 
              ...
            })
           ...
   }
   return doGetLogs();
 }

 getLogs();
chughts
  • 4,210
  • 2
  • 14
  • 27
  • Thanks for correcting my recursion usage. However, issue is related to cursor. Watson API Docs says: ***If cursor is not specified, this operation is limited to 40 requests per 30 minutes. If cursor is specified, the limit is 120 requests per minute. For more information, see Rate limiting.*** I am getting 40 records only then I get error code 429. An HTTP status code of 429 indicates that the rate limit has been exceeded. Could any one please let me know how to fix this issue. – Meet May 29 '19 at 15:10
  • 1
    So you are still getting 40 even after making the code change so that cursor is being set and used correctly? As you had it originally, you weren't actually using the cursor. – chughts May 29 '19 at 15:57
  • yes, I used your recursion. you are right. I did not set cursor properly. every time I call function cursor is empty. i fixed it now. thanks – Meet May 29 '19 at 16:22
  • don't know how I missed it :( – Meet May 29 '19 at 16:25