Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
Created December 30, 2013 23:16
Show Gist options
  • Save rsslldnphy/8189870 to your computer and use it in GitHub Desktop.
Save rsslldnphy/8189870 to your computer and use it in GitHub Desktop.
Functional HTTP request
(ns request)
(defn request [] {})
(defn body [] {})
(defn file [] {})
(defn set-url
[request url]
(assoc request :url url))
(defn set-method
[request method]
(assoc request :method method))
(defn set-body
[request body]
(assoc request :body body))
(defn set-multipart
[body]
(assoc body :multipart true))
(defn add-multipart-field
[body k v]
(assoc body k v))
(defn add-multipart-file
[body file]
(assoc body :file file))
(defn set-content-disposition
[file content-disposition]
(assoc file :content-disposition content-disposition))
(defn set-content-disposition-param
[file k v]
(assoc-in file [:content-disposition-params k] v))
(defn add-header
[file header value]
(assoc file header value))
(defn send-request
[request]
(when-not (request :method) (throw (IllegalArgumentException.
"Request method must be set")))
;; etc etc etc other checks go here
(println "Doing the thing with the request"))
;;;; App setup
(def api-password "aoesndaoe8994d',ndai")
(def project-id "123")
(def my-file
(-> (file)
(set-content-disposition :form-data)
(set-content-disposition-param :name "file")
(add-header :Content-Transfer-Encoding "binary")))
;;;; Example usage ;;;;
(-> (request)
(set-url "http://example.org")
(set-method :post)
(set-body (-> (body)
(set-multipart)
(add-multipart-field :api_password api-password)
(add-multipart-field :project_id project-id)
(add-multipart-file my-file)))
(send-request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment