Current section
Files
Jump to
Current section
Files
src/nori@response.erl
-module(nori@response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/nori/response.gleam").
-export([new/1, with_content/3, empty_link/0, link_to_operation/1, status_code_to_string/1, parse_status_code/1]).
-export_type([response/0, link/0, 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(
" Response types for OpenAPI specifications.\n"
"\n"
" Describes the responses for an operation.\n"
).
-type response() :: {response,
binary(),
gleam@dict:dict(binary(), nori@reference:ref(nori@parameter:header())),
gleam@dict:dict(binary(), nori@parameter:media_type()),
gleam@dict:dict(binary(), nori@reference:ref(link())),
gleam@dict:dict(binary(), gleam@json:json())}.
-type link() :: {link,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@dict:dict(binary(), gleam@json:json()),
gleam@option:option(gleam@json:json()),
gleam@option:option(binary()),
gleam@option:option(nori@server:server()),
gleam@dict:dict(binary(), gleam@json:json())}.
-type status_code() :: {status, integer()} |
info |
success |
redirect |
client_error |
server_error |
default.
-file("src/nori/response.gleam", 68).
?DOC(" Creates a `Response` with just a description.\n").
-spec new(binary()) -> response().
new(Description) ->
{response, Description, maps:new(), maps:new(), maps:new(), maps:new()}.
-file("src/nori/response.gleam", 79).
?DOC(" Creates a `Response` with description and content.\n").
-spec with_content(binary(), binary(), nori@parameter:media_type()) -> response().
with_content(Description, Content_type, Media_type) ->
{response,
Description,
maps:new(),
maps:from_list([{Content_type, Media_type}]),
maps:new(),
maps:new()}.
-file("src/nori/response.gleam", 94).
?DOC(" Creates an empty `Link`.\n").
-spec empty_link() -> link().
empty_link() ->
{link, none, none, maps:new(), none, none, none, maps:new()}.
-file("src/nori/response.gleam", 107).
?DOC(" Creates a `Link` with an operation ID reference.\n").
-spec link_to_operation(binary()) -> link().
link_to_operation(Operation_id) ->
{link, none, {some, Operation_id}, maps:new(), none, none, none, maps:new()}.
-file("src/nori/response.gleam", 120).
?DOC(" Converts a `StatusCode` to its string representation.\n").
-spec status_code_to_string(status_code()) -> binary().
status_code_to_string(Code) ->
case Code of
{status, N} ->
erlang:integer_to_binary(N);
info ->
<<"1XX"/utf8>>;
success ->
<<"2XX"/utf8>>;
redirect ->
<<"3XX"/utf8>>;
client_error ->
<<"4XX"/utf8>>;
server_error ->
<<"5XX"/utf8>>;
default ->
<<"default"/utf8>>
end.
-file("src/nori/response.gleam", 133).
?DOC(" Parses a status code string.\n").
-spec parse_status_code(binary()) -> {ok, status_code()} | {error, nil}.
parse_status_code(S) ->
case S of
<<"1XX"/utf8>> ->
{ok, info};
<<"2XX"/utf8>> ->
{ok, success};
<<"3XX"/utf8>> ->
{ok, redirect};
<<"4XX"/utf8>> ->
{ok, client_error};
<<"5XX"/utf8>> ->
{ok, server_error};
<<"default"/utf8>> ->
{ok, default};
Other ->
case gleam_stdlib:parse_int(Other) of
{ok, N} when (N >= 100) andalso (N < 600) ->
{ok, {status, N}};
_ ->
{error, nil}
end
end.