Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null parts disallowed in create-multipart-entity. #288

Merged
merged 1 commit into from
Nov 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/clj_http/multipart.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,37 @@
(throw (Exception. (str "Multipart string body must contain "
"at least :content")))))

(defn make-multipart-body
"Create a body object from the given map, dispatching on the type
of its content. Requires the content to be of type File, InputStream,
ByteArray, or String."
[multipart]
(let [klass (type (:content multipart))]
;; TODO: replace with multimethod? actually helpful?
(cond
(isa? klass File)
(make-file-body multipart)

(isa? klass InputStream)
(make-input-stream-body multipart)

(= klass byte-array-type)
(make-byte-array-body multipart)

(= klass String)
(make-string-body multipart)

:else
(throw (Exception. (str "Multipart content must be of type File, "
"InputStream, ByteArray, or String."))))))

(defn create-multipart-entity
"Takes a multipart vector of maps and creates a MultipartEntity with each
map added as a part, depending on the type of content."
[multipart]
(let [mp-entity (MultipartEntity.)]
(doseq [m multipart]
(let [klass (type (:content m))
name (or (:part-name m) (:name m))
;; TODO: replace with multimethod? actually helpful?
part (cond
(isa? klass File)
(make-file-body m)

(isa? klass InputStream)
(make-input-stream-body m)

(= klass byte-array-type)
(make-byte-array-body m)

(= klass String)
(make-string-body m))]
(let [name (or (:part-name m) (:name m))
part (make-multipart-body m)]
(.addPart mp-entity name part)))
mp-entity))