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

feat: Allow specifying protocol version in request specs #75

Closed
wants to merge 8 commits into from
Closed

feat: Allow specifying protocol version in request specs #75

wants to merge 8 commits into from

Conversation

vHugoObject
Copy link

In reference to issue #65. Obviously url-retrieve does not support specifying a protocol, so this addition is really only useful for curl exports. If a specific http protocol is included in a request, the corresponding curl option is now added for the export.

Copy link
Owner

@federicotdn federicotdn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, before I review the code, would you mind applying formatting?
Mostly, calling indent-region on the code, and making sure that parenthesis are never on their own in a line, e.g.:

(defun verb--protocol-as-curl-option (protocol)
  "Return the corresponding curl option for
   a given http protocol."
    (unless (verb--http-protocol-p protocol)
      (user-error "Please pass a valid http protocol"))
    (let ((protocol-maps (list (cons "HTTP/0.9" "--http0.9")
			     (cons "HTTP/1.0" "--http1.0")
			     (cons "HTTP/1.1" "--http1.1")
			     (cons "HTTP/2" "--http2")
			     (cons "HTTP/3" "--http3"))))
  (alist-get protocol protocol-maps nil nil 'equal)))

instead of:


(defun verb--protocol-as-curl-option (protocol)
  "Return the corresponding curl option for
   a given http protocol."
    (unless (verb--http-protocol-p protocol)
      (user-error "Please pass a valid http protocol")
      )
    (let (
      (protocol-maps (list (cons "HTTP/0.9" "--http0.9")
			     (cons "HTTP/1.0" "--http1.0")
			     (cons "HTTP/1.1" "--http1.1")
			     (cons "HTTP/2" "--http2")
			     (cons "HTTP/3" "--http3")
			     ))
      )

  (alist-get protocol protocol-maps nil nil 'equal))    
    )

Plus, please remove all empty lines within functions, and between functions, please only leave one line. Additionally, we should replace all tabs with spaces.

@vHugoObject
Copy link
Author

Hey, I fixed the spacing and the parentheses. I also ran make check. However make check keeps giving me errors that would be resolved by compat. I am using seq-positions, string-split and flatten-tree. All of those functions are covered by compat, which it looks like you install for make check. On top of this, you require seq. Not sure how to resolve this issue.

@vHugoObject
Copy link
Author

So bumping the emacs version this package require and adding compat to the header resolved my issue - Package-Requires: ((emacs "29.1") (compat "30.0.0.0")). This was your previous header: Package-Requires: ((emacs "26.3")). According to the compat manual, compat needs to be apart of the Package-Requires list and seq-positions was added in emacs 29.1. If you don't want to bump the emacs version this package requires, I could also just copy the seq-positions function and add it to verb and then set this as the header - Package-Requires: ((emacs "26.3") (compat "30.0.0.0").

@federicotdn
Copy link
Owner

Hi, currently Verb targets Emacs 26 or newer (meaning that it can only use functions that were available up to Emacs 26).
The check recipe however is ran only under Emacs 29, in the CI. Tests are ran for both 26 and 29. Locally, it should be enough to just run the checks and tests using Emacs 29.
I've enabled the CI in this PR as well, which should help in debugging.

@vHugoObject
Copy link
Author

vHugoObject commented Sep 16, 2024 via email

Added a new constant, a list of valid http-protocols. Also added a function that checks if a
value is a member of this list along with a test for this function.
These tests are for the new protocol field that will be added to verb-request-spec.
This step is necessary as the plan is to turn the METHOD+URL section
into METHOD+URL+PROTOCOL, as suggested in the original issue: "get
http://example.com http/1.1". More code will obviously have to be
added in order match protocols so in this step I tried to simplify the
METHOD+URL section and extract out functions that could also be used
to match protocols. Tests were are added for all three functions that
I extracted out.
METHOD+URL section in verb-request-spec-from-string

The refactor started in my last commit is now finished. It would have
been very difficult to add protocol to the original METHOD+URL
section as it already contained nested conditional logic. Instead, I
wrote a verb--single-line-method-url-protocol, a function that splits
the method+url+protocol line in to parts. Method and protocol are then
validated using validation functions that I added for this commit while the
url is simply stored. If the url is on multiple lines, the lines are
concatenated together and then if a protocol is also specified, it's
stored. Once I tested this refactor, I added protocol to the
verb-request-spec created at the the of verb-request-spec-from-string function.
fix: Added compat and seq-positions
@vHugoObject
Copy link
Author

Done

@federicotdn
Copy link
Owner

Apologies for the delay in reviewing. Would it be possible to implement the functionality without adding the compat dependency? I do not want to add any dependencies to this project. To be honest I though this change would have required much less code, maybe I underestimated the problem.

@vHugoObject
Copy link
Author

I think I am just going to close the pull request. I thought you would be ok with compat as you were already using it as a dev dependency. I could remove it and replace the functions that use it but that certainly wouldn't resolve the problems you have with the amount of code I added. The main issue was I had to work around the existing code you were using to get the METHOD+URL line while of course making sure my changes didn't break anything. One approach would have been add two more nested if's on top of another regex. I believe that would have made the code very hard to read and debug, while also making the METHOD+URL+PROTOCOL way longer than necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants