Browse files

Merge pull request #45 from eLobato/master

User agent can be set on requests
  • Loading branch information...
2 parents b4df5a4 + 25e2716 commit caeca8be442daf504daee4b4aad09803fe86ea69 @Raynes committed Dec 10, 2013
Showing with 19 additions and 5 deletions.
  1. +6 −0
  2. +5 −3 src/tentacles/core.clj
  3. +8 −2 test/tentacles/core_test.clj
@@ -55,6 +55,12 @@ user> (repos/readme "Raynes" "tentacles" {:if-modified-since "Mon, 28 Jan 2013 2
+Similarly, you can set an User-Agent to make your requests more friendly and identifiable.
+user> (repos/readme "Raynes" "tentacles" {:user-agent "MyPhoneApp"})
The Github API is massive and great. I can't demonstrate every API call. Everything is generally just as easy as the above examples, and I'm working hard to document things as well as possible, so go explore!
Here are some lovely [Marginalia docs]( I also wrote a demonstrational [blog post]( about Tentacles that I intend to keep updated with future releases.
@@ -83,8 +83,8 @@
(str url (apply format end-point (map #(URLEncoder/encode (str %) "UTF-8") positional))))
(defn make-request [method end-point positional
- {:strs [auth throw_exceptions follow_redirects
- accept oauth_token etag if_modified_since]
+ {:strs [auth throw_exceptions follow_redirects accept
+ oauth_token etag if_modified_since user_agent]
:or {follow_redirects true throw_exceptions false}
:as query}]
(let [req (merge-with merge
@@ -99,9 +99,11 @@
{:headers {"Authorization" (str "token " oauth_token)}})
(when etag
{:headers {"if-None-Match" etag}})
+ (when user_agent
+ {:headers {"User-Agent" user_agent}})
(when if_modified_since
{:headers {"if-Modified-Since" if_modified_since}}))
- proper-query (dissoc query "auth" "oauth_token" "all_pages" "accept")
+ proper-query (dissoc query "auth" "oauth_token" "all_pages" "accept" "user_agent")
req (if (#{:post :put :delete} method)
(assoc req :body (json/generate-string (or (proper-query "raw") proper-query)))
(assoc req :query-params proper-query))]
@@ -2,9 +2,15 @@
(:use clojure.test)
(:require [tentacles.core :as core]))
+(deftest request-contains-user-agent
+ (let [request (core/make-request :get "test" nil {"user_agent" "Mozilla"})]
+ (do (is (empty? (:query-params request)))
+ (is (contains? (:headers request) "User-Agent"))
+ (is (= (get (:headers request) "User-Agent") "Mozilla")))))
(deftest hitting-rate-limit-is-propagated
- (is (= (:status (core/safe-parse {:status 403}))
- 403)))
+ (is (= (:status (core/safe-parse {:status 403}))
+ 403)))
(deftest rate-limit-details-are-propagated
(is (= 60 (:call-limit (core/api-meta

0 comments on commit caeca8b

Please sign in to comment.