HTTP.jl
HTTP.jl copied to clipboard
Adding constants for status codes
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?
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.
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.
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
@quinnj Thank you for merging PR #982 . I think this issue can be closed.