Browse Source

Initial, very basic, support for dealing with rate limits

Bruce Marriner 9 years ago
parent
commit
4bdb631758
1 changed files with 14 additions and 2 deletions
  1. 14 2
      restapi.go

+ 14 - 2
restapi.go

@@ -87,8 +87,20 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
 	case 200: // OK
 	case 204: // No Content
 
-	// TODO check for 401 response, invalidate token if we get one.
-	// TODO check for 429 response, rate-limit when we get one.
+		// TODO check for 401 response, invalidate token if we get one.
+
+	case 429: // TOO MANY REQUESTS - Rate limiting
+		// This will be changed to a more robust method later.
+		// which may be hugely different as this method could cause
+		// unending recursion
+		rl := RateLimit{}
+		err = json.Unmarshal(response, &rl)
+		if err != nil {
+			err = fmt.Errorf("Request unmarshal rate limit error : ", err)
+			return
+		}
+		time.Sleep(rl.RetryAfter)
+		response, err = s.Request(method, urlStr, data)
 
 	default: // Error condition
 		err = fmt.Errorf("HTTP %s, %s", resp.Status, response)