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

Adding constants for status codes #490

Closed
tbonza opened this issue Dec 31, 2019 · 4 comments
Closed

Adding constants for status codes #490

tbonza opened this issue Dec 31, 2019 · 4 comments

Comments

@tbonza
Copy link

tbonza commented Dec 31, 2019

Julia 1.3.0
HTTP.jl 0.8.8
MbedTLS.jl 0.7

Python requests has constants available for HTTP status codes. Does it make sense to add them?

I don't mind doing it. Just need some high level guidance about your design patterns. Here's an example of an implementation I'm using in a project I'm currently doing:

module Codes
    
    # Informational

    OK = 200
    CREATED = 201

    # Client Error.

    BAD_REQUEST = 400
    UNAUTHORIZED = 401
end

And then used as

r.status == Codes.OK # true

Adding constants may be outside of the scope of what you're trying to do here and that's totally fine too. Is there a need for a higher level HTTP API?

@quinnj
Copy link
Member

quinnj commented Dec 31, 2019

Currently, we just have https://github.com/JuliaWeb/HTTP.jl/blob/master/src/status_messages.jl, which allows getting a human-readable status for a status code. I wouldn't mind having a StatusCodes module as you outlined and moving the current STATUS_MESSAGES structure into it. I'd say go ahead w/ a PR and we can review there.

@tbonza
Copy link
Author

tbonza commented Jan 25, 2020

Currently working on this issue: https://github.com/tbonza/HTTP.jl/tree/devtb

Having issues getting the module imports to line up. Will continue looking into it.

@AtsushiSakai
Copy link
Contributor

I agree adding a functionality to search a status code from human-readable status would be great for many server applications!!.

Adding status code constants is great, but I think just adding a very simple function like this would be good.

julia> function statuscode(msg)
           for pair in pairs(HTTP.Messages.STATUS_MESSAGES)
               pair.second == msg && return pair.first
           end
           throw(ArgumentError("Unknown status message:"*msg))
       end
statuscode (generic function with 1 method)

julia> statuscode("OK")
200

julia> statuscode("hoge")
ERROR: ArgumentError: Unknown status message:hoge
Stacktrace:
 [1] statuscode(::String) at ./REPL[16]:5
 [2] top-level scope at REPL[18]:1

@AtsushiSakai
Copy link
Contributor

@quinnj Thank you for merging PR #982 . I think this issue can be closed.

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

No branches or pull requests

5 participants