Packages
oaspec
0.28.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@internal@codegen@client.erl
-module(oaspec@internal@codegen@client).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/internal/codegen/client.gleam").
-export([generate/1]).
-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.gleam", 410).
?DOC(false).
-spec any_operation_has_server(
list({binary(),
oaspec@internal@openapi@spec:operation(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@openapi@spec:http_method()}),
oaspec@internal@codegen@context:context()
) -> boolean().
any_operation_has_server(Operations, Ctx) ->
gleam@list:any(
Operations,
fun(Op) ->
{_, Operation, _, _} = Op,
case erlang:element(12, Operation) of
[] ->
case erlang:element(
6,
oaspec@internal@codegen@context:spec(Ctx)
) of
[] ->
false;
_ ->
true
end;
_ ->
true
end
end
).
-file("src/oaspec/internal/codegen/client.gleam", 427).
?DOC(false).
-spec any_operation_has_no_server(
list({binary(),
oaspec@internal@openapi@spec:operation(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@openapi@spec:http_method()}),
oaspec@internal@codegen@context:context()
) -> boolean().
any_operation_has_no_server(Operations, Ctx) ->
gleam@list:any(
Operations,
fun(Op) ->
{_, Operation, _, _} = Op,
case erlang:element(12, Operation) of
[] ->
gleam@list:is_empty(
erlang:element(
6,
oaspec@internal@codegen@context:spec(Ctx)
)
);
_ ->
false
end
end
).
-file("src/oaspec/internal/codegen/client.gleam", 441).
?DOC(false).
-spec substitute_server_variables(
binary(),
list({binary(), oaspec@internal@openapi@spec:server_variable()})
) -> binary().
substitute_server_variables(Url, Variables) ->
gleam@list:fold(
Variables,
Url,
fun(Acc, Entry) ->
{Name, Variable} = Entry,
gleam@string:replace(
Acc,
<<<<"{"/utf8, Name/binary>>/binary, "}"/utf8>>,
erlang:element(2, Variable)
)
end
).
-file("src/oaspec/internal/codegen/client.gleam", 452).
?DOC(false).
-spec generate_default_base_url(
gleam@string_tree:string_tree(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_default_base_url(Sb, Ctx) ->
case erlang:element(6, oaspec@internal@codegen@context:spec(Ctx)) of
[First_server | _] ->
Variables = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(4, First_server)
),
Resolved_url = substitute_server_variables(
erlang:element(2, First_server),
Variables
),
Defaults_doc = case Variables of
[] ->
<<""/utf8>>;
_ ->
<<"Defaults: "/utf8,
(gleam@string:join(
gleam@list:map(
Variables,
fun(Entry) ->
{Name, Variable} = Entry,
<<<<<<Name/binary, " = \""/utf8>>/binary,
(erlang:element(2, Variable))/binary>>/binary,
"\""/utf8>>
end
),
<<", "/utf8>>
))/binary>>
end,
Sb@1 = case Defaults_doc of
<<""/utf8>> ->
_pipe = Sb,
oaspec@internal@util@string_extra:doc_comment(
_pipe,
<<"Default base URL declared by the OpenAPI spec."/utf8>>
);
Doc ->
_pipe@1 = Sb,
_pipe@2 = oaspec@internal@util@string_extra:doc_comment(
_pipe@1,
<<"Default base URL declared by the OpenAPI spec."/utf8>>
),
oaspec@internal@util@string_extra:doc_comment(_pipe@2, Doc)
end,
_pipe@3 = Sb@1,
_pipe@4 = oaspec@internal@util@string_extra:line(
_pipe@3,
<<"pub fn default_base_url() -> String {"/utf8>>
),
_pipe@5 = oaspec@internal@util@string_extra:indent(
_pipe@4,
1,
<<<<"\""/utf8, Resolved_url/binary>>/binary, "\""/utf8>>
),
_pipe@6 = oaspec@internal@util@string_extra:line(
_pipe@5,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@6);
[] ->
Sb
end.
-file("src/oaspec/internal/codegen/client.gleam", 494).
?DOC(false).
-spec generate_text_body_helper(gleam@string_tree:string_tree()) -> gleam@string_tree:string_tree().
generate_text_body_helper(Sb) ->
_pipe = Sb,
_pipe@1 = oaspec@internal@util@string_extra:line(
_pipe,
<<"fn text_body(body: transport.Body) -> Result(String, ClientError) {"/utf8>>
),
_pipe@2 = oaspec@internal@util@string_extra:indent(
_pipe@1,
1,
<<"case body {"/utf8>>
),
_pipe@3 = oaspec@internal@util@string_extra:indent(
_pipe@2,
2,
<<"transport.TextBody(text) -> Ok(text)"/utf8>>
),
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
2,
<<"transport.BytesBody(_) -> Error(InvalidResponse(detail: \"expected text body, got bytes\"))"/utf8>>
),
_pipe@5 = oaspec@internal@util@string_extra:indent(
_pipe@4,
2,
<<"transport.EmptyBody -> Error(InvalidResponse(detail: \"expected text body, got empty body\"))"/utf8>>
),
_pipe@6 = oaspec@internal@util@string_extra:indent(_pipe@5, 1, <<"}"/utf8>>),
_pipe@7 = oaspec@internal@util@string_extra:line(_pipe@6, <<"}"/utf8>>),
oaspec@internal@util@string_extra:blank_line(_pipe@7).
-file("src/oaspec/internal/codegen/client.gleam", 515).
?DOC(false).
-spec generate_bytes_body_helper(gleam@string_tree:string_tree()) -> gleam@string_tree:string_tree().
generate_bytes_body_helper(Sb) ->
_pipe = Sb,
_pipe@1 = oaspec@internal@util@string_extra:line(
_pipe,
<<"fn bytes_body(body: transport.Body) -> Result(BitArray, ClientError) {"/utf8>>
),
_pipe@2 = oaspec@internal@util@string_extra:indent(
_pipe@1,
1,
<<"case body {"/utf8>>
),
_pipe@3 = oaspec@internal@util@string_extra:indent(
_pipe@2,
2,
<<"transport.BytesBody(bytes) -> Ok(bytes)"/utf8>>
),
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
2,
<<"transport.TextBody(_) -> Error(InvalidResponse(detail: \"expected binary body, got text\"))"/utf8>>
),
_pipe@5 = oaspec@internal@util@string_extra:indent(
_pipe@4,
2,
<<"transport.EmptyBody -> Error(InvalidResponse(detail: \"expected binary body, got empty body\"))"/utf8>>
),
_pipe@6 = oaspec@internal@util@string_extra:indent(_pipe@5, 1, <<"}"/utf8>>),
_pipe@7 = oaspec@internal@util@string_extra:line(_pipe@6, <<"}"/utf8>>),
oaspec@internal@util@string_extra:blank_line(_pipe@7).
-file("src/oaspec/internal/codegen/client.gleam", 535).
?DOC(false).
-spec emit_simple_query_param(
gleam@string_tree:string_tree(),
oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
emit_simple_query_param(Sb, P, Param_name, Ctx) ->
case erlang:element(5, P) of
true ->
To_str = oaspec@internal@codegen@client_request:param_to_string_expr(
P,
Param_name,
Ctx
),
_pipe = Sb,
oaspec@internal@util@string_extra:indent(
_pipe,
1,
<<<<<<<<"let query = [#(\""/utf8,
(erlang:element(2, P))/binary>>/binary,
"\", "/utf8>>/binary,
To_str/binary>>/binary,
"), ..query]"/utf8>>
);
false ->
To_str@1 = oaspec@internal@codegen@client_request:to_str_for_optional_value(
P,
Ctx
),
_pipe@1 = Sb,
_pipe@2 = oaspec@internal@util@string_extra:indent(
_pipe@1,
1,
<<<<"let query = case "/utf8, Param_name/binary>>/binary,
" {"/utf8>>
),
_pipe@3 = oaspec@internal@util@string_extra:indent(
_pipe@2,
2,
<<<<<<<<"Some(v) -> [#(\""/utf8, (erlang:element(2, P))/binary>>/binary,
"\", "/utf8>>/binary,
To_str@1/binary>>/binary,
"), ..query]"/utf8>>
),
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
2,
<<"None -> query"/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@4, 1, <<"}"/utf8>>)
end.
-file("src/oaspec/internal/codegen/client.gleam", 564).
?DOC(false).
-spec http_method_to_transport(oaspec@internal@openapi@spec:http_method()) -> binary().
http_method_to_transport(Method) ->
case Method of
get ->
<<"transport.Get"/utf8>>;
post ->
<<"transport.Post"/utf8>>;
put ->
<<"transport.Put"/utf8>>;
delete ->
<<"transport.Delete"/utf8>>;
patch ->
<<"transport.Patch"/utf8>>;
head ->
<<"transport.Head"/utf8>>;
options ->
<<"transport.Options"/utf8>>;
trace ->
<<"transport.Trace"/utf8>>
end.
-file("src/oaspec/internal/codegen/client.gleam", 852).
?DOC(false).
-spec build_call_args(
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
oaspec@internal@openapi@spec:operation(oaspec@internal@openapi@spec:resolved()),
oaspec@internal@codegen@context:context()
) -> binary().
build_call_args(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation,
_
) ->
All_params = begin
_pipe = lists:append(Path_params, Query_params),
_pipe@1 = lists:append(_pipe, Header_params),
lists:append(_pipe@1, Cookie_params)
end,
Field_names = oaspec@internal@codegen@client_request:build_param_field_names(
Operation
),
Names = gleam@list:map(
All_params,
fun(P) ->
Field = oaspec@internal@codegen@client_request:field_name_for(
Field_names,
P
),
<<<<Field/binary, ": "/utf8>>/binary, Field/binary>>
end
),
Body_args = case erlang:element(7, Operation) of
{some, {value, Rb}} ->
Content_entries = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(3, Rb)
),
case Content_entries of
[_, _ | _] ->
[<<"content_type: content_type"/utf8>>,
<<"body: body"/utf8>>];
_ ->
[<<"body: body"/utf8>>]
end;
_ ->
[]
end,
All = lists:append(Names, Body_args),
case All of
[] ->
<<"()"/utf8>>;
_ ->
<<<<"("/utf8, (gleam@string:join(All, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>
end.
-file("src/oaspec/internal/codegen/client.gleam", 894).
?DOC(false).
-spec rebind_request_fields(binary()) -> binary().
rebind_request_fields(Extra) ->
gleam@string:replace(Extra, <<"req."/utf8>>, <<"request."/utf8>>).
-file("src/oaspec/internal/codegen/client.gleam", 900).
?DOC(false).
-spec trim_leading_comma(binary()) -> binary().
trim_leading_comma(S) ->
gleam@bool:guard(
not gleam_stdlib:string_starts_with(S, <<", "/utf8>>),
S,
fun() -> gleam@string:drop_start(S, 2) end
).
-file("src/oaspec/internal/codegen/client.gleam", 1279).
?DOC(false).
-spec generate_multipart_body_emission(
gleam@string_tree:string_tree(),
oaspec@internal@openapi@spec:request_body(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_multipart_body_emission(Sb, Rb, Op_id, Ctx) ->
Sb@1 = oaspec@internal@codegen@client_request:generate_multipart_body(
Sb,
Rb,
Op_id,
Ctx
),
_pipe = Sb@1,
oaspec@internal@util@string_extra:indent(
_pipe,
1,
<<"let body = transport.TextBody(body_str)"/utf8>>
).
-file("src/oaspec/internal/codegen/client.gleam", 1293).
?DOC(false).
-spec generate_form_urlencoded_body_emission(
gleam@string_tree:string_tree(),
oaspec@internal@openapi@spec:request_body(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_form_urlencoded_body_emission(Sb, Rb, Op_id, Ctx) ->
Sb@1 = oaspec@internal@codegen@client_request:generate_form_urlencoded_body(
Sb,
Rb,
Op_id,
Ctx
),
_pipe = Sb@1,
oaspec@internal@util@string_extra:indent(
_pipe,
1,
<<"let body = transport.TextBody(body_str)"/utf8>>
).
-file("src/oaspec/internal/codegen/client.gleam", 1219).
?DOC(false).
-spec generate_body_emission(
gleam@string_tree:string_tree(),
oaspec@internal@openapi@spec:request_body(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_body_emission(Sb, Rb, Op_id, Ctx) ->
Content_entries = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(3, Rb)
),
case Content_entries of
[_, _ | _] ->
case erlang:element(4, Rb) of
true ->
_pipe = Sb,
oaspec@internal@util@string_extra:indent(
_pipe,
1,
<<"let body = transport.TextBody(body)"/utf8>>
);
false ->
_pipe@1 = Sb,
_pipe@2 = oaspec@internal@util@string_extra:indent(
_pipe@1,
1,
<<"let body = case body {"/utf8>>
),
_pipe@3 = oaspec@internal@util@string_extra:indent(
_pipe@2,
2,
<<"Some(b) -> transport.TextBody(b)"/utf8>>
),
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
2,
<<"None -> transport.EmptyBody"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@4,
1,
<<"}"/utf8>>
)
end;
[{Content_type_key, _}] ->
case Content_type_key of
<<"application/octet-stream"/utf8>> ->
case erlang:element(4, Rb) of
true ->
_pipe@5 = Sb,
oaspec@internal@util@string_extra:indent(
_pipe@5,
1,
<<"let body = transport.BytesBody(body)"/utf8>>
);
false ->
_pipe@6 = Sb,
_pipe@7 = oaspec@internal@util@string_extra:indent(
_pipe@6,
1,
<<"let body = case body {"/utf8>>
),
_pipe@8 = oaspec@internal@util@string_extra:indent(
_pipe@7,
2,
<<"Some(b) -> transport.BytesBody(b)"/utf8>>
),
_pipe@9 = oaspec@internal@util@string_extra:indent(
_pipe@8,
2,
<<"None -> transport.EmptyBody"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@9,
1,
<<"}"/utf8>>
)
end;
<<"multipart/form-data"/utf8>> ->
generate_multipart_body_emission(Sb, Rb, Op_id, Ctx);
<<"application/x-www-form-urlencoded"/utf8>> ->
generate_form_urlencoded_body_emission(Sb, Rb, Op_id, Ctx);
_ ->
Encode_expr = oaspec@internal@codegen@client_request:get_body_encode_expr(
Rb,
Op_id,
Ctx
),
case erlang:element(4, Rb) of
true ->
_pipe@10 = Sb,
oaspec@internal@util@string_extra:indent(
_pipe@10,
1,
<<<<"let body = transport.TextBody("/utf8,
Encode_expr/binary>>/binary,
")"/utf8>>
);
false ->
_pipe@11 = Sb,
_pipe@12 = oaspec@internal@util@string_extra:indent(
_pipe@11,
1,
<<"let body = case body {"/utf8>>
),
_pipe@13 = oaspec@internal@util@string_extra:indent(
_pipe@12,
2,
<<<<"Some(body) -> transport.TextBody("/utf8,
Encode_expr/binary>>/binary,
")"/utf8>>
),
_pipe@14 = oaspec@internal@util@string_extra:indent(
_pipe@13,
2,
<<"None -> transport.EmptyBody"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@14,
1,
<<"}"/utf8>>
)
end
end;
[] ->
_pipe@15 = Sb,
oaspec@internal@util@string_extra:indent(
_pipe@15,
1,
<<"let body = transport.EmptyBody"/utf8>>
)
end.
-file("src/oaspec/internal/codegen/client.gleam", 908).
?DOC(false).
-spec generate_build_body(
gleam@string_tree:string_tree(),
binary(),
oaspec@internal@openapi@spec:operation(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@openapi@spec:http_method(),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
list(oaspec@internal@openapi@spec:parameter(oaspec@internal@openapi@spec:resolved())),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_build_body(
Sb,
Op_id,
Operation,
Path,
Method,
Path_params,
Query_params,
Header_params,
Cookie_params,
Ctx
) ->
Field_names = oaspec@internal@codegen@client_request:build_param_field_names(
Operation
),
Client_guard_schema = case {oaspec@config:validate(
oaspec@internal@codegen@context:config(Ctx)
),
erlang:element(7, Operation)} of
{true, {some, {value, Rb}}} ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[{_, Mt}] ->
case erlang:element(2, Mt) of
{some, {reference, _, Name}} ->
case oaspec@internal@codegen@guards:schema_has_validator(
Name,
Ctx
) of
true ->
{some, {Name, erlang:element(4, Rb)}};
false ->
none
end;
_ ->
none
end;
_ ->
none
end;
{_, _} ->
none
end,
Sb@1 = case Client_guard_schema of
{some, {Name@1, true}} ->
Validate_fn = <<<<"guards.validate_"/utf8,
(oaspec@internal@util@naming:to_snake_case(Name@1))/binary>>/binary,
"(body)"/utf8>>,
_pipe = Sb,
_pipe@1 = oaspec@internal@util@string_extra:indent(
_pipe,
1,
<<<<"case "/utf8, Validate_fn/binary>>/binary, " {"/utf8>>
),
_pipe@2 = oaspec@internal@util@string_extra:indent(
_pipe@1,
2,
<<"Error(errors) -> Error(ValidationError(errors:))"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@2,
2,
<<"Ok(_) -> {"/utf8>>
);
{some, {Name@2, false}} ->
Validate_fn@1 = <<"guards.validate_"/utf8,
(oaspec@internal@util@naming:to_snake_case(Name@2))/binary>>,
_pipe@3 = Sb,
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
1,
<<"let validation_errors = case body {"/utf8>>
),
_pipe@5 = oaspec@internal@util@string_extra:indent(
_pipe@4,
2,
<<<<"Some(b) -> case "/utf8, Validate_fn@1/binary>>/binary,
"(b) {"/utf8>>
),
_pipe@6 = oaspec@internal@util@string_extra:indent(
_pipe@5,
3,
<<"Error(errors) -> errors"/utf8>>
),
_pipe@7 = oaspec@internal@util@string_extra:indent(
_pipe@6,
3,
<<"Ok(_) -> []"/utf8>>
),
_pipe@8 = oaspec@internal@util@string_extra:indent(
_pipe@7,
2,
<<"}"/utf8>>
),
_pipe@9 = oaspec@internal@util@string_extra:indent(
_pipe@8,
2,
<<"None -> []"/utf8>>
),
_pipe@10 = oaspec@internal@util@string_extra:indent(
_pipe@9,
1,
<<"}"/utf8>>
),
_pipe@11 = oaspec@internal@util@string_extra:indent(
_pipe@10,
1,
<<"case validation_errors {"/utf8>>
),
_pipe@12 = oaspec@internal@util@string_extra:indent(
_pipe@11,
2,
<<"[_, ..] -> Error(ValidationError(errors: validation_errors))"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@12,
2,
<<"[] -> {"/utf8>>
);
none ->
Sb
end,
Effective_server_url = case erlang:element(12, Operation) of
[First_server | _] ->
Variables = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(4, First_server)
),
Resolved = substitute_server_variables(
erlang:element(2, First_server),
Variables
),
{some, Resolved};
[] ->
case erlang:element(6, oaspec@internal@codegen@context:spec(Ctx)) of
[_ | _] ->
{some, <<"default_base_url()"/utf8>>};
[] ->
none
end
end,
Sb@2 = begin
_pipe@13 = Sb@1,
oaspec@internal@util@string_extra:indent(
_pipe@13,
1,
<<<<"let path = \""/utf8, Path/binary>>/binary, "\""/utf8>>
)
end,
Sb@4 = gleam@list:fold(
Path_params,
Sb@2,
fun(Sb@3, P) ->
Param_name = oaspec@internal@codegen@client_request:field_name_for(
Field_names,
P
),
To_string_expr = oaspec@internal@codegen@client_request:param_to_string_expr(
P,
Param_name,
Ctx
),
_pipe@14 = Sb@3,
oaspec@internal@util@string_extra:indent(
_pipe@14,
1,
<<<<<<<<"let path = string.replace(path, \"{"/utf8,
(erlang:element(2, P))/binary>>/binary,
"}\", uri.percent_encode("/utf8>>/binary,
To_string_expr/binary>>/binary,
"))"/utf8>>
)
end
),
Sb@8 = case gleam@list:is_empty(Query_params) of
true ->
_pipe@15 = Sb@4,
oaspec@internal@util@string_extra:indent(
_pipe@15,
1,
<<"let query = []"/utf8>>
);
false ->
Sb@5 = begin
_pipe@16 = Sb@4,
oaspec@internal@util@string_extra:indent(
_pipe@16,
1,
<<"let query = []"/utf8>>
)
end,
Sb@7 = gleam@list:fold(
Query_params,
Sb@5,
fun(Sb@6, P@1) ->
Param_name@1 = oaspec@internal@codegen@client_request:field_name_for(
Field_names,
P@1
),
case oaspec@internal@codegen@client_request:is_exploded_array_param(
P@1,
Ctx
) of
true ->
oaspec@internal@codegen@client_request:generate_exploded_array_query_param(
Sb@6,
P@1,
Param_name@1,
Ctx
);
false ->
case oaspec@internal@codegen@client_request:is_delimited_array_param(
P@1,
Ctx
) of
{some, Joiner} ->
oaspec@internal@codegen@client_request:generate_delimited_array_query_param(
Sb@6,
P@1,
Param_name@1,
Joiner,
Ctx
);
none ->
emit_simple_query_param(
Sb@6,
P@1,
Param_name@1,
Ctx
)
end
end
end
),
_pipe@17 = Sb@7,
oaspec@internal@util@string_extra:indent(
_pipe@17,
1,
<<"let query = list.reverse(query)"/utf8>>
)
end,
Sb@9 = begin
_pipe@18 = Sb@8,
oaspec@internal@util@string_extra:indent(
_pipe@18,
1,
<<"let headers = []"/utf8>>
)
end,
Sb@11 = gleam@list:fold(
Header_params,
Sb@9,
fun(Sb@10, P@2) ->
Param_name@2 = oaspec@internal@codegen@client_request:field_name_for(
Field_names,
P@2
),
Header_name = string:lowercase(erlang:element(2, P@2)),
case erlang:element(5, P@2) of
true ->
To_str = oaspec@internal@codegen@client_request:param_to_string_expr(
P@2,
Param_name@2,
Ctx
),
_pipe@19 = Sb@10,
oaspec@internal@util@string_extra:indent(
_pipe@19,
1,
<<<<<<<<"let headers = [#(\""/utf8, Header_name/binary>>/binary,
"\", "/utf8>>/binary,
To_str/binary>>/binary,
"), ..headers]"/utf8>>
);
false ->
To_str@1 = oaspec@internal@codegen@client_request:to_str_for_optional_value(
P@2,
Ctx
),
_pipe@20 = Sb@10,
_pipe@21 = oaspec@internal@util@string_extra:indent(
_pipe@20,
1,
<<<<"let headers = case "/utf8, Param_name@2/binary>>/binary,
" {"/utf8>>
),
_pipe@22 = oaspec@internal@util@string_extra:indent(
_pipe@21,
2,
<<<<<<<<"Some(v) -> [#(\""/utf8, Header_name/binary>>/binary,
"\", "/utf8>>/binary,
To_str@1/binary>>/binary,
"), ..headers]"/utf8>>
),
_pipe@23 = oaspec@internal@util@string_extra:indent(
_pipe@22,
2,
<<"None -> headers"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@23,
1,
<<"}"/utf8>>
)
end
end
),
Sb@15 = case gleam@list:is_empty(Cookie_params) of
true ->
Sb@11;
false ->
Sb@12 = begin
_pipe@24 = Sb@11,
oaspec@internal@util@string_extra:indent(
_pipe@24,
1,
<<"let cookie_parts = []"/utf8>>
)
end,
Sb@14 = gleam@list:fold(
Cookie_params,
Sb@12,
fun(Sb@13, P@3) ->
Param_name@3 = oaspec@internal@codegen@client_request:field_name_for(
Field_names,
P@3
),
case erlang:element(5, P@3) of
true ->
To_str@2 = oaspec@internal@codegen@client_request:param_to_string_expr(
P@3,
Param_name@3,
Ctx
),
_pipe@25 = Sb@13,
oaspec@internal@util@string_extra:indent(
_pipe@25,
1,
<<<<<<<<"let cookie_parts = [\""/utf8,
(erlang:element(2, P@3))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
To_str@2/binary>>/binary,
"), ..cookie_parts]"/utf8>>
);
false ->
To_str@3 = oaspec@internal@codegen@client_request:to_str_for_optional_value(
P@3,
Ctx
),
_pipe@26 = Sb@13,
_pipe@27 = oaspec@internal@util@string_extra:indent(
_pipe@26,
1,
<<<<"let cookie_parts = case "/utf8,
Param_name@3/binary>>/binary,
" {"/utf8>>
),
_pipe@28 = oaspec@internal@util@string_extra:indent(
_pipe@27,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(2, P@3))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
To_str@3/binary>>/binary,
"), ..cookie_parts]"/utf8>>
),
_pipe@29 = oaspec@internal@util@string_extra:indent(
_pipe@28,
2,
<<"None -> cookie_parts"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@29,
1,
<<"}"/utf8>>
)
end
end
),
_pipe@30 = Sb@14,
_pipe@31 = oaspec@internal@util@string_extra:indent(
_pipe@30,
1,
<<"let headers = case cookie_parts {"/utf8>>
),
_pipe@32 = oaspec@internal@util@string_extra:indent(
_pipe@31,
2,
<<"[] -> headers"/utf8>>
),
_pipe@33 = oaspec@internal@util@string_extra:indent(
_pipe@32,
2,
<<"_ -> [#(\"cookie\", string.join(cookie_parts, \"; \")), ..headers]"/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@33, 1, <<"}"/utf8>>)
end,
Needs_reverse = not gleam@list:is_empty(Header_params) orelse not gleam@list:is_empty(
Cookie_params
),
Sb@16 = case Needs_reverse of
true ->
_pipe@34 = Sb@15,
oaspec@internal@util@string_extra:indent(
_pipe@34,
1,
<<"let headers = list.reverse(headers)"/utf8>>
);
false ->
Sb@15
end,
Sb@17 = case erlang:element(7, Operation) of
{some, {value, Rb@1}} ->
generate_body_emission(Sb@16, Rb@1, Op_id, Ctx);
_ ->
_pipe@35 = Sb@16,
oaspec@internal@util@string_extra:indent(
_pipe@35,
1,
<<"let body = transport.EmptyBody"/utf8>>
)
end,
Sb@18 = case erlang:element(7, Operation) of
{some, {value, Rb@2}} ->
Content_entries@1 = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(3, Rb@2)
),
Ct_expr = case Content_entries@1 of
[_, _ | _] ->
<<"content_type"/utf8>>;
[{<<"multipart/form-data"/utf8>>, _}] ->
<<"body_content_type"/utf8>>;
[{<<"application/x-www-form-urlencoded"/utf8>>, _}] ->
<<"body_content_type"/utf8>>;
[{Static_ct, _}] ->
<<<<"\""/utf8, Static_ct/binary>>/binary, "\""/utf8>>;
[] ->
<<""/utf8>>
end,
case Ct_expr of
<<""/utf8>> ->
Sb@17;
Ct ->
Header_line = <<<<"[#(\"content-type\", "/utf8, Ct/binary>>/binary,
"), ..headers]"/utf8>>,
case erlang:element(4, Rb@2) of
true ->
_pipe@36 = Sb@17,
oaspec@internal@util@string_extra:indent(
_pipe@36,
1,
<<"let headers = "/utf8, Header_line/binary>>
);
false ->
_pipe@37 = Sb@17,
_pipe@38 = oaspec@internal@util@string_extra:indent(
_pipe@37,
1,
<<"let headers = case body {"/utf8>>
),
_pipe@39 = oaspec@internal@util@string_extra:indent(
_pipe@38,
2,
<<"transport.EmptyBody -> headers"/utf8>>
),
_pipe@40 = oaspec@internal@util@string_extra:indent(
_pipe@39,
2,
<<"_ -> "/utf8, Header_line/binary>>
),
oaspec@internal@util@string_extra:indent(
_pipe@40,
1,
<<"}"/utf8>>
)
end
end;
_ ->
Sb@17
end,
Base_url_expr = case Effective_server_url of
{some, <<"default_base_url()"/utf8>>} ->
<<"Some(default_base_url())"/utf8>>;
{some, Url} ->
<<<<"Some(\""/utf8, Url/binary>>/binary, "\")"/utf8>>;
none ->
<<"None"/utf8>>
end,
Effective_security = case erlang:element(10, Operation) of
{some, Sec} ->
Sec;
none ->
erlang:element(7, oaspec@internal@codegen@context:spec(Ctx))
end,
Security_literal = oaspec@internal@codegen@client_security:render_security_metadata(
Ctx,
Effective_security
),
Http_method = http_method_to_transport(Method),
Sb@19 = begin
_pipe@41 = Sb@18,
_pipe@42 = oaspec@internal@util@string_extra:indent(
_pipe@41,
1,
<<"Ok(transport.Request("/utf8>>
),
_pipe@43 = oaspec@internal@util@string_extra:indent(
_pipe@42,
2,
<<<<"method: "/utf8, Http_method/binary>>/binary, ","/utf8>>
),
_pipe@44 = oaspec@internal@util@string_extra:indent(
_pipe@43,
2,
<<<<"base_url: "/utf8, Base_url_expr/binary>>/binary, ","/utf8>>
),
_pipe@45 = oaspec@internal@util@string_extra:indent(
_pipe@44,
2,
<<"path: path,"/utf8>>
),
_pipe@46 = oaspec@internal@util@string_extra:indent(
_pipe@45,
2,
<<"query: query,"/utf8>>
),
_pipe@47 = oaspec@internal@util@string_extra:indent(
_pipe@46,
2,
<<"headers: headers,"/utf8>>
),
_pipe@48 = oaspec@internal@util@string_extra:indent(
_pipe@47,
2,
<<"body: body,"/utf8>>
),
_pipe@49 = oaspec@internal@util@string_extra:indent(
_pipe@48,
2,
<<<<"security: "/utf8, Security_literal/binary>>/binary, ","/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@49, 1, <<"))"/utf8>>)
end,
case Client_guard_schema of
{some, _} ->
_pipe@50 = Sb@19,
_pipe@51 = oaspec@internal@util@string_extra:indent(
_pipe@50,
1,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@51, 1, <<"}"/utf8>>);
none ->
Sb@19
end.
-file("src/oaspec/internal/codegen/client.gleam", 579).
?DOC(false).
-spec generate_client_function(
gleam@string_tree:string_tree(),
binary(),
oaspec@internal@openapi@spec:operation(oaspec@internal@openapi@spec:resolved()),
binary(),
oaspec@internal@openapi@spec:http_method(),
oaspec@internal@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_client_function(Sb, Op_id, Operation, Path, Method, Ctx) ->
Fn_name = oaspec@internal@util@naming:operation_to_function_name(Op_id),
Build_fn = <<<<"build_"/utf8, Fn_name/binary>>/binary, "_request"/utf8>>,
Decode_fn = <<<<"decode_"/utf8, Fn_name/binary>>/binary, "_response"/utf8>>,
Response_type = <<(oaspec@internal@util@naming:schema_to_type_name(Op_id))/binary,
"Response"/utf8>>,
Unwrapped_params = gleam@list:filter_map(
erlang:element(6, Operation),
fun(Ref_p) -> case Ref_p of
{value, P} ->
{ok, P};
_ ->
{error, nil}
end end
),
Path_params = gleam@list:filter(
Unwrapped_params,
fun(P@1) -> erlang:element(3, P@1) =:= in_path end
),
Query_params = gleam@list:filter(
Unwrapped_params,
fun(P@2) -> erlang:element(3, P@2) =:= in_query end
),
Header_params = gleam@list:filter(
Unwrapped_params,
fun(P@3) -> erlang:element(3, P@3) =:= in_header end
),
Cookie_params = gleam@list:filter(
Unwrapped_params,
fun(P@4) -> erlang:element(3, P@4) =:= in_cookie end
),
Params_signature = oaspec@internal@codegen@client_request:build_param_list(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation,
Op_id,
Ctx
),
Call_args = build_call_args(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation,
Ctx
),
Sb@1 = case erlang:element(3, Operation) of
{some, Summary} ->
_pipe = Sb,
oaspec@internal@util@string_extra:doc_comment(_pipe, Summary);
_ ->
Sb
end,
Sb@2 = case erlang:element(4, Operation) of
{some, Desc} ->
_pipe@1 = Sb@1,
oaspec@internal@util@string_extra:doc_comment(_pipe@1, Desc);
_ ->
Sb@1
end,
Sb@3 = case erlang:element(7, Operation) of
{some, {value, Rb}} ->
Content_entries = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(3, Rb)
),
case Content_entries of
[_, _ | _] ->
Ct_names = begin
_pipe@2 = gleam@list:map(
Content_entries,
fun(E) -> erlang:element(1, E) end
),
gleam@string:join(_pipe@2, <<", "/utf8>>)
end,
_pipe@3 = Sb@2,
oaspec@internal@util@string_extra:doc_comment(
_pipe@3,
<<"Supported content types: "/utf8, Ct_names/binary>>
);
_ ->
Sb@2
end;
_ ->
Sb@2
end,
Sb@4 = begin
_pipe@4 = Sb@3,
oaspec@internal@util@string_extra:line(
_pipe@4,
<<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"(send send: transport.Send"/utf8>>/binary,
Params_signature/binary>>/binary,
") -> Result(response_types."/utf8>>/binary,
Response_type/binary>>/binary,
", ClientError) {"/utf8>>
)
end,
Sb@5 = begin
_pipe@5 = Sb@4,
_pipe@6 = oaspec@internal@util@string_extra:indent(
_pipe@5,
1,
<<<<<<"use req <- result.try("/utf8, Build_fn/binary>>/binary,
Call_args/binary>>/binary,
")"/utf8>>
),
_pipe@7 = oaspec@internal@util@string_extra:indent(
_pipe@6,
1,
<<"use resp <- result.try("/utf8>>
),
_pipe@8 = oaspec@internal@util@string_extra:indent(
_pipe@7,
2,
<<"send(req)"/utf8>>
),
_pipe@9 = oaspec@internal@util@string_extra:indent(
_pipe@8,
2,
<<"|> result.map_error(TransportError),"/utf8>>
),
_pipe@10 = oaspec@internal@util@string_extra:indent(
_pipe@9,
1,
<<")"/utf8>>
),
_pipe@11 = oaspec@internal@util@string_extra:indent(
_pipe@10,
1,
<<Decode_fn/binary, "(resp)"/utf8>>
),
_pipe@12 = oaspec@internal@util@string_extra:line(
_pipe@11,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@12)
end,
Sb@6 = begin
_pipe@13 = Sb@5,
_pipe@14 = oaspec@internal@util@string_extra:doc_comment(
_pipe@13,
<<<<"Build the transport request for "/utf8, Fn_name/binary>>/binary,
" without sending it. Useful for testing and for adding custom transport middleware."/utf8>>
),
oaspec@internal@util@string_extra:line(
_pipe@14,
<<<<<<<<"pub fn "/utf8, Build_fn/binary>>/binary, "("/utf8>>/binary,
(trim_leading_comma(Params_signature))/binary>>/binary,
") -> Result(transport.Request, ClientError) {"/utf8>>
)
end,
Sb@7 = generate_build_body(
Sb@6,
Op_id,
Operation,
Path,
Method,
Path_params,
Query_params,
Header_params,
Cookie_params,
Ctx
),
Sb@8 = begin
_pipe@15 = Sb@7,
_pipe@16 = oaspec@internal@util@string_extra:line(
_pipe@15,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@16)
end,
Sb@9 = begin
_pipe@17 = Sb@8,
_pipe@18 = oaspec@internal@util@string_extra:doc_comment(
_pipe@17,
<<<<"Decode a transport response into the typed response variant for "/utf8,
Fn_name/binary>>/binary,
"."/utf8>>
),
_pipe@19 = oaspec@internal@util@string_extra:line(
_pipe@18,
<<<<<<<<"pub fn "/utf8, Decode_fn/binary>>/binary,
"(resp: transport.Response) -> Result(response_types."/utf8>>/binary,
Response_type/binary>>/binary,
", ClientError) {"/utf8>>
),
oaspec@internal@util@string_extra:indent(
_pipe@19,
1,
<<"case resp.status {"/utf8>>
)
end,
Responses = oaspec@internal@util@http:sort_response_entries(
maps:to_list(erlang:element(8, Operation))
),
Sb@11 = gleam@list:fold(
Responses,
Sb@9,
fun(Sb@10, Entry) ->
{Status_code, Ref_or} = Entry,
case Ref_or of
{value, Response} ->
Variant_name = <<<<<<"response_types."/utf8,
(oaspec@internal@util@naming:schema_to_type_name(
Op_id
))/binary>>/binary,
"Response"/utf8>>/binary,
(oaspec@internal@util@http:status_code_suffix(
Status_code
))/binary>>,
Content_entries@1 = oaspec@internal@codegen@ir_build:sorted_entries(
erlang:element(3, Response)
),
case Content_entries@1 of
[] ->
_pipe@20 = Sb@10,
oaspec@internal@util@string_extra:indent(
_pipe@20,
2,
<<<<<<(oaspec@internal@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
);
[{Single_ct, Single_mt}] ->
oaspec@internal@codegen@client_response:generate_single_content_response(
Sb@10,
Status_code,
Variant_name,
Single_ct,
Single_mt,
Op_id,
Ctx
);
Multiple ->
oaspec@internal@codegen@client_response:generate_multi_content_response(
Sb@10,
Status_code,
Variant_name,
Multiple,
Op_id,
Ctx
)
end;
_ ->
Sb@10
end
end
),
Has_default = gleam@list:any(
Responses,
fun(Entry@1) ->
{Code, _} = Entry@1,
Code =:= default_status
end
),
Sb@12 = case Has_default of
true ->
Sb@11;
false ->
_pipe@21 = Sb@11,
_pipe@22 = oaspec@internal@util@string_extra:indent(
_pipe@21,
2,
<<"_ ->"/utf8>>
),
_pipe@23 = oaspec@internal@util@string_extra:indent(
_pipe@22,
3,
<<"Error(UnexpectedStatus("/utf8>>
),
_pipe@24 = oaspec@internal@util@string_extra:indent(
_pipe@23,
4,
<<"status: resp.status,"/utf8>>
),
_pipe@25 = oaspec@internal@util@string_extra:indent(
_pipe@24,
4,
<<"headers: resp.headers,"/utf8>>
),
_pipe@26 = oaspec@internal@util@string_extra:indent(
_pipe@25,
4,
<<"body: resp.body,"/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@26, 3, <<"))"/utf8>>)
end,
Sb@13 = begin
_pipe@27 = Sb@12,
_pipe@28 = oaspec@internal@util@string_extra:indent(
_pipe@27,
1,
<<"}"/utf8>>
),
_pipe@29 = oaspec@internal@util@string_extra:line(
_pipe@28,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@29)
end,
case oaspec@internal@codegen@client_request:build_request_object_call_args(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation
) of
{some, Call_args2} ->
Request_type = <<(oaspec@internal@util@naming:schema_to_type_name(
Op_id
))/binary,
"Request"/utf8>>,
Extra = case Call_args2 of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<", "/utf8, Call_args2/binary>>
end,
_pipe@30 = Sb@13,
_pipe@31 = oaspec@internal@util@string_extra:doc_comment(
_pipe@30,
<<<<"Request-object wrapper. Delegates to "/utf8,
Fn_name/binary>>/binary,
" with fields unpacked from the request record."/utf8>>
),
_pipe@32 = oaspec@internal@util@string_extra:line(
_pipe@31,
<<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"_with_request(send send: transport.Send, request request: request_types."/utf8>>/binary,
Request_type/binary>>/binary,
") -> Result(response_types."/utf8>>/binary,
Response_type/binary>>/binary,
", ClientError) {"/utf8>>
),
_pipe@33 = oaspec@internal@util@string_extra:indent(
_pipe@32,
1,
<<<<<<Fn_name/binary, "(send"/utf8>>/binary,
(rebind_request_fields(Extra))/binary>>/binary,
")"/utf8>>
),
_pipe@34 = oaspec@internal@util@string_extra:line(
_pipe@33,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@34);
none ->
Sb@13
end.
-file("src/oaspec/internal/codegen/client.gleam", 40).
?DOC(false).
-spec generate_client(oaspec@internal@codegen@context:context()) -> binary().
generate_client(Ctx) ->
Operations = oaspec@internal@openapi@operations:collect_operations(Ctx),
All_params = gleam@list:flat_map(
Operations,
fun(Op) ->
{_, Operation, _, _} = Op,
gleam@list:filter_map(
erlang:element(6, Operation),
fun(Ref_p) -> case Ref_p of
{value, P} ->
{ok, P};
_ ->
{error, nil}
end end
)
end
),
Needs_bool = gleam@list:any(
All_params,
fun(P@1) -> case erlang:element(6, P@1) of
{parameter_schema, {inline, {boolean_schema, _}}} ->
true;
_ ->
false
end end
),
Needs_float = gleam@list:any(
All_params,
fun(P@2) -> case erlang:element(6, P@2) of
{parameter_schema,
{inline, {number_schema, _, _, _, _, _, _, _}}} ->
true;
_ ->
false
end end
),
Has_multi_content_response = gleam@list:any(
Operations,
fun(Op@1) ->
{_, Operation@1, _, _} = Op@1,
gleam@list:any(
maps:to_list(erlang:element(8, Operation@1)),
fun(Entry) ->
{_, Ref_or} = Entry,
case Ref_or of
{value, Response} ->
erlang:length(
maps:to_list(erlang:element(3, Response))
)
> 1;
_ ->
false
end
end
)
end
),
Has_form_urlencoded = gleam@list:any(
Operations,
fun(Op@2) ->
{_, Operation@2, _, _} = Op@2,
case erlang:element(7, Operation@2) of
{some, {value, Rb}} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb)),
fun(Ce) ->
{Key, _} = Ce,
Key =:= <<"application/x-www-form-urlencoded"/utf8>>
end
);
_ ->
false
end
end
),
Has_multipart = gleam@list:any(
Operations,
fun(Op@3) ->
{_, Operation@3, _, _} = Op@3,
case erlang:element(7, Operation@3) of
{some, {value, Rb@1}} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb@1)),
fun(Ce@1) ->
{Key@1, _} = Ce@1,
Key@1 =:= <<"multipart/form-data"/utf8>>
end
);
_ ->
false
end
end
),
Needs_list = ((((Has_form_urlencoded orelse Has_multi_content_response)
orelse gleam@list:any(
All_params,
fun(P@3) -> erlang:element(3, P@3) =:= in_query end
))
orelse gleam@list:any(
All_params,
fun(P@4) -> erlang:element(3, P@4) =:= in_cookie end
))
orelse gleam@list:any(
All_params,
fun(P@5) -> erlang:element(3, P@5) =:= in_header end
))
orelse gleam@list:any(All_params, fun(P@6) -> case erlang:element(6, P@6) of
{parameter_schema, {inline, {array_schema, _, _, _, _, _}}} ->
true;
{parameter_schema, {reference, _, _} = Sr} ->
case oaspec@internal@openapi@resolver:resolve_schema_ref(
Sr,
oaspec@internal@codegen@context:spec(Ctx)
) of
{ok, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end end),
Needs_dyn_decode = gleam@list:any(
Operations,
fun(Op@4) ->
{_, Operation@4, _, _} = Op@4,
gleam@list:any(
maps:to_list(erlang:element(8, Operation@4)),
fun(Entry@1) ->
{_, Ref_or@1} = Entry@1,
case Ref_or@1 of
{value, Response@1} ->
gleam@list:any(
maps:to_list(erlang:element(3, Response@1)),
fun(Ce@2) ->
{Media_type_name, Mt} = Ce@2,
case Media_type_name of
<<"text/plain"/utf8>> ->
false;
_ ->
case erlang:element(2, Mt) of
{some,
{inline,
{array_schema,
_,
{inline, _},
_,
_,
_}}} ->
true;
{some,
{inline,
{string_schema,
_,
_,
_,
_,
_,
_}}} ->
true;
{some,
{inline,
{integer_schema,
_,
_,
_,
_,
_,
_,
_}}} ->
true;
{some,
{inline,
{number_schema,
_,
_,
_,
_,
_,
_,
_}}} ->
true;
{some,
{inline,
{boolean_schema, _}}} ->
true;
_ ->
false
end
end
end
);
_ ->
false
end
end
)
end
),
Needs_json = Needs_dyn_decode orelse gleam@list:any(
Operations,
fun(Op@5) ->
{_, Operation@5, _, _} = Op@5,
case erlang:element(7, Operation@5) of
{some, {value, Rb@2}} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb@2)),
fun(Ce@3) ->
{_, Mt@1} = Ce@3,
case erlang:element(2, Mt@1) of
{some,
{inline, {string_schema, _, _, _, _, _, _}}} ->
true;
{some,
{inline,
{integer_schema, _, _, _, _, _, _, _}}} ->
true;
{some,
{inline,
{number_schema, _, _, _, _, _, _, _}}} ->
true;
{some, {inline, {boolean_schema, _}}} ->
true;
_ ->
false
end
end
);
_ ->
false
end
end
),
Needs_string = ((Has_multi_content_response orelse Has_form_urlencoded)
orelse Has_multipart)
orelse gleam@list:any(
All_params,
fun(P@7) ->
(erlang:element(3, P@7) =:= in_path) orelse (erlang:element(3, P@7)
=:= in_cookie)
end
),
Needs_typed_schemas = oaspec@internal@codegen@import_analysis:operations_need_typed_schemas(
Operations
),
Needs_option_type = oaspec@internal@codegen@import_analysis:operations_have_optional_params(
Operations
)
orelse oaspec@internal@codegen@import_analysis:operations_have_optional_body(
Operations
),
Needs_some_ctor = Needs_option_type orelse any_operation_has_server(
Operations,
Ctx
),
Needs_none_ctor = Needs_option_type orelse any_operation_has_no_server(
Operations,
Ctx
),
Option_import_items = case {Needs_option_type,
Needs_some_ctor,
Needs_none_ctor} of
{true, _, _} ->
{some, <<"gleam/option.{type Option, None, Some}"/utf8>>};
{false, true, true} ->
{some, <<"gleam/option.{None, Some}"/utf8>>};
{false, true, false} ->
{some, <<"gleam/option.{Some}"/utf8>>};
{false, false, true} ->
{some, <<"gleam/option.{None}"/utf8>>};
{false, false, false} ->
none
end,
Needs_int = gleam@list:any(
All_params,
fun(P@8) -> case erlang:element(6, P@8) of
{parameter_schema,
{inline, {integer_schema, _, _, _, _, _, _, _}}} ->
true;
{parameter_schema,
{inline,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}}} ->
true;
{parameter_schema, {reference, _, _} = Sr@1} ->
case oaspec@internal@openapi@resolver:resolve_schema_ref(
Sr@1,
oaspec@internal@codegen@context:spec(Ctx)
) of
{ok, {integer_schema, _, _, _, _, _, _, _}} ->
true;
{ok,
{array_schema,
_,
{inline, {integer_schema, _, _, _, _, _, _, _}},
_,
_,
_}} ->
true;
_ ->
false
end;
_ ->
false
end end
),
Needs_bytes_helper = gleam@list:any(
Operations,
fun(Op@6) ->
{_, Operation@6, _, _} = Op@6,
gleam@list:any(
maps:to_list(erlang:element(8, Operation@6)),
fun(Entry@2) ->
{_, Ref_or@2} = Entry@2,
case Ref_or@2 of
{value, Response@2} ->
gleam@list:any(
maps:to_list(erlang:element(3, Response@2)),
fun(Ce@4) ->
{Name, _} = Ce@4,
oaspec@internal@util@content_type:from_string(
Name
)
=:= application_octet_stream
end
);
_ ->
false
end
end
)
end
),
Needs_text_helper = gleam@list:any(
Operations,
fun(Op@7) ->
{_, Operation@7, _, _} = Op@7,
gleam@list:any(
maps:to_list(erlang:element(8, Operation@7)),
fun(Entry@3) ->
{_, Ref_or@3} = Entry@3,
case Ref_or@3 of
{value, Response@3} ->
gleam@list:any(
maps:to_list(erlang:element(3, Response@3)),
fun(Ce@5) ->
{_, Mt@2} = Ce@5,
case erlang:element(2, Mt@2) of
{some, _} ->
true;
none ->
false
end
end
);
_ ->
false
end
end
)
end
),
Base_imports = [<<"gleam/result"/utf8>>,
<<"oaspec/transport"/utf8>>,
<<(oaspec@config:package(oaspec@internal@codegen@context:config(Ctx)))/binary,
"/decode"/utf8>>,
<<(oaspec@config:package(oaspec@internal@codegen@context:config(Ctx)))/binary,
"/response_types"/utf8>>],
Base_imports@1 = case Needs_int of
true ->
[<<"gleam/int"/utf8>> | Base_imports];
false ->
Base_imports
end,
Base_imports@2 = case Option_import_items of
{some, Import_line} ->
[Import_line | Base_imports@1];
none ->
Base_imports@1
end,
Base_imports@3 = case Needs_typed_schemas of
true ->
lists:append(
[<<(oaspec@config:package(
oaspec@internal@codegen@context:config(Ctx)
))/binary,
"/types"/utf8>>,
<<(oaspec@config:package(
oaspec@internal@codegen@context:config(Ctx)
))/binary,
"/encode"/utf8>>],
Base_imports@2
);
false ->
Base_imports@2
end,
Base_imports@4 = [<<(oaspec@config:package(
oaspec@internal@codegen@context:config(Ctx)
))/binary,
"/request_types"/utf8>> |
Base_imports@3],
Base_imports@5 = case Needs_string of
true ->
[<<"gleam/string"/utf8>> | Base_imports@4];
false ->
Base_imports@4
end,
Imports = case Needs_dyn_decode of
true ->
[<<"gleam/dynamic/decode as dyn_decode"/utf8>> | Base_imports@5];
false ->
Base_imports@5
end,
Imports@1 = case Needs_json of
true ->
[<<"gleam/json"/utf8>> | Imports];
false ->
Imports
end,
Imports@2 = case Needs_bool of
true ->
[<<"gleam/bool"/utf8>> | Imports@1];
false ->
Imports@1
end,
Imports@3 = case Needs_float of
true ->
[<<"gleam/float"/utf8>> | Imports@2];
false ->
Imports@2
end,
Imports@4 = case Needs_list of
true ->
[<<"gleam/list"/utf8>> | Imports@3];
false ->
Imports@3
end,
Needs_uri = Has_form_urlencoded orelse gleam@list:any(
All_params,
fun(P@9) ->
(erlang:element(3, P@9) =:= in_path) orelse (erlang:element(3, P@9)
=:= in_cookie)
end
),
Imports@5 = case Needs_uri of
true ->
[<<"gleam/uri"/utf8>> | Imports@4];
false ->
Imports@4
end,
Needs_guards = oaspec@config:validate(
oaspec@internal@codegen@context:config(Ctx)
)
andalso gleam@list:any(
Operations,
fun(Op@8) ->
{_, Operation@8, _, _} = Op@8,
case erlang:element(7, Operation@8) of
{some, {value, Rb@3}} ->
Content_entries = maps:to_list(erlang:element(3, Rb@3)),
case Content_entries of
[{_, Mt@3}] ->
case erlang:element(2, Mt@3) of
{some, {reference, _, Name@1}} ->
oaspec@internal@codegen@guards:schema_has_validator(
Name@1,
Ctx
);
_ ->
false
end;
_ ->
false
end;
_ ->
false
end
end
),
Imports@6 = case Needs_guards of
true ->
[<<(oaspec@config:package(
oaspec@internal@codegen@context:config(Ctx)
))/binary,
"/guards"/utf8>> |
Imports@5];
false ->
Imports@5
end,
Sb = begin
_pipe = oaspec@internal@util@string_extra:file_header(<<"0.28.0"/utf8>>),
oaspec@internal@util@string_extra:imports(_pipe, Imports@6)
end,
Sb@1 = begin
_pipe@1 = Sb,
_pipe@2 = oaspec@internal@util@string_extra:doc_comment(
_pipe@1,
<<"HTTP client errors."/utf8>>
),
_pipe@3 = oaspec@internal@util@string_extra:line(
_pipe@2,
<<"pub type ClientError {"/utf8>>
),
_pipe@4 = oaspec@internal@util@string_extra:indent(
_pipe@3,
1,
<<"TransportError(error: transport.TransportError)"/utf8>>
),
_pipe@5 = oaspec@internal@util@string_extra:indent(
_pipe@4,
1,
<<"DecodeFailure(detail: String)"/utf8>>
),
_pipe@6 = oaspec@internal@util@string_extra:indent(
_pipe@5,
1,
<<"InvalidResponse(detail: String)"/utf8>>
),
_pipe@7 = oaspec@internal@util@string_extra:indent(
_pipe@6,
1,
<<"UnexpectedStatus("/utf8>>
),
_pipe@8 = oaspec@internal@util@string_extra:indent(
_pipe@7,
2,
<<"status: Int,"/utf8>>
),
_pipe@9 = oaspec@internal@util@string_extra:indent(
_pipe@8,
2,
<<"headers: List(#(String, String)),"/utf8>>
),
_pipe@10 = oaspec@internal@util@string_extra:indent(
_pipe@9,
2,
<<"body: transport.Body,"/utf8>>
),
oaspec@internal@util@string_extra:indent(_pipe@10, 1, <<")"/utf8>>)
end,
Sb@2 = case Needs_guards of
true ->
_pipe@11 = Sb@1,
oaspec@internal@util@string_extra:indent(
_pipe@11,
1,
<<"ValidationError(errors: List(guards.ValidationFailure))"/utf8>>
);
false ->
Sb@1
end,
Sb@3 = begin
_pipe@12 = Sb@2,
_pipe@13 = oaspec@internal@util@string_extra:line(
_pipe@12,
<<"}"/utf8>>
),
oaspec@internal@util@string_extra:blank_line(_pipe@13)
end,
Sb@4 = generate_default_base_url(Sb@3, Ctx),
Sb@5 = case Needs_text_helper of
true ->
generate_text_body_helper(Sb@4);
false ->
Sb@4
end,
Sb@6 = case Needs_bytes_helper of
true ->
generate_bytes_body_helper(Sb@5);
false ->
Sb@5
end,
Sb@8 = gleam@list:fold(
Operations,
Sb@6,
fun(Sb@7, Op@9) ->
{Op_id, Operation@9, Path, Method} = Op@9,
generate_client_function(
Sb@7,
Op_id,
Operation@9,
Path,
Method,
Ctx
)
end
),
oaspec@internal@util@string_extra:to_string(Sb@8).
-file("src/oaspec/internal/codegen/client.gleam", 26).
?DOC(false).
-spec generate(oaspec@internal@codegen@context:context()) -> list(oaspec@internal@codegen@context:generated_file()).
generate(Ctx) ->
Client_content = generate_client(Ctx),
[{generated_file,
<<"client.gleam"/utf8>>,
Client_content,
client_target,
overwrite}].