Skip to content
Browse files

added api-meta and updated tests

  • Loading branch information...
1 parent a085e1d commit 01910a29e48f2728fca071071db60cdcaf3713bd @ngrunwald ngrunwald committed
Showing with 16 additions and 12 deletions.
  1. +3 −2 README.md
  2. +9 −5 src/tentacles/core.clj
  3. +4 −5 test/tentacles/core_test.clj
View
5 README.md
@@ -41,10 +41,11 @@ If an API function has no options and authentication would have no uses for that
Authentication is supported by Github user authentication `:auth <username:password>` as demonstrated above, or by oauth or oauth2. For oauth add `:oauth-token <token>` to the options map. Likewise, for oauth2, include `:client-id <client_id> :client-token <client_token>` in the options map.
-You can access useful information returned by the API such as current rate limits, etags, etc. by checking the metadata of the response. You can then use this to perform conditional requests against the API. If the data has not changed, the keyword `:tentacles.core/not-modified` will be returned. This does not consume any API call quota.
+You can access useful information returned by the API such as current
+rate limits, etags, etc. by checking the response with `core/api-meta`. You can then use this to perform conditional requests against the API. If the data has not changed, the keyword `:tentacles.core/not-modified` will be returned. This does not consume any API call quota.
```clojure
-user> (meta (repos/readme "Raynes" "tentacles" {}))
+user> (core/api-meta (repos/readme "Raynes" "tentacles" {}))
{:links {nil nil}, :etag "\"f1f3cfabbf0f98e0bbaa7aa424f92e75\"", :last-modified "Mon, 28 Jan 2013 21:13:48 GMT", :call-limit 60, :call-remaining 59}
user> (repos/readme "Raynes" "tentacles" {:etag "\"f1f3cfabbf0f98e0bbaa7aa424f92e75\""})
View
14 src/tentacles/core.clj
@@ -33,10 +33,14 @@
(defn extract-useful-meta
[h]
- (let [{:strs [^String etag last-modified x-ratelimit-limit x-ratelimit-remaining]} h]
+ (let [{:strs [etag last-modified x-ratelimit-limit x-ratelimit-remaining]} h]
{:etag etag :last-modified last-modified
- :call-limit (Long/parseLong x-ratelimit-limit)
- :call-remaining (Long/parseLong x-ratelimit-remaining)}))
+ :call-limit (when x-ratelimit-limit (Long/parseLong x-ratelimit-limit))
+ :call-remaining (when x-ratelimit-remaining (Long/parseLong x-ratelimit-remaining))}))
+
+(defn api-meta
+ [obj]
+ (:api-meta (meta obj)))
(defn safe-parse
"Takes a response and checks for certain status codes. If 204, return nil.
@@ -54,9 +58,9 @@
(if-not (.contains content-type "raw")
(let [parsed (parse-json body)]
(if (map? parsed)
- (with-meta parsed (merge metadata {:links links}))
+ (with-meta parsed {:links links :api-meta metadata})
(with-meta (map #(with-meta % metadata) parsed)
- (merge metadata {:links links}))))
+ {:links links :api-meta metadata})))
body))))
(defn update-req
View
9 test/tentacles/core_test.clj
@@ -6,8 +6,7 @@
(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))))
+(deftest rate-limit-details-are-propagated
+ (is (= 60 (:call-limit (core/api-meta
+ (core/safe-parse {:status 200 :headers {"x-ratelimit-limit" "60"
+ "content-type" ""}}))))))

0 comments on commit 01910a2

Please sign in to comment.
Something went wrong with that request. Please try again.