Skip to content

Commit

Permalink
replace defcusom request-coding-system with key :coding-system
Browse files Browse the repository at this point in the history
Not tested yet!

This code removes the new `request-coding-system` defcustom, and
instead uses a keyword parameter to the main `request` function. Since
this parameter needs to be passed down the chain several times I'm not
entirely certain that I've made the changes everywhere they need to be
made.

This commit also shifts the default form 'binary to 'utf-8.
  • Loading branch information
titaniumbones committed Dec 2, 2018
1 parent cf8a069 commit 081f6bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ PUT JSON data including non-ascii strings:

.. code:: emacs-lisp
(setq request-coding-system 'utf-8)
(request
"http://httpbin.org/put"
:type "PUT"
:data (json-encode '(("key" . "値1") ("key2" . "値2")))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:coding-system 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
Expand Down
45 changes: 22 additions & 23 deletions request.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@
"Directory to store data related to request.el."
:type 'directory)

(defcustom request-coding-system 'binary
"coding-system for request."
:type 'symbol)

(defcustom request-curl "curl"
"Executable for curl command."
:type 'string)
Expand Down Expand Up @@ -398,6 +394,7 @@ Example::
(files nil)
(parser nil)
(headers nil)
(coding-system 'utf-8)
(success nil)
(error nil)
(complete nil)
Expand All @@ -413,18 +410,19 @@ Request.el has a single entry point. It is `request'.
==================== ========================================================
Keyword argument Explanation
==================== ========================================================
TYPE (string) type of request to make: POST/GET/PUT/DELETE
PARAMS (alist) set \"?key=val\" part in URL
DATA (string/alist) data to be sent to the server
FILES (alist) files to be sent to the server (see below)
PARSER (symbol) a function that reads current buffer and return data
HEADERS (alist) additional headers to send with the request
SUCCESS (function) called on success
ERROR (function) called on error
COMPLETE (function) called on both success and error
TIMEOUT (number) timeout in second
STATUS-CODE (alist) map status code (int) to callback
SYNC (bool) If `t', wait until request is done. Default is `nil'.
TYPE (string) type of request to make: POST/GET/PUT/DELETE
PARAMS (alist) set \"?key=val\" part in URL
DATA (string/alist) data to be sent to the server
FILES (alist) files to be sent to the server (see below)
PARSER (symbol) a function that reads current buffer and return data
HEADERS (alist) additional headers to send with the request
CODING-SYSTEM (symbol) coding system for tempfiles ('utf by default)
SUCCESS (function) called on success
ERROR (function) called on error
COMPLETE (function) called on both success and error
TIMEOUT (number) timeout in second
STATUS-CODE (alist) map status code (int) to callback
SYNC (bool) If `t', wait until request is done. Default is `nil'.
==================== ========================================================
Expand Down Expand Up @@ -574,6 +572,7 @@ and requests.request_ (Python).
(request--urlencode-alist params))))
(setq settings (plist-put settings :url url))
(setq settings (plist-put settings :response response))
(setq settings (plist-put settings :coding-system coding-system))
(setf (request-response-settings response) settings)
(setf (request-response-url response) url)
(setf (request-response--backend response) request-backend)
Expand Down Expand Up @@ -900,7 +899,7 @@ Currently it is used only for testing.")
(make-directory (file-name-directory (request--curl-cookie-jar)) t)))

(cl-defun request--curl-command
(url &key type data headers timeout response files* unix-socket
(url &key type data headers timeout response files* unix-socket coding-system
&allow-other-keys
&aux
(cookie-jar (convert-standard-filename
Expand All @@ -927,10 +926,10 @@ Currently it is used only for testing.")
(let ((tempfile (request--make-temp-file)))
(push tempfile (request-response--tempfiles response))
(let ((file-coding-system-alist nil)
(coding-system-for-write request-coding-system))
(coding-system-for-write coding-system))
(with-temp-file tempfile
(setq buffer-file-coding-system request-coding-system)
(if (eq request-coding-system 'binary)
(setq buffer-file-coding-system coding-system)
(if (eq coding-system 'binary)
(set-buffer-multibyte nil))
(insert data)))
(list "--data-binary" (concat "@" (request-untrampify-filename tempfile)))))
Expand Down Expand Up @@ -1009,7 +1008,7 @@ temporary file paths."
files))

(cl-defun request--curl (url &rest settings
&key type data files headers timeout response
&key type data files headers timeout response coding-system
&allow-other-keys)
"cURL-based request backend.
Expand Down Expand Up @@ -1046,12 +1045,12 @@ removed from the buffer before it is shown to the parser function.
(request--curl-normalize-files files)
(setf (request-response--tempfiles response) tempfiles)
(apply #'request--curl-command url :files* files*
:response response settings)))
:response response :coding-system coding-system settings)))
(proc (apply #'start-file-process "request curl" buffer command)))
(request-log 'debug "Run: %s" (mapconcat 'identity command " "))
(setf (request-response--buffer response) buffer)
(process-put proc :request-response response)
(set-process-coding-system proc request-coding-system request-coding-system)
(set-process-coding-system proc coding-system coding-system)
(set-process-query-on-exit-flag proc nil)
(set-process-sentinel proc #'request--curl-callback)))

Expand Down
2 changes: 1 addition & 1 deletion tests/test-request.el
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ To check that, run test with:

(request-deftest request-simple-post-multibyte-json ()
:backends (curl)
(setq request-coding-system 'utf-8)
(request-testing-with-response-slots
(request-testing-sync "report/some-path"
:type "POST" :data "{\"\": \"\"}"
:headers '(("Content-Type" . "application/json"))
:coding-system 'utf-8
:parser 'json-read)
(should (equal status-code 200))
(should (equal (assoc-default 'path data) "some-path"))
Expand Down

0 comments on commit 081f6bf

Please sign in to comment.