Current section

Files

Jump to
oaspec src oaspec@codegen@client.erl
Raw

src/oaspec@codegen@client.erl

-module(oaspec@codegen@client).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/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.
-file("src/oaspec/codegen/client.gleam", 390).
?DOC(" Substitute server variable placeholders in a URL template with their default values.\n").
-spec substitute_server_variables(
binary(),
list({binary(), oaspec@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/codegen/client.gleam", 401).
?DOC(" Generate the default_base_url function from the first server's template and variables.\n").
-spec generate_default_base_url(
gleam@string_tree:string_tree(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_default_base_url(Sb, Ctx) ->
case erlang:element(6, erlang:element(2, Ctx)) of
[First_server | _] ->
Variables = maps:to_list(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@util@string_extra:doc_comment(
_pipe,
<<"Build the base URL from server template variables."/utf8>>
);
Doc ->
_pipe@1 = Sb,
_pipe@2 = oaspec@util@string_extra:doc_comment(
_pipe@1,
<<"Build the base URL from server template variables."/utf8>>
),
oaspec@util@string_extra:doc_comment(_pipe@2, Doc)
end,
_pipe@3 = Sb@1,
_pipe@4 = oaspec@util@string_extra:line(
_pipe@3,
<<"pub fn default_base_url() -> String {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
1,
<<<<"\""/utf8, Resolved_url/binary>>/binary, "\""/utf8>>
),
_pipe@6 = oaspec@util@string_extra:line(_pipe@5, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@6);
[] ->
_pipe@7 = Sb,
_pipe@8 = oaspec@util@string_extra:doc_comment(
_pipe@7,
<<"Build the base URL from server template variables."/utf8>>
),
_pipe@9 = oaspec@util@string_extra:line(
_pipe@8,
<<"pub fn default_base_url() -> String {"/utf8>>
),
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
1,
<<"\"\""/utf8>>
),
_pipe@11 = oaspec@util@string_extra:line(_pipe@10, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@11)
end.
-file("src/oaspec/codegen/client.gleam", 968).
?DOC(
" Generate response handling for multiple content types.\n"
" Since the response variant uses String for multi-content (to stay type-safe),\n"
" all branches return resp.body directly.\n"
).
-spec generate_multi_content_response(
gleam@string_tree:string_tree(),
binary(),
binary(),
list({binary(), oaspec@openapi@spec:media_type()}),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_multi_content_response(Sb, Status_code, Variant_name, _, _, _) ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(Status_code))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
).
-file("src/oaspec/codegen/client.gleam", 1031).
?DOC(" Convert a parameter to its Gleam type string.\n").
-spec param_to_type(
oaspec@openapi@spec:parameter(),
oaspec@codegen@context:context()
) -> binary().
param_to_type(Param, Ctx) ->
Base = oaspec@codegen@schema_dispatch:resolve_param_type(
erlang:element(6, Param),
erlang:element(2, Ctx)
),
case erlang:element(5, Param) of
true ->
Base;
false ->
<<<<"Option("/utf8, Base/binary>>/binary, ")"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1040).
?DOC(" Convert a parameter value to its String representation for URL/header use.\n").
-spec param_to_string_expr(
oaspec@openapi@spec:parameter(),
binary(),
oaspec@codegen@context:context()
) -> binary().
param_to_string_expr(Param, Param_name, Ctx) ->
case erlang:element(6, Param) of
{some, {inline, {array_schema, _, Items, _, _, _}}} ->
Item_to_str = oaspec@codegen@schema_dispatch:to_string_fn(
Items,
erlang:element(2, Ctx)
),
<<<<<<<<"string.join(list.map("/utf8, Param_name/binary>>/binary,
", "/utf8>>/binary,
Item_to_str/binary>>/binary,
"), \",\")"/utf8>>;
{some, {inline, S}} ->
oaspec@codegen@schema_dispatch:to_string_expr(S, Param_name);
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, Items@1, _, _, _}} ->
Item_to_str@1 = oaspec@codegen@schema_dispatch:to_string_fn(
Items@1,
erlang:element(2, Ctx)
),
<<<<<<<<"string.join(list.map("/utf8, Param_name/binary>>/binary,
", "/utf8>>/binary,
Item_to_str@1/binary>>/binary,
"), \",\")"/utf8>>;
_ ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Schema_ref,
Param_name,
erlang:element(2, Ctx)
)
end;
_ ->
Param_name
end.
-file("src/oaspec/codegen/client.gleam", 1079).
?DOC(" Convert a required param to string for query building.\n").
-spec to_str_for_required(
oaspec@openapi@spec:parameter(),
binary(),
oaspec@codegen@context:context()
) -> binary().
to_str_for_required(Param, Param_name, Ctx) ->
param_to_string_expr(Param, Param_name, Ctx).
-file("src/oaspec/codegen/client.gleam", 1088).
?DOC(" Convert an optional param value (bound to `v`) to string.\n").
-spec to_str_for_optional_value(
oaspec@openapi@spec:parameter(),
oaspec@codegen@context:context()
) -> binary().
to_str_for_optional_value(Param, Ctx) ->
case erlang:element(6, Param) of
{some, {inline, {array_schema, _, Items, _, _, _}}} ->
Item_to_str = oaspec@codegen@schema_dispatch:to_string_fn(
Items,
erlang:element(2, Ctx)
),
<<<<"string.join(list.map(v, "/utf8, Item_to_str/binary>>/binary,
"), \",\")"/utf8>>;
{some, {inline, S}} ->
oaspec@codegen@schema_dispatch:to_string_expr(S, <<"v"/utf8>>);
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, Items@1, _, _, _}} ->
Item_to_str@1 = oaspec@codegen@schema_dispatch:to_string_fn(
Items@1,
erlang:element(2, Ctx)
),
<<<<"string.join(list.map(v, "/utf8, Item_to_str@1/binary>>/binary,
"), \",\")"/utf8>>;
_ ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Schema_ref,
<<"v"/utf8>>,
erlang:element(2, Ctx)
)
end;
_ ->
<<"v"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1110).
?DOC(" Get the Gleam type for a request body parameter.\n").
-spec get_body_type(oaspec@openapi@spec:request_body(), binary()) -> binary().
get_body_type(Rb, Op_id) ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[_, _ | _] ->
<<"String"/utf8>>;
[{_, Media_type}] ->
case erlang:element(2, Media_type) of
{some, {reference, _, Name}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Name))/binary>>;
{some, {inline, {string_schema, _, _, _, _, _, _}}} ->
<<"String"/utf8>>;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<"Int"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<"Float"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<"Bool"/utf8>>;
{some, {inline, _}} ->
<<<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"RequestBody"/utf8>>;
_ ->
<<"String"/utf8>>
end;
[] ->
<<"String"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 988).
?DOC(" Build parameter list for function signature.\n").
-spec build_param_list(
list(oaspec@openapi@spec:parameter()),
list(oaspec@openapi@spec:parameter()),
list(oaspec@openapi@spec:parameter()),
list(oaspec@openapi@spec:parameter()),
oaspec@openapi@spec:operation(),
binary(),
oaspec@codegen@context:context()
) -> binary().
build_param_list(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation,
Op_id,
Ctx
) ->
All_params = begin
_pipe = lists:append(Path_params, Query_params),
_pipe@1 = lists:append(_pipe, Header_params),
lists:append(_pipe@1, Cookie_params)
end,
Param_strs = gleam@list:map(
All_params,
fun(P) ->
Param_name = oaspec@util@naming:to_snake_case(erlang:element(2, P)),
Param_type = param_to_type(P, Ctx),
<<<<<<", "/utf8, Param_name/binary>>/binary, ": "/utf8>>/binary,
Param_type/binary>>
end
),
_ = Ctx,
Body_param = case erlang:element(7, Operation) of
{some, Rb} ->
Body_type = get_body_type(Rb, Op_id),
Wrapped_type = case erlang:element(4, Rb) of
true ->
Body_type;
false ->
<<<<"Option("/utf8, Body_type/binary>>/binary, ")"/utf8>>
end,
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[_, _ | _] ->
[<<", content_type: String"/utf8>>,
<<", body: "/utf8, Wrapped_type/binary>>];
_ ->
[<<", body: "/utf8, Wrapped_type/binary>>]
end;
_ ->
[]
end,
gleam@string:join(lists:append(Param_strs, Body_param), <<""/utf8>>).
-file("src/oaspec/codegen/client.gleam", 1132).
?DOC(" Get the encode expression for a request body.\n").
-spec get_body_encode_expr(
oaspec@openapi@spec:request_body(),
binary(),
oaspec@codegen@context:context()
) -> binary().
get_body_encode_expr(Rb, Op_id, _) ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
case Content_entries of
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some, {reference, _, Name}} ->
<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"(body)"/utf8>>;
{some, {inline, {string_schema, _, _, _, _, _, _}}} ->
<<"json.to_string(json.string(body))"/utf8>>;
{some, {inline, {integer_schema, _, _, _, _, _, _, _}}} ->
<<"json.to_string(json.int(body))"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _, _, _}}} ->
<<"json.to_string(json.float(body))"/utf8>>;
{some, {inline, {boolean_schema, _}}} ->
<<"json.to_string(json.bool(body))"/utf8>>;
{some, {inline, _}} ->
Fn_name = <<<<"encode_"/utf8,
(oaspec@util@naming:to_snake_case(Op_id))/binary>>/binary,
"_request_body"/utf8>>,
<<<<"encode."/utf8, Fn_name/binary>>/binary, "(body)"/utf8>>;
_ ->
<<"body"/utf8>>
end;
[] ->
<<"body"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1271).
-spec multipart_field_is_binary(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> boolean().
multipart_field_is_binary(Field_schema, Ctx) ->
case Field_schema of
{inline, {string_schema, _, {some, <<"binary"/utf8>>}, _, _, _, _}} ->
true;
{reference, _, _} = Schema_ref ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {string_schema, _, {some, <<"binary"/utf8>>}, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end.
-file("src/oaspec/codegen/client.gleam", 1286).
-spec multipart_field_to_string_fn(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
multipart_field_to_string_fn(Field_schema, Ctx) ->
Result = oaspec@codegen@schema_dispatch:to_string_fn(
Field_schema,
erlang:element(2, Ctx)
),
case Result of
<<"fn(x) { x }"/utf8>> ->
<<""/utf8>>;
_ ->
Result
end.
-file("src/oaspec/codegen/client.gleam", 1164).
?DOC(" Generate multipart/form-data body encoding in the client function.\n").
-spec generate_multipart_body(
gleam@string_tree:string_tree(),
oaspec@openapi@spec:request_body(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_multipart_body(Sb, Rb, _, Ctx) ->
Boundary = <<"----oaspec-boundary"/utf8>>,
Content_entries = maps:to_list(erlang:element(3, Rb)),
{Properties@2, Required_fields} = case Content_entries of
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some,
{inline,
{object_schema, _, Properties, Required, _, _, _, _}}} ->
{maps:to_list(Properties), Required};
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok,
{object_schema,
_,
Properties@1,
Required@1,
_,
_,
_,
_}} ->
{maps:to_list(Properties@1), Required@1};
_ ->
{[], []}
end;
_ ->
{[], []}
end;
_ ->
{[], []}
end,
Sb@1 = begin
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:indent(
_pipe,
1,
<<<<"let boundary = \""/utf8, Boundary/binary>>/binary, "\""/utf8>>
),
oaspec@util@string_extra:indent(_pipe@1, 1, <<"let parts = []"/utf8>>)
end,
Sb@3 = gleam@list:fold(
Properties@2,
Sb@1,
fun(Sb@2, Prop) ->
{Field_name, Field_schema} = Prop,
Gleam_field = oaspec@util@naming:to_snake_case(Field_name),
Is_required = gleam@list:contains(Required_fields, Field_name),
Is_binary = multipart_field_is_binary(Field_schema, Ctx),
To_string_fn = case Is_binary of
true ->
<<""/utf8>>;
false ->
multipart_field_to_string_fn(Field_schema, Ctx)
end,
Part_header_binary = <<<<<<<<"\"--\" <> boundary <> \"\\r\\nContent-Disposition: form-data; name=\\\""/utf8,
Field_name/binary>>/binary,
"\\\"; filename=\\\""/utf8>>/binary,
Field_name/binary>>/binary,
"\\\"\\r\\nContent-Type: application/octet-stream\\r\\n\\r\\n\""/utf8>>,
Part_header_text = <<<<"\"--\" <> boundary <> \"\\r\\nContent-Disposition: form-data; name=\\\""/utf8,
Field_name/binary>>/binary,
"\\\"\\r\\n\\r\\n\""/utf8>>,
Part_header = case Is_binary of
true ->
Part_header_binary;
false ->
Part_header_text
end,
case Is_required of
true ->
Value_expr = case To_string_fn of
<<""/utf8>> ->
<<"body."/utf8, Gleam_field/binary>>;
Fn_name ->
<<<<<<Fn_name/binary, "(body."/utf8>>/binary,
Gleam_field/binary>>/binary,
")"/utf8>>
end,
_pipe@2 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@2,
1,
<<<<<<<<"let parts = ["/utf8, Part_header/binary>>/binary,
" <> "/utf8>>/binary,
Value_expr/binary>>/binary,
" <> \"\\r\\n\", ..parts]"/utf8>>
);
false ->
Value_expr@1 = case To_string_fn of
<<""/utf8>> ->
<<"v"/utf8>>;
Fn_name@1 ->
<<Fn_name@1/binary, "(v)"/utf8>>
end,
_pipe@3 = Sb@2,
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<"let parts = case body."/utf8, Gleam_field/binary>>/binary,
" {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
2,
<<<<<<<<"Some(v) -> ["/utf8, Part_header/binary>>/binary,
" <> "/utf8>>/binary,
Value_expr@1/binary>>/binary,
" <> \"\\r\\n\", ..parts]"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
2,
<<"None -> parts"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@6, 1, <<"}"/utf8>>)
end
end
),
_pipe@7 = Sb@3,
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
1,
<<"let body_str = string.join(parts, \"\") <> \"--\" <> boundary <> \"--\\r\\n\""/utf8>>
),
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
1,
<<"let req = request.set_header(req, \"content-type\", \"multipart/form-data; boundary=\" <> boundary)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@9,
1,
<<"let req = request.set_body(req, body_str)"/utf8>>
).
-file("src/oaspec/codegen/client.gleam", 1300).
?DOC(
" Convert an array field's items to a string expression for form-urlencoded encoding.\n"
" Returns an expression that converts `item` to a String.\n"
).
-spec form_array_item_to_string(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
form_array_item_to_string(Field_schema, Ctx) ->
case Field_schema of
{inline, {array_schema, _, Items, _, _, _}} ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Items,
<<"item"/utf8>>,
erlang:element(2, Ctx)
);
_ ->
<<"string.inspect(item)"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1449).
?DOC(
" Recursively generate bracket-encoded form fields for nested objects.\n"
" Produces key[sub]=value for leaf fields and recurses for object children.\n"
).
-spec generate_form_bracket_fields(
gleam@string_tree:string_tree(),
binary(),
binary(),
oaspec@openapi@schema:schema_ref(),
boolean(),
integer(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_form_bracket_fields(
Sb,
Key_prefix,
Accessor_prefix,
Field_schema,
_,
Indent_base,
Parts_var,
Ctx
) ->
Resolved = case Field_schema of
{inline, S} ->
{ok, S};
{reference, _, _} ->
oaspec@openapi@resolver:resolve_schema_ref(
Field_schema,
erlang:element(2, Ctx)
)
end,
case Resolved of
{ok, {object_schema, _, Properties, Required, _, _, _, _}} ->
Props = maps:to_list(Properties),
gleam@list:fold(
Props,
Sb,
fun(Sb@1, Entry) ->
{Prop_name, Prop_ref} = Entry,
Prop_field = oaspec@util@naming:to_snake_case(Prop_name),
Prop_accessor = <<<<Accessor_prefix/binary, "."/utf8>>/binary,
Prop_field/binary>>,
Prop_required = gleam@list:contains(Required, Prop_name),
Is_obj = case Prop_ref of
{inline, {object_schema, _, _, _, _, _, _, _}} ->
true;
{reference, _, _} = Sr ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end,
case Is_obj of
true ->
generate_form_bracket_fields(
Sb@1,
<<<<<<Key_prefix/binary, "["/utf8>>/binary,
Prop_name/binary>>/binary,
"]"/utf8>>,
Prop_accessor,
Prop_ref,
Prop_required,
Indent_base,
Parts_var,
Ctx
);
false ->
To_str = multipart_field_to_string_fn(Prop_ref, Ctx),
case Prop_required of
true ->
Value_expr = case To_str of
<<""/utf8>> ->
Prop_accessor;
Fn_name ->
<<<<<<Fn_name/binary, "("/utf8>>/binary,
Prop_accessor/binary>>/binary,
")"/utf8>>
end,
_pipe = Sb@1,
oaspec@util@string_extra:indent(
_pipe,
Indent_base,
<<<<<<<<<<<<<<<<<<<<"let "/utf8,
Parts_var/binary>>/binary,
" = [\""/utf8>>/binary,
Key_prefix/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
Value_expr/binary>>/binary,
"), .."/utf8>>/binary,
Parts_var/binary>>/binary,
"]"/utf8>>
);
false ->
_pipe@1 = Sb@1,
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
Indent_base,
<<<<<<<<"let "/utf8, Parts_var/binary>>/binary,
" = case "/utf8>>/binary,
Prop_accessor/binary>>/binary,
" {"/utf8>>
),
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
Indent_base + 1,
<<<<<<<<<<<<<<<<"Some(v) -> [\""/utf8,
Key_prefix/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
((case To_str of
<<""/utf8>> ->
<<"v"/utf8>>;
Fn_name@1 ->
<<Fn_name@1/binary,
"(v)"/utf8>>
end))/binary>>/binary,
"), .."/utf8>>/binary,
Parts_var/binary>>/binary,
"]"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
Indent_base + 1,
<<"None -> "/utf8, Parts_var/binary>>
),
oaspec@util@string_extra:indent(
_pipe@4,
Indent_base,
<<"}"/utf8>>
)
end
end
end
);
_ ->
Sb
end.
-file("src/oaspec/codegen/client.gleam", 1313).
?DOC(
" Generate form encoding for a nested object property.\n"
" Serializes as field[subkey]=value for each sub-property.\n"
).
-spec generate_form_nested_object(
gleam@string_tree:string_tree(),
binary(),
binary(),
oaspec@openapi@schema:schema_ref(),
boolean(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_form_nested_object(
Sb,
Field_name,
Gleam_field,
Field_schema,
Is_required,
Ctx
) ->
Resolved = case Field_schema of
{inline, S} ->
{ok, S};
{reference, _, _} ->
oaspec@openapi@resolver:resolve_schema_ref(
Field_schema,
erlang:element(2, Ctx)
)
end,
Sub_props = case Resolved of
{ok, {object_schema, _, Properties, Required, _, _, _, _}} ->
{maps:to_list(Properties), Required};
_ ->
{[], []}
end,
{Props, Required_fields} = Sub_props,
Accessor_prefix = case Is_required of
true ->
<<"body."/utf8, Gleam_field/binary>>;
false ->
<<"obj"/utf8>>
end,
Sb@1 = case Is_required of
true ->
Sb;
false ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:indent(
_pipe,
1,
<<<<"let form_parts = case body."/utf8, Gleam_field/binary>>/binary,
" {"/utf8>>
),
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
2,
<<"Some(obj) -> {"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@2,
3,
<<"let fp = form_parts"/utf8>>
)
end,
Indent_base = case Is_required of
true ->
1;
false ->
3
end,
Parts_var = case Is_required of
true ->
<<"form_parts"/utf8>>;
false ->
<<"fp"/utf8>>
end,
Sb@3 = gleam@list:fold(
Props,
Sb@1,
fun(Sb@2, Entry) ->
{Sub_name, Sub_ref} = Entry,
Sub_field = oaspec@util@naming:to_snake_case(Sub_name),
Sub_accessor = <<<<Accessor_prefix/binary, "."/utf8>>/binary,
Sub_field/binary>>,
Sub_required = gleam@list:contains(Required_fields, Sub_name),
Is_sub_object = case Sub_ref of
{inline, {object_schema, _, _, _, _, _, _, _}} ->
true;
{reference, _, _} = Sr ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end,
case Is_sub_object of
true ->
generate_form_bracket_fields(
Sb@2,
<<<<<<Field_name/binary, "["/utf8>>/binary,
Sub_name/binary>>/binary,
"]"/utf8>>,
Sub_accessor,
Sub_ref,
Sub_required,
Indent_base,
Parts_var,
Ctx
);
false ->
To_str = multipart_field_to_string_fn(Sub_ref, Ctx),
case Sub_required of
true ->
Value_expr = case To_str of
<<""/utf8>> ->
Sub_accessor;
Fn_name ->
<<<<<<Fn_name/binary, "("/utf8>>/binary,
Sub_accessor/binary>>/binary,
")"/utf8>>
end,
_pipe@3 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@3,
Indent_base,
<<<<<<<<<<<<<<<<<<<<"let "/utf8,
Parts_var/binary>>/binary,
" = [\""/utf8>>/binary,
Field_name/binary>>/binary,
"["/utf8>>/binary,
Sub_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
Value_expr/binary>>/binary,
"), .."/utf8>>/binary,
Parts_var/binary>>/binary,
"]"/utf8>>
);
false ->
_pipe@4 = Sb@2,
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
Indent_base,
<<<<<<<<"let "/utf8, Parts_var/binary>>/binary,
" = case "/utf8>>/binary,
Sub_accessor/binary>>/binary,
" {"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
Indent_base + 1,
<<<<<<<<<<<<<<<<"Some(v) -> [\""/utf8,
Field_name/binary>>/binary,
"["/utf8>>/binary,
Sub_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
((case To_str of
<<""/utf8>> ->
<<"v"/utf8>>;
Fn_name@1 ->
<<Fn_name@1/binary,
"(v)"/utf8>>
end))/binary>>/binary,
"), .."/utf8>>/binary,
Parts_var/binary>>/binary,
"]"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
Indent_base + 1,
<<"None -> "/utf8, Parts_var/binary>>
),
oaspec@util@string_extra:indent(
_pipe@7,
Indent_base,
<<"}"/utf8>>
)
end
end
end
),
case Is_required of
true ->
Sb@3;
false ->
_pipe@8 = Sb@3,
_pipe@9 = oaspec@util@string_extra:indent(_pipe@8, 3, <<"fp"/utf8>>),
_pipe@10 = oaspec@util@string_extra:indent(_pipe@9, 2, <<"}"/utf8>>),
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
2,
<<"None -> form_parts"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@11, 1, <<"}"/utf8>>)
end.
-file("src/oaspec/codegen/client.gleam", 1551).
?DOC(" Generate application/x-www-form-urlencoded body encoding in the client function.\n").
-spec generate_form_urlencoded_body(
gleam@string_tree:string_tree(),
oaspec@openapi@spec:request_body(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_form_urlencoded_body(Sb, Rb, _, Ctx) ->
Content_entries = maps:to_list(erlang:element(3, Rb)),
{Properties@2, Required_fields} = case Content_entries of
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some,
{inline,
{object_schema, _, Properties, Required, _, _, _, _}}} ->
{maps:to_list(Properties), Required};
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok,
{object_schema,
_,
Properties@1,
Required@1,
_,
_,
_,
_}} ->
{maps:to_list(Properties@1), Required@1};
_ ->
{[], []}
end;
_ ->
{[], []}
end;
_ ->
{[], []}
end,
Sb@1 = begin
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
1,
<<"let form_parts = []"/utf8>>
)
end,
Sb@3 = gleam@list:fold(
Properties@2,
Sb@1,
fun(Sb@2, Prop) ->
{Field_name, Field_schema} = Prop,
Gleam_field = oaspec@util@naming:to_snake_case(Field_name),
Is_required = gleam@list:contains(Required_fields, Field_name),
Is_array = case Field_schema of
{inline, {array_schema, _, _, _, _, _}} ->
true;
{reference, _, _} = Sr ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end,
Is_object = case Field_schema of
{inline, {object_schema, _, _, _, _, _, _, _}} ->
true;
{reference, _, _} = Sr@1 ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr@1,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end,
case Is_object of
true ->
generate_form_nested_object(
Sb@2,
Field_name,
Gleam_field,
Field_schema,
Is_required,
Ctx
);
false ->
case Is_array of
true ->
case Is_required of
true ->
_pipe@1 = Sb@2,
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
1,
<<<<"let form_parts = list.fold(body."/utf8,
Gleam_field/binary>>/binary,
", form_parts, fn(acc, item) {"/utf8>>
),
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
2,
<<<<<<<<"[\""/utf8, Field_name/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
(form_array_item_to_string(
Field_schema,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@3,
1,
<<"})"/utf8>>
);
false ->
_pipe@4 = Sb@2,
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
1,
<<<<"let form_parts = case body."/utf8,
Gleam_field/binary>>/binary,
" {"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
2,
<<"Some(items) -> list.fold(items, form_parts, fn(acc, item) {"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
3,
<<<<<<<<"[\""/utf8, Field_name/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
(form_array_item_to_string(
Field_schema,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
2,
<<"})"/utf8>>
),
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
2,
<<"None -> form_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@9,
1,
<<"}"/utf8>>
)
end;
false ->
To_str = multipart_field_to_string_fn(
Field_schema,
Ctx
),
case Is_required of
true ->
Value_expr = case To_str of
<<""/utf8>> ->
<<"body."/utf8, Gleam_field/binary>>;
Fn_name ->
<<<<<<Fn_name/binary,
"(body."/utf8>>/binary,
Gleam_field/binary>>/binary,
")"/utf8>>
end,
_pipe@10 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@10,
1,
<<<<<<<<"let form_parts = [\""/utf8,
Field_name/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
Value_expr/binary>>/binary,
"), ..form_parts]"/utf8>>
);
false ->
_pipe@11 = Sb@2,
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
1,
<<<<"let form_parts = case body."/utf8,
Gleam_field/binary>>/binary,
" {"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
Field_name/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
((case To_str of
<<""/utf8>> ->
<<"v"/utf8>>;
Fn_name@1 ->
<<Fn_name@1/binary,
"(v)"/utf8>>
end))/binary>>/binary,
"), ..form_parts]"/utf8>>
),
_pipe@14 = oaspec@util@string_extra:indent(
_pipe@13,
2,
<<"None -> form_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@14,
1,
<<"}"/utf8>>
)
end
end
end
end
),
_pipe@15 = Sb@3,
_pipe@16 = oaspec@util@string_extra:indent(
_pipe@15,
1,
<<"let body_str = string.join(form_parts, \"&\")"/utf8>>
),
_pipe@17 = oaspec@util@string_extra:indent(
_pipe@16,
1,
<<"let req = request.set_header(req, \"content-type\", \"application/x-www-form-urlencoded\")"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@17,
1,
<<"let req = request.set_body(req, body_str)"/utf8>>
).
-file("src/oaspec/codegen/client.gleam", 1750).
?DOC(
" Convert an inline schema to a decoder expression for use in generated client.\n"
" Uses dyn_decode (gleam/dynamic/decode) to avoid collision with the generated\n"
" decode module.\n"
).
-spec inline_schema_to_decoder(oaspec@openapi@schema:schema_object()) -> binary().
inline_schema_to_decoder(S) ->
case S of
{string_schema, _, _, _, _, _, _} ->
<<"dyn_decode.string"/utf8>>;
{integer_schema, _, _, _, _, _, _, _} ->
<<"dyn_decode.int"/utf8>>;
{number_schema, _, _, _, _, _, _, _} ->
<<"dyn_decode.float"/utf8>>;
{boolean_schema, _} ->
<<"dyn_decode.bool"/utf8>>;
_ ->
<<"dyn_decode.string"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1711).
?DOC(" Get the decode expression for a response body.\n").
-spec get_response_decode_expr(
oaspec@openapi@schema:schema_ref(),
binary(),
binary(),
oaspec@codegen@context:context()
) -> binary().
get_response_decode_expr(Schema_ref, Op_id, Status_code, _) ->
case Schema_ref of
{reference, _, Name} ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"(resp.body)"/utf8>>;
{inline, {array_schema, _, Items, _, _, _}} ->
case Items of
{reference, _, Name@1} ->
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name@1))/binary>>/binary,
"_list(resp.body)"/utf8>>;
{inline, Inner} ->
Inner_decoder = inline_schema_to_decoder(Inner),
<<<<"json.parse(resp.body, decode.list("/utf8,
Inner_decoder/binary>>/binary,
"))"/utf8>>
end;
{inline, {string_schema, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.string)"/utf8>>;
{inline, {integer_schema, _, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.int)"/utf8>>;
{inline, {number_schema, _, _, _, _, _, _, _}} ->
<<"json.parse(resp.body, dyn_decode.float)"/utf8>>;
{inline, {boolean_schema, _}} ->
<<"json.parse(resp.body, dyn_decode.bool)"/utf8>>;
{inline, _} ->
Fn_name = <<<<<<"decode_"/utf8,
(oaspec@util@naming:to_snake_case(Op_id))/binary>>/binary,
"_response_"/utf8>>/binary,
(oaspec@util@naming:to_snake_case(
oaspec@util@http:status_code_suffix(Status_code)
))/binary>>,
<<<<"decode."/utf8, Fn_name/binary>>/binary, "(resp.body)"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 902).
?DOC(" Generate response handling for a single content type.\n").
-spec generate_single_content_response(
gleam@string_tree:string_tree(),
binary(),
binary(),
binary(),
oaspec@openapi@spec:media_type(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_single_content_response(
Sb,
Status_code,
Variant_name,
Media_type_name,
Media_type,
Op_id,
Ctx
) ->
case Media_type_name of
<<"text/plain"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"application/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"text/xml"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
<<"application/octet-stream"/utf8>> ->
case erlang:element(2, Media_type) of
{some, _} ->
_pipe = Sb,
oaspec@util@string_extra:indent(
_pipe,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
"(resp.body))"/utf8>>
);
_ ->
_pipe@1 = Sb,
oaspec@util@string_extra:indent(
_pipe@1,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end;
_ ->
case erlang:element(2, Media_type) of
{some, Schema_ref} ->
Decode_expr = get_response_decode_expr(
Schema_ref,
Op_id,
Status_code,
Ctx
),
_pipe@2 = Sb,
_pipe@3 = oaspec@util@string_extra:indent(
_pipe@2,
4,
<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
5,
<<<<"case "/utf8, Decode_expr/binary>>/binary,
" {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
6,
<<<<"Ok(decoded) -> Ok("/utf8, Variant_name/binary>>/binary,
"(decoded))"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
6,
<<"Error(_) -> Error(DecodeError(detail: \"Failed to decode response body\"))"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
5,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@7, 4, <<"}"/utf8>>);
_ ->
_pipe@8 = Sb,
oaspec@util@string_extra:indent(
_pipe@8,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end
end.
-file("src/oaspec/codegen/client.gleam", 1762).
?DOC(
" Return a function expression that converts an array item to String.\n"
" Used in generated code: `list.map(param, <fn>)`.\n"
).
-spec array_item_to_string_fn(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
array_item_to_string_fn(Items, Ctx) ->
oaspec@codegen@schema_dispatch:to_string_fn(Items, erlang:element(2, Ctx)).
-file("src/oaspec/codegen/client.gleam", 1767).
?DOC(" Convert a deepObject array item to a string expression.\n").
-spec deep_object_array_item_to_string(
oaspec@openapi@schema:schema_ref(),
oaspec@codegen@context:context()
) -> binary().
deep_object_array_item_to_string(Prop_ref, Ctx) ->
case Prop_ref of
{inline, {array_schema, _, Items, _, _, _}} ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Items,
<<"item"/utf8>>,
erlang:element(2, Ctx)
);
_ ->
<<"item"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 1780).
?DOC(
" Check if a parameter is an array with explode behavior.\n"
" OpenAPI default: style: form has explode: true by default.\n"
).
-spec is_exploded_array_param(
oaspec@openapi@spec:parameter(),
oaspec@codegen@context:context()
) -> boolean().
is_exploded_array_param(Param, Ctx) ->
Is_array = case erlang:element(6, Param) of
{some, {inline, {array_schema, _, _, _, _, _}}} ->
true;
{some, {reference, _, _} = Sr} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end,
case Is_array of
false ->
false;
true ->
Effective_explode = case erlang:element(8, Param) of
{some, V} ->
V;
none ->
case erlang:element(7, Param) of
{some, form_style} ->
true;
none ->
true;
_ ->
false
end
end,
Effective_explode
end.
-file("src/oaspec/codegen/client.gleam", 1808).
?DOC(" Generate exploded array query parameter: tags=a&tags=b\n").
-spec generate_exploded_array_query_param(
gleam@string_tree:string_tree(),
oaspec@openapi@spec:parameter(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_exploded_array_query_param(Sb, Param, Param_name, Ctx) ->
Item_to_str = case erlang:element(6, Param) of
{some, {inline, {array_schema, _, Items, _, _, _}}} ->
array_item_to_string_fn(Items, Ctx);
{some, {reference, _, _} = Sr} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, Items@1, _, _, _}} ->
array_item_to_string_fn(Items@1, Ctx);
_ ->
<<"fn(x) { x }"/utf8>>
end;
_ ->
<<"fn(x) { x }"/utf8>>
end,
case erlang:element(5, Param) of
true ->
_pipe = Sb,
_pipe@1 = oaspec@util@string_extra:indent(
_pipe,
1,
<<<<"let query_parts = list.fold("/utf8, Param_name/binary>>/binary,
", query_parts, fn(acc, item) {"/utf8>>
),
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
2,
<<<<<<<<"[\""/utf8, (erlang:element(2, Param))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
Item_to_str/binary>>/binary,
"(item)), ..acc]"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@2, 1, <<"})"/utf8>>);
false ->
_pipe@3 = Sb,
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<"let query_parts = case "/utf8, Param_name/binary>>/binary,
" {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
2,
<<"Some(items) -> list.fold(items, query_parts, fn(acc, item) {"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
3,
<<<<<<<<"[\""/utf8, (erlang:element(2, Param))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
Item_to_str/binary>>/binary,
"(item)), ..acc]"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(_pipe@6, 2, <<"})"/utf8>>),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
2,
<<"None -> query_parts"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@8, 1, <<"}"/utf8>>)
end.
-file("src/oaspec/codegen/client.gleam", 1865).
?DOC(" Check if a parameter uses deepObject style with an object schema.\n").
-spec is_deep_object_param(
oaspec@openapi@spec:parameter(),
oaspec@codegen@context:context()
) -> boolean().
is_deep_object_param(Param, Ctx) ->
case erlang:element(6, Param) of
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, _, _, _, _, _, _}} ->
true;
_ ->
false
end;
{some, {inline, {object_schema, _, _, _, _, _, _, _}}} ->
true;
_ ->
false
end.
-file("src/oaspec/codegen/client.gleam", 2078).
?DOC(" Convert a SchemaRef to a string expression for a given accessor.\n").
-spec schema_ref_to_string_expr(
oaspec@openapi@schema:schema_ref(),
binary(),
oaspec@codegen@context:context()
) -> binary().
schema_ref_to_string_expr(Schema_ref, Accessor, Ctx) ->
oaspec@codegen@schema_dispatch:schema_ref_to_string_expr(
Schema_ref,
Accessor,
erlang:element(2, Ctx)
).
-file("src/oaspec/codegen/client.gleam", 1878).
?DOC(" Generate deepObject-style query parameters: key[prop]=value for each property.\n").
-spec generate_deep_object_query_param(
gleam@string_tree:string_tree(),
oaspec@openapi@spec:parameter(),
binary(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_deep_object_query_param(Sb, Param, Param_name, Ctx) ->
Properties@2 = case erlang:element(6, Param) of
{some, {reference, _, _} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {object_schema, _, Properties, Required, _, _, _, _}} ->
{maps:to_list(Properties), Required};
_ ->
{[], []}
end;
{some,
{inline, {object_schema, _, Properties@1, Required@1, _, _, _, _}}} ->
{maps:to_list(Properties@1), Required@1};
_ ->
{[], []}
end,
{Props, Required_fields} = Properties@2,
case erlang:element(5, Param) of
true ->
gleam@list:fold(
Props,
Sb,
fun(Sb@1, Entry) ->
{Prop_name, Prop_ref} = Entry,
Field_name = oaspec@util@naming:to_snake_case(Prop_name),
Accessor = <<<<Param_name/binary, "."/utf8>>/binary,
Field_name/binary>>,
Is_required = gleam@list:contains(
Required_fields,
Prop_name
),
Is_array = case Prop_ref of
{inline, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end,
case {Is_array, Is_required} of
{true, true} ->
_pipe = Sb@1,
_pipe@1 = oaspec@util@string_extra:indent(
_pipe,
1,
<<<<"let query_parts = list.fold("/utf8,
Accessor/binary>>/binary,
", query_parts, fn(acc, item) {"/utf8>>
),
_pipe@2 = oaspec@util@string_extra:indent(
_pipe@1,
2,
<<<<<<<<<<<<"[\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(deep_object_array_item_to_string(
Prop_ref,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@2,
1,
<<"})"/utf8>>
);
{true, false} ->
_pipe@3 = Sb@1,
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<"let query_parts = case "/utf8,
Accessor/binary>>/binary,
" {"/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
2,
<<"Some(items) -> list.fold(items, query_parts, fn(acc, item) {"/utf8>>
),
_pipe@6 = oaspec@util@string_extra:indent(
_pipe@5,
3,
<<<<<<<<<<<<"[\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(deep_object_array_item_to_string(
Prop_ref,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
_pipe@7 = oaspec@util@string_extra:indent(
_pipe@6,
2,
<<"})"/utf8>>
),
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
2,
<<"None -> query_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@8,
1,
<<"}"/utf8>>
);
{false, true} ->
To_str = schema_ref_to_string_expr(
Prop_ref,
Accessor,
Ctx
),
_pipe@9 = Sb@1,
oaspec@util@string_extra:indent(
_pipe@9,
1,
<<<<<<<<<<<<"let query_parts = [\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
To_str/binary>>/binary,
"), ..query_parts]"/utf8>>
);
{false, false} ->
_pipe@10 = Sb@1,
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
1,
<<<<"let query_parts = case "/utf8,
Accessor/binary>>/binary,
" {"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
2,
<<<<<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(schema_ref_to_string_expr(
Prop_ref,
<<"v"/utf8>>,
Ctx
))/binary>>/binary,
"), ..query_parts]"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
2,
<<"None -> query_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@13,
1,
<<"}"/utf8>>
)
end
end
);
false ->
Sb@2 = begin
_pipe@14 = Sb,
oaspec@util@string_extra:indent(
_pipe@14,
1,
<<<<"let query_parts = case "/utf8, Param_name/binary>>/binary,
" {"/utf8>>
)
end,
Sb@3 = begin
_pipe@15 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@15,
2,
<<"Some(obj) -> {"/utf8>>
)
end,
Sb@4 = begin
_pipe@16 = Sb@3,
oaspec@util@string_extra:indent(
_pipe@16,
3,
<<"let qp = query_parts"/utf8>>
)
end,
Sb@6 = gleam@list:fold(
Props,
Sb@4,
fun(Sb@5, Entry@1) ->
{Prop_name@1, Prop_ref@1} = Entry@1,
Field_name@1 = oaspec@util@naming:to_snake_case(Prop_name@1),
Accessor@1 = <<"obj."/utf8, Field_name@1/binary>>,
Is_required@1 = gleam@list:contains(
Required_fields,
Prop_name@1
),
Is_array@1 = case Prop_ref@1 of
{inline, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end,
case {Is_array@1, Is_required@1} of
{true, true} ->
_pipe@17 = Sb@5,
_pipe@18 = oaspec@util@string_extra:indent(
_pipe@17,
3,
<<<<"let qp = list.fold("/utf8,
Accessor@1/binary>>/binary,
", qp, fn(acc, item) {"/utf8>>
),
_pipe@19 = oaspec@util@string_extra:indent(
_pipe@18,
4,
<<<<<<<<<<<<"[\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name@1/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(deep_object_array_item_to_string(
Prop_ref@1,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@19,
3,
<<"})"/utf8>>
);
{true, false} ->
_pipe@20 = Sb@5,
_pipe@21 = oaspec@util@string_extra:indent(
_pipe@20,
3,
<<<<"let qp = case "/utf8, Accessor@1/binary>>/binary,
" {"/utf8>>
),
_pipe@22 = oaspec@util@string_extra:indent(
_pipe@21,
4,
<<"Some(items) -> list.fold(items, qp, fn(acc, item) {"/utf8>>
),
_pipe@23 = oaspec@util@string_extra:indent(
_pipe@22,
5,
<<<<<<<<<<<<"[\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name@1/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(deep_object_array_item_to_string(
Prop_ref@1,
Ctx
))/binary>>/binary,
"), ..acc]"/utf8>>
),
_pipe@24 = oaspec@util@string_extra:indent(
_pipe@23,
4,
<<"})"/utf8>>
),
_pipe@25 = oaspec@util@string_extra:indent(
_pipe@24,
4,
<<"None -> qp"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@25,
3,
<<"}"/utf8>>
);
{false, true} ->
To_str@1 = schema_ref_to_string_expr(
Prop_ref@1,
Accessor@1,
Ctx
),
_pipe@26 = Sb@5,
oaspec@util@string_extra:indent(
_pipe@26,
3,
<<<<<<<<<<<<"let qp = [\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name@1/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
To_str@1/binary>>/binary,
"), ..qp]"/utf8>>
);
{false, false} ->
_pipe@27 = Sb@5,
_pipe@28 = oaspec@util@string_extra:indent(
_pipe@27,
3,
<<<<"let qp = case "/utf8, Accessor@1/binary>>/binary,
" {"/utf8>>
),
_pipe@29 = oaspec@util@string_extra:indent(
_pipe@28,
4,
<<<<<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(
2,
Param
))/binary>>/binary,
"["/utf8>>/binary,
Prop_name@1/binary>>/binary,
"]=\" <> uri.percent_encode("/utf8>>/binary,
(schema_ref_to_string_expr(
Prop_ref@1,
<<"v"/utf8>>,
Ctx
))/binary>>/binary,
"), ..qp]"/utf8>>
),
_pipe@30 = oaspec@util@string_extra:indent(
_pipe@29,
4,
<<"None -> qp"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@30,
3,
<<"}"/utf8>>
)
end
end
),
Sb@7 = begin
_pipe@31 = Sb@6,
oaspec@util@string_extra:indent(_pipe@31, 3, <<"qp"/utf8>>)
end,
Sb@8 = begin
_pipe@32 = Sb@7,
oaspec@util@string_extra:indent(_pipe@32, 2, <<"}"/utf8>>)
end,
Sb@9 = begin
_pipe@33 = Sb@8,
oaspec@util@string_extra:indent(
_pipe@33,
2,
<<"None -> query_parts"/utf8>>
)
end,
_pipe@34 = Sb@9,
oaspec@util@string_extra:indent(_pipe@34, 1, <<"}"/utf8>>)
end.
-file("src/oaspec/codegen/client.gleam", 2087).
?DOC(" Capitalize the first letter of a string (for HTTP scheme prefix).\n").
-spec capitalize_first(binary()) -> binary().
capitalize_first(S) ->
case gleam_stdlib:string_pop_grapheme(S) of
{ok, {First, Rest}} ->
<<(string:uppercase(First))/binary, Rest/binary>>;
{error, _} ->
S
end.
-file("src/oaspec/codegen/client.gleam", 2259).
?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, erlang:element(2, Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Scheme_ref)
) of
{ok, {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, {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, {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, {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, {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, {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, {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, {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, {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.gleam", 2348).
?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, erlang:element(2, Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Scheme_ref)
) of
{ok, {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, {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, {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, {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, {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, {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, {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, {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, {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.gleam", 2188).
?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.gleam", 2097).
?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.gleam", 2447).
?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()) -> binary().
maybe_percent_encode(Value_expr, Param) ->
case erlang:element(10, Param) of
true ->
Value_expr;
false ->
<<<<"uri.percent_encode("/utf8, Value_expr/binary>>/binary,
")"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 453).
?DOC(" Generate a client function for a single operation.\n").
-spec generate_client_function(
gleam@string_tree:string_tree(),
binary(),
oaspec@openapi@spec:operation(),
binary(),
oaspec@openapi@spec:http_method(),
oaspec@codegen@context:context()
) -> gleam@string_tree:string_tree().
generate_client_function(Sb, Op_id, Operation, Path, Method, Ctx) ->
Fn_name = oaspec@util@naming:operation_to_function_name(Op_id),
Sb@1 = case erlang:element(3, Operation) of
{some, Summary} ->
_pipe = Sb,
oaspec@util@string_extra:doc_comment(_pipe, Summary);
_ ->
Sb
end,
Sb@2 = case erlang:element(4, Operation) of
{some, Desc} ->
_pipe@1 = Sb@1,
oaspec@util@string_extra:doc_comment(_pipe@1, Desc);
_ ->
Sb@1
end,
Sb@3 = case erlang:element(7, Operation) of
{some, Rb} ->
Content_entries = maps:to_list(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@util@string_extra:doc_comment(
_pipe@3,
<<"Supported content types: "/utf8, Ct_names/binary>>
);
_ ->
Sb@2
end;
_ ->
Sb@2
end,
Path_params = gleam@list:filter(
erlang:element(6, Operation),
fun(P) -> case erlang:element(3, P) of
in_path ->
true;
_ ->
false
end end
),
Query_params = gleam@list:filter(
erlang:element(6, Operation),
fun(P@1) -> case erlang:element(3, P@1) of
in_query ->
true;
_ ->
false
end end
),
Header_params = gleam@list:filter(
erlang:element(6, Operation),
fun(P@2) -> case erlang:element(3, P@2) of
in_header ->
true;
_ ->
false
end end
),
Cookie_params = gleam@list:filter(
erlang:element(6, Operation),
fun(P@3) -> case erlang:element(3, P@3) of
in_cookie ->
true;
_ ->
false
end end
),
Response_type = <<(oaspec@util@naming:schema_to_type_name(Op_id))/binary,
"Response"/utf8>>,
Params = build_param_list(
Path_params,
Query_params,
Header_params,
Cookie_params,
Operation,
Op_id,
Ctx
),
Sb@4 = begin
_pipe@4 = Sb@3,
oaspec@util@string_extra:line(
_pipe@4,
<<<<<<<<<<<<"pub fn "/utf8, Fn_name/binary>>/binary,
"(config: ClientConfig"/utf8>>/binary,
Params/binary>>/binary,
") -> Result(response_types."/utf8>>/binary,
Response_type/binary>>/binary,
", ClientError) {"/utf8>>
)
end,
Sb@5 = begin
_pipe@5 = Sb@4,
oaspec@util@string_extra:indent(
_pipe@5,
1,
<<<<"let path = \""/utf8, Path/binary>>/binary, "\""/utf8>>
)
end,
Sb@7 = gleam@list:fold(
Path_params,
Sb@5,
fun(Sb@6, P@4) ->
Param_name = oaspec@util@naming:to_snake_case(
erlang:element(2, P@4)
),
To_string_expr = param_to_string_expr(P@4, Param_name, Ctx),
_pipe@6 = Sb@6,
oaspec@util@string_extra:indent(
_pipe@6,
1,
<<<<<<<<"let path = string.replace(path, \"{"/utf8,
(erlang:element(2, P@4))/binary>>/binary,
"}\", uri.percent_encode("/utf8>>/binary,
To_string_expr/binary>>/binary,
"))"/utf8>>
)
end
),
Sb@12 = case gleam@list:is_empty(Query_params) of
true ->
Sb@7;
false ->
Sb@8 = begin
_pipe@7 = Sb@7,
oaspec@util@string_extra:indent(
_pipe@7,
1,
<<"let query_parts = []"/utf8>>
)
end,
Sb@10 = gleam@list:fold(
Query_params,
Sb@8,
fun(Sb@9, P@5) ->
Param_name@1 = oaspec@util@naming:to_snake_case(
erlang:element(2, P@5)
),
case {erlang:element(7, P@5),
is_deep_object_param(P@5, Ctx)} of
{{some, deep_object_style}, true} ->
generate_deep_object_query_param(
Sb@9,
P@5,
Param_name@1,
Ctx
);
{_, _} ->
case is_exploded_array_param(P@5, Ctx) of
true ->
generate_exploded_array_query_param(
Sb@9,
P@5,
Param_name@1,
Ctx
);
false ->
case erlang:element(5, P@5) of
true ->
To_str = to_str_for_required(
P@5,
Param_name@1,
Ctx
),
Encoded = maybe_percent_encode(
To_str,
P@5
),
_pipe@8 = Sb@9,
oaspec@util@string_extra:indent(
_pipe@8,
1,
<<<<<<<<"let query_parts = [\""/utf8,
(erlang:element(
2,
P@5
))/binary>>/binary,
"=\" <> "/utf8>>/binary,
Encoded/binary>>/binary,
", ..query_parts]"/utf8>>
);
false ->
To_str@1 = to_str_for_optional_value(
P@5,
Ctx
),
Encoded@1 = maybe_percent_encode(
To_str@1,
P@5
),
_pipe@9 = Sb@9,
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
1,
<<<<"let query_parts = case "/utf8,
Param_name@1/binary>>/binary,
" {"/utf8>>
),
_pipe@11 = oaspec@util@string_extra:indent(
_pipe@10,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(
2,
P@5
))/binary>>/binary,
"=\" <> "/utf8>>/binary,
Encoded@1/binary>>/binary,
", ..query_parts]"/utf8>>
),
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
2,
<<"None -> query_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@12,
1,
<<"}"/utf8>>
)
end
end
end
end
),
Sb@11 = begin
_pipe@13 = Sb@10,
_pipe@14 = oaspec@util@string_extra:indent(
_pipe@13,
1,
<<"let query_string = string.join(query_parts, \"&\")"/utf8>>
),
_pipe@15 = oaspec@util@string_extra:indent(
_pipe@14,
1,
<<"let path = case query_string {"/utf8>>
),
_pipe@16 = oaspec@util@string_extra:indent(
_pipe@15,
2,
<<"\"\" -> path"/utf8>>
),
_pipe@17 = oaspec@util@string_extra:indent(
_pipe@16,
2,
<<"_ -> path <> \"?\" <> query_string"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@17, 1, <<"}"/utf8>>)
end,
Sb@11
end,
Http_method = case Method of
get ->
<<"http.Get"/utf8>>;
post ->
<<"http.Post"/utf8>>;
put ->
<<"http.Put"/utf8>>;
delete ->
<<"http.Delete"/utf8>>;
patch ->
<<"http.Patch"/utf8>>;
head ->
<<"http.Head"/utf8>>;
options ->
<<"http.Options"/utf8>>;
trace ->
<<"http.Trace"/utf8>>
end,
Sb@13 = begin
_pipe@18 = Sb@12,
_pipe@19 = oaspec@util@string_extra:indent(
_pipe@18,
1,
<<"let assert Ok(req) = request.to(config.base_url <> path)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@19,
1,
<<<<"let req = request.set_method(req, "/utf8, Http_method/binary>>/binary,
")"/utf8>>
)
end,
Sb@16 = case erlang:element(7, Operation) of
{some, Rb@1} ->
Sb@14 = case erlang:element(4, Rb@1) of
true ->
Sb@13;
false ->
_pipe@20 = Sb@13,
_pipe@21 = oaspec@util@string_extra:indent(
_pipe@20,
1,
<<"let req = case body {"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@21,
2,
<<"Some(body) -> {"/utf8>>
)
end,
Indent_offset = case erlang:element(4, Rb@1) of
true ->
0;
false ->
2
end,
_ = Indent_offset,
Content_entries@1 = maps:to_list(erlang:element(3, Rb@1)),
Sb@15 = case Content_entries@1 of
[_, _ | _] ->
_pipe@22 = Sb@14,
_pipe@23 = oaspec@util@string_extra:indent(
_pipe@22,
1,
<<"let req = request.set_header(req, \"content-type\", content_type)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@23,
1,
<<"let req = request.set_body(req, body)"/utf8>>
);
[{Content_type_key, _}] ->
case Content_type_key of
<<"multipart/form-data"/utf8>> ->
generate_multipart_body(Sb@14, Rb@1, Op_id, Ctx);
<<"application/x-www-form-urlencoded"/utf8>> ->
generate_form_urlencoded_body(
Sb@14,
Rb@1,
Op_id,
Ctx
);
_ ->
Body_encode_expr = get_body_encode_expr(
Rb@1,
Op_id,
Ctx
),
_pipe@24 = Sb@14,
_pipe@25 = oaspec@util@string_extra:indent(
_pipe@24,
1,
<<<<"let req = request.set_header(req, \"content-type\", \""/utf8,
Content_type_key/binary>>/binary,
"\")"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@25,
1,
<<<<"let req = request.set_body(req, "/utf8,
Body_encode_expr/binary>>/binary,
")"/utf8>>
)
end;
[] ->
Sb@14
end,
case erlang:element(4, Rb@1) of
true ->
Sb@15;
false ->
_pipe@26 = Sb@15,
_pipe@27 = oaspec@util@string_extra:indent(
_pipe@26,
2,
<<"req"/utf8>>
),
_pipe@28 = oaspec@util@string_extra:indent(
_pipe@27,
1,
<<"}"/utf8>>
),
_pipe@29 = oaspec@util@string_extra:indent(
_pipe@28,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@29, 1, <<"}"/utf8>>)
end;
_ ->
Sb@13
end,
Sb@18 = gleam@list:fold(
Header_params,
Sb@16,
fun(Sb@17, P@6) ->
Param_name@2 = oaspec@util@naming:to_snake_case(
erlang:element(2, P@6)
),
Header_name = string:lowercase(erlang:element(2, P@6)),
case erlang:element(5, P@6) of
true ->
To_str@2 = param_to_string_expr(P@6, Param_name@2, Ctx),
_pipe@30 = Sb@17,
oaspec@util@string_extra:indent(
_pipe@30,
1,
<<<<<<<<"let req = request.set_header(req, \""/utf8,
Header_name/binary>>/binary,
"\", "/utf8>>/binary,
To_str@2/binary>>/binary,
")"/utf8>>
);
false ->
To_str@3 = to_str_for_optional_value(P@6, Ctx),
_pipe@31 = Sb@17,
_pipe@32 = oaspec@util@string_extra:indent(
_pipe@31,
1,
<<<<"let req = case "/utf8, Param_name@2/binary>>/binary,
" {"/utf8>>
),
_pipe@33 = oaspec@util@string_extra:indent(
_pipe@32,
2,
<<<<<<<<"Some(v) -> request.set_header(req, \""/utf8,
Header_name/binary>>/binary,
"\", "/utf8>>/binary,
To_str@3/binary>>/binary,
")"/utf8>>
),
_pipe@34 = oaspec@util@string_extra:indent(
_pipe@33,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@34, 1, <<"}"/utf8>>)
end
end
),
Sb@22 = case gleam@list:is_empty(Cookie_params) of
true ->
Sb@18;
false ->
Sb@19 = begin
_pipe@35 = Sb@18,
oaspec@util@string_extra:indent(
_pipe@35,
1,
<<"let cookie_parts = []"/utf8>>
)
end,
Sb@21 = gleam@list:fold(
Cookie_params,
Sb@19,
fun(Sb@20, P@7) ->
Param_name@3 = oaspec@util@naming:to_snake_case(
erlang:element(2, P@7)
),
case erlang:element(5, P@7) of
true ->
To_str@4 = param_to_string_expr(
P@7,
Param_name@3,
Ctx
),
_pipe@36 = Sb@20,
oaspec@util@string_extra:indent(
_pipe@36,
1,
<<<<<<<<"let cookie_parts = [\""/utf8,
(erlang:element(2, P@7))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
To_str@4/binary>>/binary,
"), ..cookie_parts]"/utf8>>
);
false ->
To_str@5 = to_str_for_optional_value(P@7, Ctx),
_pipe@37 = Sb@20,
_pipe@38 = oaspec@util@string_extra:indent(
_pipe@37,
1,
<<<<"let cookie_parts = case "/utf8,
Param_name@3/binary>>/binary,
" {"/utf8>>
),
_pipe@39 = oaspec@util@string_extra:indent(
_pipe@38,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(2, P@7))/binary>>/binary,
"=\" <> uri.percent_encode("/utf8>>/binary,
To_str@5/binary>>/binary,
"), ..cookie_parts]"/utf8>>
),
_pipe@40 = oaspec@util@string_extra:indent(
_pipe@39,
2,
<<"None -> cookie_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@40,
1,
<<"}"/utf8>>
)
end
end
),
_pipe@41 = Sb@21,
_pipe@42 = oaspec@util@string_extra:indent(
_pipe@41,
1,
<<"let req = case cookie_parts {"/utf8>>
),
_pipe@43 = oaspec@util@string_extra:indent(
_pipe@42,
2,
<<"[] -> req"/utf8>>
),
_pipe@44 = oaspec@util@string_extra:indent(
_pipe@43,
2,
<<"_ -> request.set_header(req, \"cookie\", string.join(cookie_parts, \"; \"))"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@44, 1, <<"}"/utf8>>)
end,
Effective_security = case erlang:element(10, Operation) of
{some, Sec} ->
Sec;
none ->
erlang:element(7, erlang:element(2, Ctx))
end,
Sb@25 = case Effective_security of
[] ->
Sb@22;
Alternatives ->
Sb@24 = gleam@list:fold(
Alternatives,
Sb@22,
fun(Sb@23, Alt) ->
All_scopes = gleam@list:flat_map(
erlang:element(2, Alt),
fun(S) -> erlang:element(3, S) end
),
case All_scopes of
[] ->
Sb@23;
Scopes ->
_pipe@45 = Sb@23,
oaspec@util@string_extra:indent(
_pipe@45,
1,
<<"// Required scopes: "/utf8,
(gleam@string:join(Scopes, <<", "/utf8>>))/binary>>
)
end
end
),
generate_security_or_chain(Sb@24, Ctx, Alternatives, 1)
end,
Sb@26 = begin
_pipe@46 = Sb@25,
_pipe@47 = oaspec@util@string_extra:indent(
_pipe@46,
1,
<<"case config.send(req) {"/utf8>>
),
_pipe@48 = oaspec@util@string_extra:indent(
_pipe@47,
2,
<<"Error(e) -> Error(e)"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@48, 2, <<"Ok(resp) -> {"/utf8>>)
end,
Responses = oaspec@util@http:sort_response_entries(
maps:to_list(erlang:element(8, Operation))
),
Sb@27 = begin
_pipe@49 = Sb@26,
oaspec@util@string_extra:indent(
_pipe@49,
3,
<<"case resp.status {"/utf8>>
)
end,
Sb@29 = gleam@list:fold(
Responses,
Sb@27,
fun(Sb@28, Entry) ->
{Status_code, Response} = Entry,
Variant_name = <<<<<<"response_types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"Response"/utf8>>/binary,
(oaspec@util@http:status_code_suffix(Status_code))/binary>>,
Content_entries@2 = maps:to_list(erlang:element(3, Response)),
case Content_entries@2 of
[] ->
_pipe@50 = Sb@28,
oaspec@util@string_extra:indent(
_pipe@50,
4,
<<<<<<(oaspec@util@http:status_code_to_int_pattern(
Status_code
))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
);
[{Single_ct, Single_mt}] ->
generate_single_content_response(
Sb@28,
Status_code,
Variant_name,
Single_ct,
Single_mt,
Op_id,
Ctx
);
Multiple ->
generate_multi_content_response(
Sb@28,
Status_code,
Variant_name,
Multiple,
Op_id,
Ctx
)
end
end
),
Has_default = gleam@list:any(
Responses,
fun(Entry@1) ->
{Code, _} = Entry@1,
Code =:= <<"default"/utf8>>
end
),
Sb@30 = case Has_default of
true ->
Sb@29;
false ->
_pipe@51 = Sb@29,
oaspec@util@string_extra:indent(
_pipe@51,
4,
<<"_ -> Error(DecodeError(detail: \"Unexpected status: \" <> int.to_string(resp.status)))"/utf8>>
)
end,
Sb@31 = begin
_pipe@52 = Sb@30,
_pipe@53 = oaspec@util@string_extra:indent(_pipe@52, 3, <<"}"/utf8>>),
_pipe@54 = oaspec@util@string_extra:indent(_pipe@53, 2, <<"}"/utf8>>),
oaspec@util@string_extra:indent(_pipe@54, 1, <<"}"/utf8>>)
end,
_pipe@55 = Sb@31,
_pipe@56 = oaspec@util@string_extra:line(_pipe@55, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@56).
-file("src/oaspec/codegen/client.gleam", 29).
?DOC(" Generate the client module with functions for each operation.\n").
-spec generate_client(oaspec@codegen@context:context()) -> binary().
generate_client(Ctx) ->
Operations = oaspec@codegen@types:collect_operations(Ctx),
All_params = gleam@list:flat_map(
Operations,
fun(Op) ->
{_, Operation, _, _} = Op,
erlang:element(6, Operation)
end
),
Needs_bool = gleam@list:any(
All_params,
fun(P) -> case erlang:element(6, P) of
{some, {inline, {boolean_schema, _}}} ->
true;
_ ->
false
end end
),
Needs_float = gleam@list:any(
All_params,
fun(P@1) -> case erlang:element(6, P@1) of
{some, {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) ->
{_, Response} = Entry,
erlang:length(maps:to_list(erlang:element(3, Response))) > 1
end
)
end
),
Has_form_urlencoded = gleam@list:any(
Operations,
fun(Op@2) ->
{_, Operation@2, _, _} = Op@2,
case erlang:element(7, Operation@2) of
{some, 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
),
Needs_list = (Has_form_urlencoded orelse Has_multi_content_response) orelse gleam@list:any(
All_params,
fun(P@2) -> case erlang:element(6, P@2) of
{some, {inline, {array_schema, _, _, _, _, _}}} ->
true;
{some, {reference, _, _} = Sr} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Sr,
erlang:element(2, Ctx)
) of
{ok, {array_schema, _, _, _, _, _}} ->
true;
_ ->
false
end;
_ ->
false
end end
),
Needs_dyn_decode = gleam@list:any(
Operations,
fun(Op@3) ->
{_, Operation@3, _, _} = Op@3,
gleam@list:any(
maps:to_list(erlang:element(8, Operation@3)),
fun(Entry@1) ->
{_, Response@1} = Entry@1,
gleam@list:any(
maps:to_list(erlang:element(3, Response@1)),
fun(Ce@1) ->
{Media_type_name, Mt} = Ce@1,
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
)
end
)
end
),
Needs_json = Needs_dyn_decode orelse gleam@list:any(
Operations,
fun(Op@4) ->
{_, Operation@4, _, _} = Op@4,
case erlang:element(7, Operation@4) of
{some, Rb@1} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb@1)),
fun(Ce@2) ->
{_, Mt@1} = Ce@2,
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 gleam@list:any(
Operations,
fun(Op@5) ->
{_, Operation@5, _, _} = Op@5,
not gleam@list:is_empty(erlang:element(6, Operation@5))
end
))
orelse gleam@list:any(
Operations,
fun(Op@6) ->
{_, Operation@6, _, _} = Op@6,
case erlang:element(7, Operation@6) of
{some, Rb@2} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb@2)),
fun(Ce@3) ->
{Key@1, _} = Ce@3,
Key@1 =:= <<"multipart/form-data"/utf8>>
end
);
_ ->
false
end
end
))
orelse begin
Security_schemes = case erlang:element(5, erlang:element(2, Ctx)) of
{some, C} ->
maps:to_list(erlang:element(6, C));
_ ->
[]
end,
gleam@list:any(Security_schemes, fun(Entry@2) -> case Entry@2 of
{_, {api_key_scheme, _, scheme_in_query}} ->
true;
_ ->
false
end end)
end,
Needs_typed_schemas = gleam@list:any(
Operations,
fun(Op@7) ->
{_, Operation@7, _, _} = Op@7,
Has_ref_body = case erlang:element(7, Operation@7) of
{some, Rb@3} ->
gleam@list:any(
maps:to_list(erlang:element(3, Rb@3)),
fun(Ce@4) ->
{_, Mt@2} = Ce@4,
case erlang:element(2, Mt@2) of
{some, {reference, _, _}} ->
true;
{some,
{inline,
{object_schema, _, _, _, _, _, _, _}}} ->
true;
{some, {inline, {all_of_schema, _, _}}} ->
true;
_ ->
false
end
end
);
_ ->
false
end,
Has_ref_params = gleam@list:any(
erlang:element(6, Operation@7),
fun(P@3) -> case erlang:element(6, P@3) of
{some, {reference, _, _}} ->
true;
_ ->
false
end end
),
Has_ref_body orelse Has_ref_params
end
),
Needs_option = gleam@list:any(
Operations,
fun(Op@8) ->
{_, Operation@8, _, _} = Op@8,
gleam@list:any(
erlang:element(6, Operation@8),
fun(P@4) -> not erlang:element(5, P@4) end
)
end
)
orelse begin
Security_schemes@1 = case erlang:element(5, erlang:element(2, Ctx)) of
{some, C@1} ->
maps:to_list(erlang:element(6, C@1));
_ ->
[]
end,
not gleam@list:is_empty(Security_schemes@1)
end,
Base_imports = [<<"gleam/http/request"/utf8>>,
<<"gleam/http"/utf8>>,
<<"gleam/int"/utf8>>,
<<(erlang:element(5, erlang:element(3, Ctx)))/binary, "/decode"/utf8>>,
<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/response_types"/utf8>>],
Base_imports@1 = case Needs_option of
true ->
[<<"gleam/option.{type Option, None, Some}"/utf8>> | Base_imports];
false ->
Base_imports
end,
Base_imports@2 = case Needs_typed_schemas of
true ->
lists:append(
[<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/types"/utf8>>,
<<(erlang:element(5, erlang:element(3, Ctx)))/binary,
"/encode"/utf8>>],
Base_imports@1
);
false ->
Base_imports@1
end,
Base_imports@3 = case Needs_string of
true ->
[<<"gleam/string"/utf8>> | Base_imports@2];
false ->
Base_imports@2
end,
Imports = case Needs_dyn_decode of
true ->
[<<"gleam/dynamic/decode as dyn_decode"/utf8>> | Base_imports@3];
false ->
Base_imports@3
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(
Operations,
fun(Op@9) ->
{_, Operation@9, _, _} = Op@9,
not gleam@list:is_empty(erlang:element(6, Operation@9))
end
),
Imports@5 = case Needs_uri of
true ->
[<<"gleam/uri"/utf8>> | Imports@4];
false ->
Imports@4
end,
Has_cookie_api_key = case erlang:element(5, erlang:element(2, Ctx)) of
{some, C@2} ->
gleam@list:any(
maps:to_list(erlang:element(6, C@2)),
fun(Entry@3) -> case Entry@3 of
{_, {api_key_scheme, _, scheme_in_cookie}} ->
true;
_ ->
false
end end
);
_ ->
false
end,
Imports@7 = case Has_cookie_api_key of
true ->
Imports@6 = case Needs_list of
true ->
Imports@5;
false ->
[<<"gleam/list"/utf8>> | Imports@5]
end,
[<<"gleam/result"/utf8>> | Imports@6];
false ->
Imports@5
end,
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.5.0"/utf8>>),
oaspec@util@string_extra:imports(_pipe, Imports@7)
end,
Security_schemes@2 = case erlang:element(5, erlang:element(2, Ctx)) of
{some, Components} ->
maps:to_list(erlang:element(6, Components));
_ ->
[]
end,
Sb@1 = begin
_pipe@1 = Sb,
_pipe@2 = oaspec@util@string_extra:doc_comment(
_pipe@1,
<<"HTTP client configuration."/utf8>>
),
_pipe@3 = oaspec@util@string_extra:line(
_pipe@2,
<<"pub type ClientConfig {"/utf8>>
),
_pipe@4 = oaspec@util@string_extra:indent(
_pipe@3,
1,
<<"ClientConfig("/utf8>>
),
_pipe@5 = oaspec@util@string_extra:indent(
_pipe@4,
2,
<<"base_url: String,"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@5,
2,
<<"send: fn(request.Request(String)) -> Result(ClientResponse, ClientError),"/utf8>>
)
end,
Sb@3 = gleam@list:fold(
Security_schemes@2,
Sb@1,
fun(Sb@2, Entry@4) ->
{Scheme_name, _} = Entry@4,
Field_name = oaspec@util@naming:to_snake_case(Scheme_name),
_pipe@6 = Sb@2,
oaspec@util@string_extra:indent(
_pipe@6,
2,
<<Field_name/binary, ": Option(String),"/utf8>>
)
end
),
Sb@4 = begin
_pipe@7 = Sb@3,
_pipe@8 = oaspec@util@string_extra:indent(_pipe@7, 1, <<")"/utf8>>),
_pipe@9 = oaspec@util@string_extra:line(_pipe@8, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@9)
end,
Sb@5 = begin
_pipe@10 = Sb@4,
_pipe@11 = oaspec@util@string_extra:doc_comment(
_pipe@10,
<<"Raw HTTP response from the server."/utf8>>
),
_pipe@12 = oaspec@util@string_extra:line(
_pipe@11,
<<"pub type ClientResponse {"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
1,
<<"ClientResponse("/utf8>>
),
_pipe@14 = oaspec@util@string_extra:indent(
_pipe@13,
2,
<<"status: Int,"/utf8>>
),
_pipe@15 = oaspec@util@string_extra:indent(
_pipe@14,
2,
<<"body: String,"/utf8>>
),
_pipe@16 = oaspec@util@string_extra:indent(_pipe@15, 1, <<")"/utf8>>),
_pipe@17 = oaspec@util@string_extra:line(_pipe@16, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@17)
end,
Sb@6 = begin
_pipe@18 = Sb@5,
_pipe@19 = oaspec@util@string_extra:doc_comment(
_pipe@18,
<<"HTTP client errors."/utf8>>
),
_pipe@20 = oaspec@util@string_extra:line(
_pipe@19,
<<"pub type ClientError {"/utf8>>
),
_pipe@21 = oaspec@util@string_extra:indent(
_pipe@20,
1,
<<"ConnectionError(detail: String)"/utf8>>
),
_pipe@22 = oaspec@util@string_extra:indent(
_pipe@21,
1,
<<"TimeoutError"/utf8>>
),
_pipe@23 = oaspec@util@string_extra:indent(
_pipe@22,
1,
<<"DecodeError(detail: String)"/utf8>>
),
_pipe@24 = oaspec@util@string_extra:line(_pipe@23, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@24)
end,
Sb@7 = begin
_pipe@25 = Sb@6,
_pipe@26 = oaspec@util@string_extra:doc_comment(
_pipe@25,
<<"Create a new client configuration."/utf8>>
),
_pipe@27 = oaspec@util@string_extra:line(
_pipe@26,
<<"pub fn new("/utf8>>
),
_pipe@28 = oaspec@util@string_extra:indent(
_pipe@27,
1,
<<"base_url: String,"/utf8>>
),
_pipe@29 = oaspec@util@string_extra:indent(
_pipe@28,
1,
<<"send: fn(request.Request(String)) -> Result(ClientResponse, ClientError),"/utf8>>
),
_pipe@30 = oaspec@util@string_extra:line(
_pipe@29,
<<") -> ClientConfig {"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@30,
1,
<<"ClientConfig(base_url:, send:,"/utf8>>
)
end,
Sb@9 = gleam@list:fold(
Security_schemes@2,
Sb@7,
fun(Sb@8, Entry@5) ->
{Scheme_name@1, _} = Entry@5,
Field_name@1 = oaspec@util@naming:to_snake_case(Scheme_name@1),
_pipe@31 = Sb@8,
oaspec@util@string_extra:indent(
_pipe@31,
2,
<<Field_name@1/binary, ": None,"/utf8>>
)
end
),
Sb@10 = begin
_pipe@32 = Sb@9,
_pipe@33 = oaspec@util@string_extra:indent(_pipe@32, 1, <<")"/utf8>>),
_pipe@34 = oaspec@util@string_extra:line(_pipe@33, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@34)
end,
Sb@11 = generate_default_base_url(Sb@10, Ctx),
Sb@13 = gleam@list:fold(
Operations,
Sb@11,
fun(Sb@12, Op@10) ->
{Op_id, Operation@10, Path, Method} = Op@10,
generate_client_function(
Sb@12,
Op_id,
Operation@10,
Path,
Method,
Ctx
)
end
),
oaspec@util@string_extra:to_string(Sb@13).
-file("src/oaspec/codegen/client.gleam", 16).
?DOC(" Generate client SDK files.\n").
-spec generate(oaspec@codegen@context:context()) -> list(oaspec@codegen@context:generated_file()).
generate(Ctx) ->
Client_content = generate_client(Ctx),
[{generated_file, <<"client.gleam"/utf8>>, Client_content, client_target}].