-module(oaspec@internal@util@http). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/oaspec/internal/util/http.gleam"). -export([http_status_from_int/1, parse_status_code/1, status_code_to_string/1, status_code_suffix/1, status_code_to_int_pattern/1, status_code_to_int/1, sort_response_entries/1]). -export_type([http_status_code/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -type http_status_code() :: {status, integer()} | {status_range, integer()} | default_status. -file("src/oaspec/internal/util/http.gleam", 50). ?DOC(false). -spec http_status_from_int(integer()) -> {ok, http_status_code()} | {error, nil}. http_status_from_int(N) -> gleam@bool:guard( (N < 100) orelse (N > 599), {error, nil}, fun() -> {ok, {status, N}} end ). -file("src/oaspec/internal/util/http.gleam", 55). ?DOC(false). -spec parse_canonical_http_status(binary()) -> {ok, http_status_code()} | {error, nil}. parse_canonical_http_status(Code) -> gleam@result:'try'( gleam_stdlib:parse_int(Code), fun(N) -> gleam@bool:guard( erlang:integer_to_binary(N) /= Code, {error, nil}, fun() -> gleam@bool:guard( (N < 100) orelse (N > 599), {error, nil}, fun() -> {ok, {status, N}} end ) end ) end ). -file("src/oaspec/internal/util/http.gleam", 29). ?DOC(false). -spec parse_status_code(binary()) -> {ok, http_status_code()} | {error, nil}. parse_status_code(Code) -> case Code of <<"default"/utf8>> -> {ok, default_status}; <<"1XX"/utf8>> -> {ok, {status_range, 1}}; <<"1xx"/utf8>> -> {ok, {status_range, 1}}; <<"2XX"/utf8>> -> {ok, {status_range, 2}}; <<"2xx"/utf8>> -> {ok, {status_range, 2}}; <<"3XX"/utf8>> -> {ok, {status_range, 3}}; <<"3xx"/utf8>> -> {ok, {status_range, 3}}; <<"4XX"/utf8>> -> {ok, {status_range, 4}}; <<"4xx"/utf8>> -> {ok, {status_range, 4}}; <<"5XX"/utf8>> -> {ok, {status_range, 5}}; <<"5xx"/utf8>> -> {ok, {status_range, 5}}; _ -> parse_canonical_http_status(Code) end. -file("src/oaspec/internal/util/http.gleam", 70). ?DOC(false). -spec status_code_to_string(http_status_code()) -> binary(). status_code_to_string(Code) -> case Code of {status, N} -> erlang:integer_to_binary(N); {status_range, N@1} -> <<(erlang:integer_to_binary(N@1))/binary, "XX"/utf8>>; default_status -> <<"default"/utf8>> end. -file("src/oaspec/internal/util/http.gleam", 88). ?DOC(false). -spec status_code_suffix(http_status_code()) -> binary(). status_code_suffix(Code) -> case Code of {status, 100} -> <<"Continue"/utf8>>; {status, 101} -> <<"SwitchingProtocols"/utf8>>; {status, 102} -> <<"Processing"/utf8>>; {status, 103} -> <<"EarlyHints"/utf8>>; {status, 200} -> <<"Ok"/utf8>>; {status, 201} -> <<"Created"/utf8>>; {status, 202} -> <<"Accepted"/utf8>>; {status, 203} -> <<"NonAuthoritativeInformation"/utf8>>; {status, 204} -> <<"NoContent"/utf8>>; {status, 205} -> <<"ResetContent"/utf8>>; {status, 206} -> <<"PartialContent"/utf8>>; {status, 207} -> <<"MultiStatus"/utf8>>; {status, 208} -> <<"AlreadyReported"/utf8>>; {status, 226} -> <<"ImUsed"/utf8>>; {status, 300} -> <<"MultipleChoices"/utf8>>; {status, 301} -> <<"MovedPermanently"/utf8>>; {status, 302} -> <<"Found"/utf8>>; {status, 303} -> <<"SeeOther"/utf8>>; {status, 304} -> <<"NotModified"/utf8>>; {status, 305} -> <<"UseProxy"/utf8>>; {status, 307} -> <<"TemporaryRedirect"/utf8>>; {status, 308} -> <<"PermanentRedirect"/utf8>>; {status, 400} -> <<"BadRequest"/utf8>>; {status, 401} -> <<"Unauthorized"/utf8>>; {status, 402} -> <<"PaymentRequired"/utf8>>; {status, 403} -> <<"Forbidden"/utf8>>; {status, 404} -> <<"NotFound"/utf8>>; {status, 405} -> <<"MethodNotAllowed"/utf8>>; {status, 406} -> <<"NotAcceptable"/utf8>>; {status, 407} -> <<"ProxyAuthenticationRequired"/utf8>>; {status, 408} -> <<"RequestTimeout"/utf8>>; {status, 409} -> <<"Conflict"/utf8>>; {status, 410} -> <<"Gone"/utf8>>; {status, 411} -> <<"LengthRequired"/utf8>>; {status, 412} -> <<"PreconditionFailed"/utf8>>; {status, 413} -> <<"ContentTooLarge"/utf8>>; {status, 414} -> <<"UriTooLong"/utf8>>; {status, 415} -> <<"UnsupportedMediaType"/utf8>>; {status, 416} -> <<"RangeNotSatisfiable"/utf8>>; {status, 417} -> <<"ExpectationFailed"/utf8>>; {status, 418} -> <<"IAmATeapot"/utf8>>; {status, 421} -> <<"MisdirectedRequest"/utf8>>; {status, 422} -> <<"UnprocessableEntity"/utf8>>; {status, 423} -> <<"Locked"/utf8>>; {status, 424} -> <<"FailedDependency"/utf8>>; {status, 425} -> <<"TooEarly"/utf8>>; {status, 426} -> <<"UpgradeRequired"/utf8>>; {status, 428} -> <<"PreconditionRequired"/utf8>>; {status, 429} -> <<"TooManyRequests"/utf8>>; {status, 431} -> <<"RequestHeaderFieldsTooLarge"/utf8>>; {status, 451} -> <<"UnavailableForLegalReasons"/utf8>>; {status, 500} -> <<"InternalServerError"/utf8>>; {status, 501} -> <<"NotImplemented"/utf8>>; {status, 502} -> <<"BadGateway"/utf8>>; {status, 503} -> <<"ServiceUnavailable"/utf8>>; {status, 504} -> <<"GatewayTimeout"/utf8>>; {status, 505} -> <<"HttpVersionNotSupported"/utf8>>; {status, 506} -> <<"VariantAlsoNegotiates"/utf8>>; {status, 507} -> <<"InsufficientStorage"/utf8>>; {status, 508} -> <<"LoopDetected"/utf8>>; {status, 510} -> <<"NotExtended"/utf8>>; {status, 511} -> <<"NetworkAuthenticationRequired"/utf8>>; {status, N} -> <<"Status"/utf8, (erlang:integer_to_binary(N))/binary>>; {status_range, 1} -> <<"Status1xx"/utf8>>; {status_range, 2} -> <<"Status2xx"/utf8>>; {status_range, 3} -> <<"Status3xx"/utf8>>; {status_range, 4} -> <<"Status4xx"/utf8>>; {status_range, 5} -> <<"Status5xx"/utf8>>; {status_range, N@1} -> <<<<"Status"/utf8, (erlang:integer_to_binary(N@1))/binary>>/binary, "xx"/utf8>>; default_status -> <<"Default"/utf8>> end. -file("src/oaspec/internal/util/http.gleam", 172). ?DOC(false). -spec status_code_to_int_pattern(http_status_code()) -> binary(). status_code_to_int_pattern(Code) -> case Code of default_status -> <<"_"/utf8>>; {status_range, N} -> Low = erlang:integer_to_binary(N * 100), High = erlang:integer_to_binary((N * 100) + 99), <<<<<<"status if status >= "/utf8, Low/binary>>/binary, " && status <= "/utf8>>/binary, High/binary>>; {status, N@1} -> erlang:integer_to_binary(N@1) end. -file("src/oaspec/internal/util/http.gleam", 197). ?DOC(false). -spec status_code_to_int(http_status_code()) -> binary(). status_code_to_int(Code) -> case Code of default_status -> <<"500"/utf8>>; {status_range, N} -> erlang:integer_to_binary(N * 100); {status, N@1} -> erlang:integer_to_binary(N@1) end. -file("src/oaspec/internal/util/http.gleam", 205). ?DOC(false). -spec status_sort_priority(http_status_code()) -> integer(). status_sort_priority(Code) -> case Code of default_status -> 9999; {status_range, N} -> (N * 100) + 1000; {status, N@1} -> N@1 end. -file("src/oaspec/internal/util/http.gleam", 187). ?DOC(false). -spec sort_response_entries(list({http_status_code(), HTI})) -> list({http_status_code(), HTI}). sort_response_entries(Entries) -> gleam@list:sort( Entries, fun(A, B) -> gleam@int:compare( status_sort_priority(erlang:element(1, A)), status_sort_priority(erlang:element(1, B)) ) end ).