Skip to content

Commit

Permalink
Merge: Consolidate curl argument logic
Browse files Browse the repository at this point in the history
Also adds Joseph's test for "'head :as 'file".
  • Loading branch information
alphapapa committed Aug 16, 2024
2 parents 8ddabbb + 60c8e3f commit 9fb22d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 60 deletions.
88 changes: 28 additions & 60 deletions plz.el
Original file line number Diff line number Diff line change
Expand Up @@ -432,74 +432,42 @@ into the process buffer.
collect (cons "--header" (format "%s: %s" key value))))
(curl-config-args (append curl-config-header-args
(list (cons "--url" url)
(cons "--create-dirs" ""))
(cons "--create-dirs" "")
(cons "--request" (upcase (symbol-name method)))
(cons "--dump-header" "-"))
(when connect-timeout
(list (cons "--connect-timeout"
(number-to-string connect-timeout))))
(when timeout
(list (cons "--max-time" (number-to-string timeout))))
;; NOTE: To make a HEAD request
;; requires using the "--head"
;; option rather than "--request
;; HEAD", and doing so with
;; "--dump-header" duplicates the
;; headers, so we must instead
;; specify that for each other
;; method.
(pcase as
('file
(setf filename (make-temp-file "plz-"))
(list (cons "--output" filename)))
(`(file ,(and (pred stringp) as-filename))
(when (file-exists-p as-filename)
(error "File exists, will not overwrite: %S" as-filename))
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(setf filename (expand-file-name as-filename))
(list (cons "--output" filename)))
((guard (eq 'head method))
;; Don't duplicate headers for HEAD
;; requests which output to the terminal.
(list (cons "--dump-header" null-device))))
(pcase method
('get
(append (list (cons "--dump-header" "-"))
(pcase as
('file
(setf filename (make-temp-file "plz-"))
(list (cons "--output" filename)))
(`(file ,(and (pred stringp) as-filename))
(when (file-exists-p as-filename)
(error "File exists, will not overwrite: %S" as-filename))
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(setf filename (expand-file-name as-filename))
(list (cons "--output" filename))))))
((or 'put 'post)
(append (list (cons "--dump-header" "-")
(cons "--request" (upcase (symbol-name method))))
(pcase as
('file
(setf filename (make-temp-file "plz-"))
(list (cons "--output" filename)))
(`(file ,(and (pred stringp) as-filename))
(when (file-exists-p as-filename)
(error "File exists, will not overwrite: %S" as-filename))
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(setf filename (expand-file-name as-filename))
(list (cons "--output" filename))))
(list
;; It appears that this must be the last argument
;; in order to pass data on the rest of STDIN.
(pcase body
(`(file ,filename)
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(cons "--upload-file" (expand-file-name filename)))
(_ (cons data-arg "@-"))))))
('delete
(append (list (cons "--dump-header" "-")
(cons "--request" (upcase (symbol-name method))))
(pcase as
('file
(setf filename (make-temp-file "plz-"))
(list (cons "--output" filename)))
(`(file ,(and (pred stringp) as-filename))
(when (file-exists-p as-filename)
(error "File exists, will not overwrite: %S" as-filename))
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(setf filename (expand-file-name as-filename))
(list (cons "--output" filename))))))
(list
;; It appears that this must be the last argument
;; in order to pass data on the rest of STDIN.
(pcase body
(`(file ,filename)
;; Use `expand-file-name' because curl doesn't
;; expand, e.g. "~" into "/home/...".
(cons "--upload-file" (expand-file-name filename)))
(_ (cons data-arg "@-")))))
('head
(list (cons "--head" "")
(cons "--request" "HEAD"))))))
(list (cons "--head" ""))))))
(curl-config (cl-loop for (key . value) in curl-config-args
concat (format "%s \"%s\"\n" key value)))
(decode (pcase as
Expand Down
10 changes: 10 additions & 0 deletions tests/test-plz.el
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ in URL-encoded form)."
;; It's a temp file, so it should always be deleted.
(delete-file filename))))

(plz-deftest plz-head-temp-file ()
(let ((filename (plz 'head (url "/image/jpeg")
:as 'file :then 'sync)))
(unwind-protect
(with-temp-buffer
(insert-file-contents filename)
(should (re-search-forward "Content-Type: image/jpeg")))
;; It's a temp file, so it should always be deleted.
(delete-file filename))))

(plz-deftest plz-get-named-file ()
(let ((filename (make-temp-file "plz-")))
;; HACK: Delete the temp file and reuse its name, because
Expand Down

0 comments on commit 9fb22d8

Please sign in to comment.