Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for Rate Limiting #21

Merged
merged 2 commits into from

2 participants

@jeluard

Implementation for #17

Tests are in a separate commit as I am not sure that's the infrastructure you had in mind.

@Raynes
Owner

Yeah, I never really did much with tests. Never quite figured out the best way to do it. This is fine. Thanks for contributing. :)

@Raynes Raynes merged commit eb14c22 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 27, 2012
  1. @jeluard
  2. @jeluard
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 3 deletions.
  1. +7 −3 src/tentacles/core.clj
  2. +13 −0 test/tentacles/core_test.clj
View
10 src/tentacles/core.clj
@@ -31,17 +31,21 @@
(map parse-link)
(into {})))
+(defn merge-rate-limit [m h]
+ "Merges RateLimit values from headers into Json response"
+ (merge m (select-keys h [:X-RateLimit-Limit :X-RateLimit-Remaining])))
+
(defn safe-parse
"Takes a response and checks for certain status codes. If 204, return nil.
- If 400, 422, 404, 204, or 500, return the original response with the body parsed
+ If 400, 401, 204, 422, 403, 404 or 500, return the original response with the body parsed
as json. Otherwise, parse and return the body if json, or return the body if raw."
[resp]
- (if (#{400 401 204 422 404 500} (:status resp))
+ (if (#{400 401 204 422 403 404 500} (:status resp))
(update-in resp [:body] parse-json)
(let [links (parse-links (get-in resp [:headers "link"] ""))
content-type (get-in resp [:headers "content-type"])]
(if-not (.contains content-type "raw")
- (with-meta (parse-json (:body resp)) {:links links})
+ (with-meta (merge-rate-limit (parse-json (:body resp)) (:headers resp)) {:links links})
(resp :body)))))
(defn update-req
View
13 test/tentacles/core_test.clj
@@ -0,0 +1,13 @@
+(ns tentacles.core-test
+ (:use clojure.test)
+ (:require [tentacles.core :as core]))
+
+(deftest hitting-rate-limit-is-propagated
+ (is (= (:status (core/safe-parse {:status 403}))
+ 403)))
+
+(deftest rate-limit-details-are-propagated-when-defined
+ (is (contains? (core/safe-parse {:status 200 :X-RateLimit-Limit 20 :headers {"content-type" ""}}) :X-RateLimit-Limit)))
+
+(deftest rate-limit-details-are-ignored-when-undefined
+ (is (not (contains? (core/safe-parse {:status 200 :headers {"content-type" ""}}) :X-RateLimit-Limit))))
Something went wrong with that request. Please try again.