Packages
oaspec
0.17.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@codegen@client_security.erl
-module(oaspec@codegen@client_security).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/client_security.gleam").
-export([capitalize_first/1, generate_security_or_chain/4, maybe_percent_encode/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/oaspec/codegen/client_security.gleam", 12).
?DOC(" Capitalize the first letter of a string (for HTTP scheme prefix).\n").
-spec capitalize_first(binary()) -> binary().
capitalize_first(Value) ->
case gleam_stdlib:string_pop_grapheme(Value) of
{ok, {First, Rest}} ->
<<(string:uppercase(First))/binary, Rest/binary>>;
{error, _} ->
Value
end.
-file("src/oaspec/codegen/client_security.gleam", 185).
?DOC(" Generate the Some branch for a single scheme (the apply-credential line).\n").
-spec generate_scheme_some_branch(
gleam@string_tree:string_tree(),
oaspec@codegen@context:context(),
oaspec@openapi@spec:security_scheme_ref(),
integer()
) -> gleam@string_tree:string_tree().
generate_scheme_some_branch(Sb, Ctx, Scheme_ref, Indent) ->
case erlang:element(5, oaspec@codegen@context:spec(Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Scheme_ref)
) of
{ok, {value, {api_key_scheme, Header_name, scheme_in_header}}} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
Indent,
<<<<"Some(key) -> request.set_header(req, \""/utf8,
(string:lowercase(Header_name))/binary>>/binary,
"\", key)"/utf8>>
);
{ok, {value, {api_key_scheme, Query_name, scheme_in_query}}} ->
_pipe@1 = Sb,
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
Indent,
<<"Some(key) -> {"/utf8>>
),
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
Indent + 1,
<<"let sep = case string.contains(req.path, \"?\") {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
Indent + 2,
<<"True -> \"&\""/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
Indent + 2,
<<"False -> \"?\""/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
Indent + 1,
<<"}"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
Indent + 1,
<<<<"request.Request(..req, path: req.path <> sep <> \""/utf8,
Query_name/binary>>/binary,
"=\" <> key)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@7,
Indent,
<<"}"/utf8>>
);
{ok, {value, {api_key_scheme, Cookie_name, scheme_in_cookie}}} ->
_pipe@8 = Sb,
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
Indent,
<<"Some(value) -> {"/utf8>>
),
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
Indent + 1,
<<"let existing = list.key_find(req.headers, \"cookie\") |> result.unwrap(\"\")"/utf8>>
),
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
Indent + 1,
<<<<"let cookie_val = \""/utf8, Cookie_name/binary>>/binary,
"=\" <> value"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
Indent + 1,
<<"let new_cookie = case existing {"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
Indent + 2,
<<"\"\" -> cookie_val"/utf8>>
),
_pipe@14 = oaspec@util@string_extra:indent(
_pipe@13,
Indent + 2,
<<"_ -> existing <> \"; \" <> cookie_val"/utf8>>
),
_pipe@15 = oaspec@util@string_extra:indent(
_pipe@14,
Indent + 1,
<<"}"/utf8>>
),
_pipe@16 = oaspec@util@string_extra:indent(
_pipe@15,
Indent + 1,
<<"request.set_header(req, \"cookie\", new_cookie)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@16,
Indent,
<<"}"/utf8>>
);
{ok, {value, {http_scheme, <<"basic"/utf8>>, _}}} ->
_pipe@17 = Sb,
oaspec@util@string_extra:indent(
_pipe@17,
Indent,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Basic \" <> token)"/utf8>>
);
{ok, {value, {http_scheme, <<"digest"/utf8>>, _}}} ->
_pipe@18 = Sb,
oaspec@util@string_extra:indent(
_pipe@18,
Indent,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Digest \" <> token)"/utf8>>
);
{ok, {value, {http_scheme, <<"bearer"/utf8>>, _}}} ->
_pipe@19 = Sb,
oaspec@util@string_extra:indent(
_pipe@19,
Indent,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Bearer \" <> token)"/utf8>>
);
{ok, {value, {o_auth2_scheme, _, _}}} ->
_pipe@19 = Sb,
oaspec@util@string_extra:indent(
_pipe@19,
Indent,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Bearer \" <> token)"/utf8>>
);
{ok, {value, {open_id_connect_scheme, _, _}}} ->
_pipe@19 = Sb,
oaspec@util@string_extra:indent(
_pipe@19,
Indent,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Bearer \" <> token)"/utf8>>
);
{ok, {value, {http_scheme, Scheme_name, _}}} ->
_pipe@20 = Sb,
oaspec@util@string_extra:indent(
_pipe@20,
Indent,
<<<<"Some(token) -> request.set_header(req, \"authorization\", \""/utf8,
(capitalize_first(Scheme_name))/binary>>/binary,
" \" <> token)"/utf8>>
);
_ ->
Sb
end;
_ ->
Sb
end.
-file("src/oaspec/codegen/client_security.gleam", 283).
?DOC(" Generate scheme application using a known value variable (for AND tuple matches).\n").
-spec generate_scheme_apply(
gleam@string_tree:string_tree(),
oaspec@codegen@context:context(),
oaspec@openapi@spec:security_scheme_ref(),
binary(),
integer()
) -> gleam@string_tree:string_tree().
generate_scheme_apply(Sb, Ctx, Scheme_ref, Val_var, Indent) ->
case erlang:element(5, oaspec@codegen@context:spec(Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Scheme_ref)
) of
{ok, {value, {api_key_scheme, Header_name, scheme_in_header}}} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
Indent,
<<<<<<<<"let req = request.set_header(req, \""/utf8,
(string:lowercase(Header_name))/binary>>/binary,
"\", "/utf8>>/binary,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {api_key_scheme, Query_name, scheme_in_query}}} ->
_pipe@1 = Sb,
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
Indent,
<<"let sep = case string.contains(req.path, \"?\") {"/utf8>>
),
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
Indent + 1,
<<"True -> \"&\""/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
Indent + 1,
<<"False -> \"?\""/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
Indent,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@5,
Indent,
<<<<<<<<"let req = request.Request(..req, path: req.path <> sep <> \""/utf8,
Query_name/binary>>/binary,
"=\" <> "/utf8>>/binary,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {api_key_scheme, Cookie_name, scheme_in_cookie}}} ->
_pipe@6 = Sb,
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
Indent,
<<"let existing = list.key_find(req.headers, \"cookie\") |> result.unwrap(\"\")"/utf8>>
),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
Indent,
<<<<<<"let cookie_val = \""/utf8, Cookie_name/binary>>/binary,
"=\" <> "/utf8>>/binary,
Val_var/binary>>
),
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
Indent,
<<"let new_cookie = case existing {"/utf8>>
),
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
Indent + 1,
<<"\"\" -> cookie_val"/utf8>>
),
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
Indent + 1,
<<"_ -> existing <> \"; \" <> cookie_val"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
Indent,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@12,
Indent,
<<"let req = request.set_header(req, \"cookie\", new_cookie)"/utf8>>
);
{ok, {value, {http_scheme, <<"basic"/utf8>>, _}}} ->
_pipe@13 = Sb,
oaspec@util@string_extra:indent(
_pipe@13,
Indent,
<<<<"let req = request.set_header(req, \"authorization\", \"Basic \" <> "/utf8,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {http_scheme, <<"digest"/utf8>>, _}}} ->
_pipe@14 = Sb,
oaspec@util@string_extra:indent(
_pipe@14,
Indent,
<<<<"let req = request.set_header(req, \"authorization\", \"Digest \" <> "/utf8,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {http_scheme, <<"bearer"/utf8>>, _}}} ->
_pipe@15 = Sb,
oaspec@util@string_extra:indent(
_pipe@15,
Indent,
<<<<"let req = request.set_header(req, \"authorization\", \"Bearer \" <> "/utf8,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {o_auth2_scheme, _, _}}} ->
_pipe@15 = Sb,
oaspec@util@string_extra:indent(
_pipe@15,
Indent,
<<<<"let req = request.set_header(req, \"authorization\", \"Bearer \" <> "/utf8,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {open_id_connect_scheme, _, _}}} ->
_pipe@15 = Sb,
oaspec@util@string_extra:indent(
_pipe@15,
Indent,
<<<<"let req = request.set_header(req, \"authorization\", \"Bearer \" <> "/utf8,
Val_var/binary>>/binary,
")"/utf8>>
);
{ok, {value, {http_scheme, Scheme_name, _}}} ->
_pipe@16 = Sb,
oaspec@util@string_extra:indent(
_pipe@16,
Indent,
<<<<<<<<"let req = request.set_header(req, \"authorization\", \""/utf8,
(capitalize_first(Scheme_name))/binary>>/binary,
" \" <> "/utf8>>/binary,
Val_var/binary>>/binary,
")"/utf8>>
);
_ ->
Sb
end;
_ ->
Sb
end.
-file("src/oaspec/codegen/client_security.gleam", 114).
?DOC(" Generate a single security alternative (last in chain, None -> req).\n").
-spec generate_security_alternative(
gleam@string_tree:string_tree(),
oaspec@codegen@context:context(),
list(oaspec@openapi@spec:security_scheme_ref()),
integer(),
binary()
) -> gleam@string_tree:string_tree().
generate_security_alternative(Sb, Ctx, Schemes, Base_indent, Fallback) ->
case Schemes of
[] ->
Sb;
[Single_scheme] ->
Field_name = oaspec@util@naming:to_snake_case(
erlang:element(2, Single_scheme)
),
Sb@1 = begin
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
Base_indent,
<<<<"let req = case config."/utf8, Field_name/binary>>/binary,
" {"/utf8>>
)
end,
Sb@2 = generate_scheme_some_branch(
Sb@1,
Ctx,
Single_scheme,
Base_indent + 1
),
_pipe@1 = Sb@2,
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
Base_indent + 1,
<<"None -> "/utf8, Fallback/binary>>
),
oaspec@util@string_extra:indent(_pipe@2, Base_indent, <<"}"/utf8>>);
Schemes@1 ->
Fields = gleam@list:map(
Schemes@1,
fun(S) ->
<<"config."/utf8,
(oaspec@util@naming:to_snake_case(erlang:element(2, S)))/binary>>
end
),
Sb@3 = begin
_pipe@3 = Sb,
oaspec@util@string_extra:indent(
_pipe@3,
Base_indent,
<<<<"let req = case "/utf8,
(gleam@string:join(Fields, <<", "/utf8>>))/binary>>/binary,
" {"/utf8>>
)
end,
Some_patterns = gleam@list:map(
Schemes@1,
fun(S@1) ->
<<<<"Some("/utf8,
(oaspec@util@naming:to_snake_case(
erlang:element(2, S@1)
))/binary>>/binary,
"_val)"/utf8>>
end
),
Sb@4 = begin
_pipe@4 = Sb@3,
oaspec@util@string_extra:indent(
_pipe@4,
Base_indent + 1,
<<(gleam@string:join(Some_patterns, <<", "/utf8>>))/binary,
" -> {"/utf8>>
)
end,
Sb@6 = gleam@list:fold(
Schemes@1,
Sb@4,
fun(Sb@5, Scheme_ref) ->
generate_scheme_apply(
Sb@5,
Ctx,
Scheme_ref,
<<(oaspec@util@naming:to_snake_case(
erlang:element(2, Scheme_ref)
))/binary,
"_val"/utf8>>,
Base_indent + 2
)
end
),
Sb@7 = begin
_pipe@5 = Sb@6,
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
Base_indent + 2,
<<"req"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@6,
Base_indent + 1,
<<"}"/utf8>>
)
end,
Wildcard = begin
_pipe@7 = gleam@list:map(Schemes@1, fun(_) -> <<"_"/utf8>> end),
gleam@string:join(_pipe@7, <<", "/utf8>>)
end,
_pipe@8 = Sb@7,
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
Base_indent + 1,
<<<<Wildcard/binary, " -> "/utf8>>/binary, Fallback/binary>>
),
oaspec@util@string_extra:indent(_pipe@9, Base_indent, <<"}"/utf8>>)
end.
-file("src/oaspec/codegen/client_security.gleam", 23).
?DOC(
" Generate a chain of OR alternatives for security requirements.\n"
" Each alternative is tried in order; the first one with all credentials\n"
" present is applied. If none match, req is returned unchanged.\n"
).
-spec generate_security_or_chain(
gleam@string_tree:string_tree(),
oaspec@codegen@context:context(),
list(oaspec@openapi@spec:security_requirement()),
integer()
) -> gleam@string_tree:string_tree().
generate_security_or_chain(Sb, Ctx, Alternatives, Base_indent) ->
case Alternatives of
[] ->
Sb;
[Alt] ->
generate_security_alternative(
Sb,
Ctx,
erlang:element(2, Alt),
Base_indent,
<<"req"/utf8>>
);
[Alt@1 | Rest] ->
case erlang:element(2, Alt@1) of
[] ->
generate_security_or_chain(Sb, Ctx, Rest, Base_indent);
[Single_scheme] ->
Field_name = oaspec@util@naming:to_snake_case(
erlang:element(2, Single_scheme)
),
Sb@1 = begin
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
Base_indent,
<<<<"let req = case config."/utf8,
Field_name/binary>>/binary,
" {"/utf8>>
)
end,
Sb@2 = generate_scheme_some_branch(
Sb@1,
Ctx,
Single_scheme,
Base_indent + 1
),
Sb@3 = begin
_pipe@1 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@1,
Base_indent + 1,
<<"None -> {"/utf8>>
)
end,
Sb@4 = generate_security_or_chain(
Sb@3,
Ctx,
Rest,
Base_indent + 2
),
_pipe@2 = Sb@4,
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
Base_indent + 2,
<<"req"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
Base_indent + 1,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@4,
Base_indent,
<<"}"/utf8>>
);
Schemes ->
Fields = gleam@list:map(
Schemes,
fun(S) ->
<<"config."/utf8,
(oaspec@util@naming:to_snake_case(
erlang:element(2, S)
))/binary>>
end
),
Sb@5 = begin
_pipe@5 = Sb,
oaspec@util@string_extra:indent(
_pipe@5,
Base_indent,
<<<<"let req = case "/utf8,
(gleam@string:join(Fields, <<", "/utf8>>))/binary>>/binary,
" {"/utf8>>
)
end,
Some_patterns = gleam@list:map(
Schemes,
fun(S@1) ->
<<<<"Some("/utf8,
(oaspec@util@naming:to_snake_case(
erlang:element(2, S@1)
))/binary>>/binary,
"_val)"/utf8>>
end
),
Sb@6 = begin
_pipe@6 = Sb@5,
oaspec@util@string_extra:indent(
_pipe@6,
Base_indent + 1,
<<(gleam@string:join(Some_patterns, <<", "/utf8>>))/binary,
" -> {"/utf8>>
)
end,
Sb@8 = gleam@list:fold(
Schemes,
Sb@6,
fun(Sb@7, Scheme_ref) ->
generate_scheme_apply(
Sb@7,
Ctx,
Scheme_ref,
<<(oaspec@util@naming:to_snake_case(
erlang:element(2, Scheme_ref)
))/binary,
"_val"/utf8>>,
Base_indent + 2
)
end
),
Sb@9 = begin
_pipe@7 = Sb@8,
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
Base_indent + 2,
<<"req"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@8,
Base_indent + 1,
<<"}"/utf8>>
)
end,
Wildcard = begin
_pipe@9 = gleam@list:map(
Schemes,
fun(_) -> <<"_"/utf8>> end
),
gleam@string:join(_pipe@9, <<", "/utf8>>)
end,
Sb@10 = begin
_pipe@10 = Sb@9,
oaspec@util@string_extra:indent(
_pipe@10,
Base_indent + 1,
<<Wildcard/binary, " -> {"/utf8>>
)
end,
Sb@11 = generate_security_or_chain(
Sb@10,
Ctx,
Rest,
Base_indent + 2
),
_pipe@11 = Sb@11,
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
Base_indent + 2,
<<"req"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
Base_indent + 1,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@13,
Base_indent,
<<"}"/utf8>>
)
end
end.
-file("src/oaspec/codegen/client_security.gleam", 391).
?DOC(
" Wrap a value expression with uri.percent_encode or not, based on allowReserved.\n"
" When allowReserved is true, reserved characters are sent as-is per OpenAPI spec.\n"
).
-spec maybe_percent_encode(
binary(),
oaspec@openapi@spec:parameter(oaspec@openapi@spec:resolved())
) -> binary().
maybe_percent_encode(Value_expr, Param) ->
gleam@bool:guard(
erlang:element(10, Param),
Value_expr,
fun() ->
<<<<"uri.percent_encode("/utf8, Value_expr/binary>>/binary,
")"/utf8>>
end
).