Created
October 23, 2015 06:07
-
-
Save rymndhng/396bdc702dd6f6ed3769 to your computer and use it in GitHub Desktop.
CLJ-HTTP Dynamic Basic Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ext | |
"This module adds default parameters to requests such as the ability to dynamically set the | |
user credentials. This pattern could be used to do many things such | |
as append {:debug true}, useful in an interactive environment." | |
(:require [clj-http.client :as http])) | |
(def ^:dynamic *additional-params* | |
"Available at any time to set additional params to | |
Automatically bound when `with-middleware` is used." | |
{}) | |
(defn wrap-additional-params | |
"Additional middleware for dynamically setting anything (such as | |
credentials)." | |
[client] | |
(fn [req] | |
(client (merge req *additional-params*)))) | |
(defmacro with-basic-auth [credentials & body] | |
"Wraps the context of the request with basic auth so we can stream larger requests." | |
`(binding [*additional-params* (assoc *additional-params* :basic-auth ~credentials)] | |
(http/with-middleware (concat http/*current-middleware* ~[wrap-additional-params]) | |
~@body))) | |
(comment | |
; Example of how to use this macro. | |
(with-basic-auth "rymndhng:foobarbaz" | |
(http/get "https://api.github.com/users/whatever")) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment