Current section

Files

Jump to
oaspec src oaspec@internal@codegen@client_security.erl
Raw

src/oaspec@internal@codegen@client_security.erl

-module(oaspec@internal@codegen@client_security).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/internal/codegen/client_security.gleam").
-export([capitalize_first/1, render_security_metadata/2, 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.
?MODULEDOC(false).
-file("src/oaspec/internal/codegen/client_security.gleam", 12).
?DOC(false).
-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/internal/codegen/client_security.gleam", 99).
?DOC(false).
-spec http_authorization(binary(), binary()) -> binary().
http_authorization(Scheme_name, Prefix) ->
<<<<<<<<"transport.HttpAuthorization(scheme_name: \""/utf8,
Scheme_name/binary>>/binary,
"\", prefix: \""/utf8>>/binary,
Prefix/binary>>/binary,
"\")"/utf8>>.
-file("src/oaspec/internal/codegen/client_security.gleam", 107).
?DOC(false).
-spec field_label(binary()) -> binary().
field_label(Variant) ->
case Variant of
<<"ApiKeyHeader"/utf8>> ->
<<"header_name"/utf8>>;
<<"ApiKeyQuery"/utf8>> ->
<<"query_name"/utf8>>;
<<"ApiKeyCookie"/utf8>> ->
<<"cookie_name"/utf8>>;
_ ->
<<"name"/utf8>>
end.
-file("src/oaspec/internal/codegen/client_security.gleam", 87).
?DOC(false).
-spec api_key(binary(), binary(), binary()) -> binary().
api_key(Scheme_name, Variant, Name) ->
<<<<<<<<<<<<<<<<"transport."/utf8, Variant/binary>>/binary,
"(scheme_name: \""/utf8>>/binary,
Scheme_name/binary>>/binary,
"\", "/utf8>>/binary,
(field_label(Variant))/binary>>/binary,
": \""/utf8>>/binary,
Name/binary>>/binary,
"\")"/utf8>>.
-file("src/oaspec/internal/codegen/client_security.gleam", 57).
?DOC(false).
-spec render_requirement(
oaspec@internal@codegen@context:context(),
oaspec@openapi@spec:security_scheme_ref()
) -> {ok, binary()} | {error, nil}.
render_requirement(Ctx, Scheme_ref) ->
case erlang:element(5, oaspec@internal@codegen@context:spec(Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Scheme_ref)
) of
{ok, {value, Scheme}} ->
case Scheme of
{api_key_scheme, Header_name, scheme_in_header} ->
{ok,
api_key(
erlang:element(2, Scheme_ref),
<<"ApiKeyHeader"/utf8>>,
Header_name
)};
{api_key_scheme, Query_name, scheme_in_query} ->
{ok,
api_key(
erlang:element(2, Scheme_ref),
<<"ApiKeyQuery"/utf8>>,
Query_name
)};
{api_key_scheme, Cookie_name, scheme_in_cookie} ->
{ok,
api_key(
erlang:element(2, Scheme_ref),
<<"ApiKeyCookie"/utf8>>,
Cookie_name
)};
{http_scheme, Prefix, _} ->
{ok,
http_authorization(
erlang:element(2, Scheme_ref),
capitalize_first(Prefix)
)};
{o_auth2_scheme, _, _} ->
{ok,
http_authorization(
erlang:element(2, Scheme_ref),
<<"Bearer"/utf8>>
)};
{open_id_connect_scheme, _, _} ->
{ok,
http_authorization(
erlang:element(2, Scheme_ref),
<<"Bearer"/utf8>>
)};
_ ->
{error, nil}
end;
_ ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("src/oaspec/internal/codegen/client_security.gleam", 47).
?DOC(false).
-spec render_alternative(
oaspec@internal@codegen@context:context(),
oaspec@openapi@spec:security_requirement()
) -> binary().
render_alternative(Ctx, Alt) ->
Reqs = begin
_pipe = erlang:element(2, Alt),
gleam@list:filter_map(
_pipe,
fun(Scheme_ref) -> render_requirement(Ctx, Scheme_ref) end
)
end,
case Reqs of
[] ->
<<""/utf8>>;
_ ->
<<<<"transport.SecurityAlternative(["/utf8,
(gleam@string:join(Reqs, <<", "/utf8>>))/binary>>/binary,
"])"/utf8>>
end.
-file("src/oaspec/internal/codegen/client_security.gleam", 24).
?DOC(false).
-spec render_security_metadata(
oaspec@internal@codegen@context:context(),
list(oaspec@openapi@spec:security_requirement())
) -> binary().
render_security_metadata(Ctx, Alternatives) ->
case Alternatives of
[] ->
<<"[]"/utf8>>;
_ ->
Rendered = begin
_pipe = Alternatives,
gleam@list:filter_map(
_pipe,
fun(Alt) -> case render_alternative(Ctx, Alt) of
<<""/utf8>> ->
{error, nil};
S ->
{ok, S}
end end
)
end,
case Rendered of
[] ->
<<"[]"/utf8>>;
_ ->
<<<<"["/utf8,
(gleam@string:join(Rendered, <<", "/utf8>>))/binary>>/binary,
"]"/utf8>>
end
end.
-file("src/oaspec/internal/codegen/client_security.gleam", 118).
?DOC(false).
-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
).