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

Add support for debugging HTTP responses #16

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions plz-media-type.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@
(require 'eieio)
(require 'plz)

(defcustom plz-media-type-debug-response-buffer "*plz-media-type-response*"
"The name of the buffer to which the HTTP response is written to.

When `plz-media-type-debug-p' is non-nil, the HTTP response will
be written to this buffer. The buffer will be erased on each
HTTP request, unless configured otherwise with
`plz-media-type-debug-erase-buffer-p'."
:group 'plz-media-type
:safe #'stringp
:type 'string)

(defcustom plz-media-type-debug-erase-buffer-p t
"Whether to erase the response buffer on each new request or not."
:group 'plz-media-type
:safe #'booleanp
:type 'boolean)

(defcustom plz-media-type-debug-p nil
"Whether to write the HTTP response to the debug buffer or not."
:group 'plz-media-type
:safe #'booleanp
:type 'boolean)

(defclass plz-media-type ()
((coding-system
:documentation "The coding system to use for the media type."
Expand Down Expand Up @@ -221,6 +244,9 @@ MEDIA-TYPES is an association list from media type to an
instance of a content type class.

STRING which is output just received from the process."
(when plz-media-type-debug-p
(with-current-buffer (get-buffer-create plz-media-type-debug-response-buffer)
(insert string)))
(when (buffer-live-p (process-buffer process))
(with-current-buffer (process-buffer process)
(let ((moving (= (point) (process-mark process))))
Expand Down Expand Up @@ -677,6 +703,9 @@ not.
(`(media-types ,media-types)
media-types)))
(let ((buffer))
(when (and plz-media-type-debug-p plz-media-type-debug-erase-buffer-p)
(with-current-buffer (get-buffer-create plz-media-type-debug-response-buffer)
(erase-buffer)))
(condition-case error
(let* ((plz-curl-default-args (cons "--no-buffer" plz-curl-default-args))
(result (plz method url
Expand Down
15 changes: 15 additions & 0 deletions plz-media-type.org
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ media type formats.
- [[#usage][Usage]]
- [[#quick-start][Quick Start]]
- [[#media-types][Media Types]]
- [[#troubleshooting][Troubleshooting]]
- [[#debugging-http-responses][Debugging HTTP responses]]
- [[#credits][Credits]]
- [[#copyright-assignment][Copyright assignment]]
:END:
Expand Down Expand Up @@ -333,6 +335,19 @@ does not parse the response body in any way.
"{\n \"slideshow\": {\n \"author\": \"Yours Truly\", \n \"date\": \"date of publication\", \n \"slides\": [\n {\n \"title\": \"Wake up to WonderWidgets!\", \n \"type\": \"all\"\n }, \n {\n \"items\": [\n \"Why <em>WonderWidgets</em> are great\", \n \"Who <em>buys</em> WonderWidgets\"\n ], \n \"title\": \"Overview\", \n \"type\": \"all\"\n }\n ], \n \"title\": \"Sample Slide Show\"\n }\n}\n")
#+end_src

* Troubleshooting

** Debugging HTTP responses

It might be useful to see the full HTTP response while developing a
new media type format or ~plz-media-type~ is failing to parse a
response of an HTTP request. When the ~plz-media-type-debug-p~ custom
variable is set to a non-nil value, each chunk of the HTTP response is
written to the buffer configured by the
~plz-media-type-debug-response-buffer~ custom variable as it
arrives. The buffer is cleared on each new request, unless
~plz-media-type-debug-erase-buffer-p~ is set to a non-nil value.

* Credits

- Thanks to [[https://github.com/ahyatt][ahyatt]] and [[https://github.com/alphapapa][alphapapa]] for their help and advice.
Expand Down
Loading