HTTP.jl icon indicating copy to clipboard operation
HTTP.jl copied to clipboard

Adding constants for status codes

Open tbonza opened this issue 5 years ago • 3 comments

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?

tbonza avatar Dec 31 '19 15:12 tbonza

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.

quinnj avatar Dec 31 '19 21:12 quinnj

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.

tbonza avatar Jan 25 '20 00:01 tbonza

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 avatar Feb 27 '21 06:02 AtsushiSakai

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

AtsushiSakai avatar Jan 07 '23 08:01 AtsushiSakai