Packages
oaspec
0.24.0
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.1
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.3
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.1.3
Generate Gleam code from OpenAPI 3.x specifications
Current section
Files
Jump to
Current section
Files
src/oaspec@util@http.erl
-module(oaspec@util@http).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/util/http.gleam").
-export([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.
-type http_status_code() :: {status, integer()} |
{status_range, integer()} |
default_status.
-file("src/oaspec/util/http.gleam", 17).
?DOC(
" Parse a raw status code string from an OpenAPI spec into an HttpStatusCode.\n"
" Returns Error(Nil) for unrecognisable strings.\n"
).
-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}};
_ ->
case gleam_stdlib:parse_int(Code) of
{ok, N} ->
{ok, {status, N}};
{error, _} ->
{error, nil}
end
end.
-file("src/oaspec/util/http.gleam", 34).
?DOC(" Convert an HttpStatusCode back to its canonical string form.\n").
-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/util/http.gleam", 45).
?DOC(
" Shared HTTP status code utilities for code generation.\n"
" Get a human-readable suffix for a given HTTP status code.\n"
" Used to build anonymous type names like \"ListPetsResponseOk\".\n"
).
-spec status_code_suffix(http_status_code()) -> binary().
status_code_suffix(Code) ->
case Code of
{status, 200} ->
<<"Ok"/utf8>>;
{status, 201} ->
<<"Created"/utf8>>;
{status, 204} ->
<<"NoContent"/utf8>>;
{status, 400} ->
<<"BadRequest"/utf8>>;
{status, 401} ->
<<"Unauthorized"/utf8>>;
{status, 403} ->
<<"Forbidden"/utf8>>;
{status, 404} ->
<<"NotFound"/utf8>>;
{status, 409} ->
<<"Conflict"/utf8>>;
{status, 422} ->
<<"UnprocessableEntity"/utf8>>;
{status, 500} ->
<<"InternalServerError"/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/util/http.gleam", 71).
?DOC(
" Convert an HttpStatusCode to a valid Gleam case pattern.\n"
" Exact codes become integer literals, range codes become guard expressions,\n"
" and DefaultStatus becomes \"_\" (catch-all).\n"
).
-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/util/http.gleam", 96).
?DOC(
" Convert an HttpStatusCode to an integer string for use in ServerResponse.\n"
" Range codes and DefaultStatus map to a representative status code.\n"
).
-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/util/http.gleam", 104).
-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/util/http.gleam", 86).
?DOC(
" Sort response entries so that exact codes come before ranges,\n"
" and ranges come before the default catch-all. This ensures\n"
" correct pattern matching order in generated case expressions.\n"
).
-spec sort_response_entries(list({http_status_code(), HKK})) -> list({http_status_code(),
HKK}).
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
).