Skip to content

Commit

Permalink
added api-meta and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngrunwald committed Feb 2, 2013
1 parent a085e1d commit 01910a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -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\""})
Expand Down
14 changes: 9 additions & 5 deletions src/tentacles/core.clj
Expand Up @@ -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.
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions test/tentacles/core_test.clj
Expand Up @@ -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.