Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Last active December 15, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenjamin/548670fe5855c7943d81 to your computer and use it in GitHub Desktop.
Save glenjamin/548670fe5855c7943d81 to your computer and use it in GitHub Desktop.
Crazily indented clojure macro
(ns http)
(defn post [a b] "test")
(def ^:private http-extras
{:insecure? true, :debug true})
(let [orig http/post
f (fn [u r] (orig u (merge r http-extras)))]
(defmacro patch-http
"Monkey-patch HTTP requests to do stuff we want"
[& body]
`(with-redefs [http/post ~f] ~@body)))
(defn get-token
"Get an OAuth request token"
[callback]
(patch-http
(+ 1 2)))
@edw
Copy link

edw commented Mar 17, 2013

(defmacro patch-http
  "Monkey-patch HTTP requests to do stuff we want"
  [& body]
  `(with-redefs
     [http/post
      #(clj-http.client/post %1 (assoc %2 :insecure? true :debug true))]
      ~@body))

@edw
Copy link

edw commented Mar 17, 2013

(defmacro patch-http
  "Monkey-patch HTTP requests to do stuff we want"
  [& body]
  (let [f #(clj-http.client/post %1 (assoc %2 :insecure? true :debug true))]
    `(with-redefs [http/post ~f] ~@body)))

@edw
Copy link

edw commented Mar 17, 2013

(let [orig-post clj-http.client/post
      f #(orig-post %1 (assoc %2 :insecure? true :debug true))]
  (defmacro patch-http
    "Monkey-patch HTTP requests to do stuff we want"
    [& body] `(with-redefs [http/post ~f] ~@body)))

@edw
Copy link

edw commented Mar 17, 2013

(ns http)

(defn post [a b] "test")

(def ^:private http-extras
  {:insecure? true, :debug true})

(let [orig-post http/post]
  (defmacro patch-http
    "Monkey-patch HTTP requests to do stuff we want"
    [& body]
    `(with-redefs [http/post #(~orig-post %1 (merge %2 http-extras))]
       ~@body)))

(defn get-token
  "Get an OAuth request token"
  [callback]
  (let [orig-post http/post]
    (with-redefs [http/post #(orig-post %1 (merge %2 http-extras))]
      (+ 1 2))))

(defn get-token-2
  "Get an OAuth request token"
  [callback]
  (patch-http (+ 1 2)))

@edw
Copy link

edw commented Mar 17, 2013

(ns http)

(defn post [a b] [a b])

(def ^:private http-extras
  {:insecure? true, :debug true})

(let [orig-post http/post]
  (defmacro patch-http
    "Monkey-patch HTTP requests to do stuff we want"
    [& body]
    `(with-redefs [http/post #(~orig-post %1 (merge %2 http-extras))]
       ~@body)))

(defn get-token
  "Get an OAuth request token"
  [callback]
  (let [orig-post http/post]
    (with-redefs [http/post #(orig-post %1 (merge %2 http-extras))]
      (post "/foo" {}))))

(defn get-token-2
  "Get an OAuth request token"
  [callback]
  (patch-http (post "/foo" {})))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment