Skip to content

Commit

Permalink
Merge pull request #20 from brandonbloom/defaults
Browse files Browse the repository at this point in the history
Add support for default options
  • Loading branch information
Raynes committed Dec 20, 2012
2 parents e8874ba + f1376e5 commit 555f704
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,8 @@ user> (repos/repos {:auth "Raynes:REDACTED" :per-page 1})
[{:fork true, :pushed_at "2011-09-21T05:37:17Z", :name "lein-marginalia", :clone_url "https://github.com/Raynes/lein-marginalia.git", :watchers 1, :updated_at "2011-11-23T03:27:47Z", :html_url "https://github.com/Raynes/lein-marginalia", :owner {:login "Raynes", :avatar_url "https://secure.gravatar.com/avatar/54222b6321f0504e0a312c24e97adfc1?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", :url "https://api.github.com/users/Raynes", :gravatar_id "54222b6321f0504e0a312c24e97adfc1", :id 54435}, :language "Clojure", :size 180, :created_at "2011-11-23T03:27:47Z", :private false, :homepage "", :git_url "git://github.com/Raynes/lein-marginalia.git", :url "https://api.github.com/repos/Raynes/lein-marginalia", :master_branch nil, :ssh_url "git@github.com:Raynes/lein-marginalia.git", :open_issues 0, :id 2832999, :forks 0, :svn_url "https://svn.github.com/Raynes/lein-marginalia", :description "A Marginalia plugin to Leiningen "}] [{:fork true, :pushed_at "2011-09-21T05:37:17Z", :name "lein-marginalia", :clone_url "https://github.com/Raynes/lein-marginalia.git", :watchers 1, :updated_at "2011-11-23T03:27:47Z", :html_url "https://github.com/Raynes/lein-marginalia", :owner {:login "Raynes", :avatar_url "https://secure.gravatar.com/avatar/54222b6321f0504e0a312c24e97adfc1?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", :url "https://api.github.com/users/Raynes", :gravatar_id "54222b6321f0504e0a312c24e97adfc1", :id 54435}, :language "Clojure", :size 180, :created_at "2011-11-23T03:27:47Z", :private false, :homepage "", :git_url "git://github.com/Raynes/lein-marginalia.git", :url "https://api.github.com/repos/Raynes/lein-marginalia", :master_branch nil, :ssh_url "git@github.com:Raynes/lein-marginalia.git", :open_issues 0, :id 2832999, :forks 0, :svn_url "https://svn.github.com/Raynes/lein-marginalia", :description "A Marginalia plugin to Leiningen "}]
``` ```


Default options can be specified via `with-defaults`.

If an API function has no options and authentication would have no uses for that particular call, the options map is not a parameter at all. For API calls that can do different things based on whether or not you are authenticated but authentication is not **required**, then the options map will be an optional argument. For API calls that require authentication to function at all, the options map is a required argument. Any data that is required by an API call is a positional argument to the API functions. The options map only ever contains authentication info and/or optional input. If an API function has no options and authentication would have no uses for that particular call, the options map is not a parameter at all. For API calls that can do different things based on whether or not you are authenticated but authentication is not **required**, then the options map will be an optional argument. For API calls that require authentication to function at all, the options map is a required argument. Any data that is required by an API call is a positional argument to the API functions. The options map only ever contains authentication info and/or optional input.


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. 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.
Expand Down
9 changes: 7 additions & 2 deletions src/tentacles/core.clj
Expand Up @@ -6,12 +6,13 @@
(:import java.net.URLEncoder)) (:import java.net.URLEncoder))


(def ^:dynamic url "https://api.github.com/") (def ^:dynamic url "https://api.github.com/")
(def ^:dynamic defaults {})


(defn query-map (defn query-map
"Turn keywords into strings and replace hyphens with underscores." "Merge defaults, turn keywords into strings, and replace hyphens with underscores."
[entries] [entries]
(into {} (into {}
(for [[k v] entries] (for [[k v] (concat defaults entries)]
[(.replace (name k) "-" "_") v]))) [(.replace (name k) "-" "_") v])))


(defn parse-json (defn parse-json
Expand Down Expand Up @@ -96,3 +97,7 @@
(defmacro with-url [new-url & body] (defmacro with-url [new-url & body]
`(binding [url ~new-url] `(binding [url ~new-url]
~@body)) ~@body))

(defmacro with-defaults [options & body]
`(binding [defaults ~options]
~@body))
4 changes: 2 additions & 2 deletions src/tentacles/users.clj
Expand Up @@ -10,7 +10,7 @@


(defn me (defn me
"Get info about the currently authenticated user." "Get info about the currently authenticated user."
[options] [& [options]]
(api-call :get "user" nil options)) (api-call :get "user" nil options))


(defn edit-user (defn edit-user
Expand Down Expand Up @@ -103,4 +103,4 @@
(defn delete-key (defn delete-key
"Delete a public key." "Delete a public key."
[id options] [id options]
(no-content? (api-call :delete "user/keys/%s" [id] options))) (no-content? (api-call :delete "user/keys/%s" [id] options)))

0 comments on commit 555f704

Please sign in to comment.