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", 732).
?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, _) ->
Base = case erlang:element(6, Param) of
{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, {reference, Ref}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(
oaspec@openapi@resolver:ref_to_name(Ref)
))/binary>>;
_ ->
<<"String"/utf8>>
end,
case erlang:element(5, Param) of
true ->
Base;
false ->
<<<<"Option("/utf8, Base/binary>>/binary, ")"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 749).
?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, {integer_schema, _, _, _, _, _}}} ->
<<<<"int.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _}}} ->
<<<<"float.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>>;
{some, {inline, {boolean_schema, _, _}}} ->
<<<<"bool.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>>;
{some, {reference, Ref} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {string_schema, _, _, Enum_values, _, _, _, _}} when Enum_values =/= [] ->
Name = oaspec@openapi@resolver:ref_to_name(Ref),
<<<<<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"_to_string("/utf8>>/binary,
Param_name/binary>>/binary,
")"/utf8>>;
{ok, {integer_schema, _, _, _, _, _}} ->
<<<<"int.to_string("/utf8, Param_name/binary>>/binary,
")"/utf8>>;
{ok, {number_schema, _, _, _, _, _}} ->
<<<<"float.to_string("/utf8, Param_name/binary>>/binary,
")"/utf8>>;
{ok, {boolean_schema, _, _}} ->
<<<<"bool.to_string("/utf8, Param_name/binary>>/binary,
")"/utf8>>;
{ok, {string_schema, _, _, _, _, _, _, _}} ->
Param_name;
_ ->
Param_name
end;
_ ->
Param_name
end.
-file("src/oaspec/codegen/client.gleam", 782).
?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", 791).
?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, {integer_schema, _, _, _, _, _}}} ->
<<"int.to_string(v)"/utf8>>;
{some, {inline, {number_schema, _, _, _, _, _}}} ->
<<"float.to_string(v)"/utf8>>;
{some, {inline, {boolean_schema, _, _}}} ->
<<"bool.to_string(v)"/utf8>>;
{some, {reference, Ref} = Schema_ref} ->
case oaspec@openapi@resolver:resolve_schema_ref(
Schema_ref,
erlang:element(2, Ctx)
) of
{ok, {string_schema, _, _, Enum_values, _, _, _, _}} when Enum_values =/= [] ->
Name = oaspec@openapi@resolver:ref_to_name(Ref),
<<<<"encode.encode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"_to_string(v)"/utf8>>;
{ok, {integer_schema, _, _, _, _, _}} ->
<<"int.to_string(v)"/utf8>>;
{ok, {number_schema, _, _, _, _, _}} ->
<<"float.to_string(v)"/utf8>>;
{ok, {boolean_schema, _, _}} ->
<<"bool.to_string(v)"/utf8>>;
{ok, {string_schema, _, _, _, _, _, _, _}} ->
<<"v"/utf8>>;
_ ->
<<"v"/utf8>>
end;
_ ->
<<"v"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 814).
?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
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some, {reference, Ref}} ->
<<"types."/utf8,
(oaspec@util@naming:schema_to_type_name(
oaspec@openapi@resolver:ref_to_name(Ref)
))/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", 698).
?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),
[<<", body: "/utf8, Body_type/binary>>];
_ ->
[]
end,
gleam@string:join(lists:append(Param_strs, Body_param), <<""/utf8>>).
-file("src/oaspec/codegen/client.gleam", 834).
?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, Ref}} ->
Name = oaspec@openapi@resolver:ref_to_name(Ref),
<<<<"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", 906).
?DOC(" Get a status code suffix for type names.\n").
-spec status_code_suffix(binary()) -> binary().
status_code_suffix(Code) ->
case Code of
<<"200"/utf8>> ->
<<"Ok"/utf8>>;
<<"201"/utf8>> ->
<<"Created"/utf8>>;
<<"204"/utf8>> ->
<<"NoContent"/utf8>>;
<<"400"/utf8>> ->
<<"BadRequest"/utf8>>;
<<"401"/utf8>> ->
<<"Unauthorized"/utf8>>;
<<"403"/utf8>> ->
<<"Forbidden"/utf8>>;
<<"404"/utf8>> ->
<<"NotFound"/utf8>>;
<<"409"/utf8>> ->
<<"Conflict"/utf8>>;
<<"422"/utf8>> ->
<<"UnprocessableEntity"/utf8>>;
<<"500"/utf8>> ->
<<"InternalServerError"/utf8>>;
<<"default"/utf8>> ->
<<"Default"/utf8>>;
Other ->
<<"Status"/utf8, Other/binary>>
end.
-file("src/oaspec/codegen/client.gleam", 924).
?DOC(" Convert a status code string to an int pattern for case matching.\n").
-spec status_code_to_int_pattern(binary()) -> binary().
status_code_to_int_pattern(Code) ->
case Code of
<<"default"/utf8>> ->
<<"_"/utf8>>;
_ ->
Code
end.
-file("src/oaspec/codegen/client.gleam", 934).
?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", 867).
?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, Ref} ->
Name = oaspec@openapi@resolver:ref_to_name(Ref),
<<<<"decode.decode_"/utf8,
(oaspec@util@naming:to_snake_case(Name))/binary>>/binary,
"(resp.body)"/utf8>>;
{inline, {array_schema, _, Items, _, _, _}} ->
case Items of
{reference, Ref@1} ->
Name@1 = oaspec@openapi@resolver:ref_to_name(Ref@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(
status_code_suffix(Status_code)
))/binary>>,
<<<<"decode."/utf8, Fn_name/binary>>/binary, "(resp.body)"/utf8>>
end.
-file("src/oaspec/codegen/client.gleam", 286).
?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,
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@3 = begin
_pipe@2 = Sb@2,
oaspec@util@string_extra:line(
_pipe@2,
<<<<<<<<<<<<"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@4 = begin
_pipe@3 = Sb@3,
oaspec@util@string_extra:indent(
_pipe@3,
1,
<<<<"let path = \""/utf8, Path/binary>>/binary, "\""/utf8>>
)
end,
Sb@6 = gleam@list:fold(
Path_params,
Sb@4,
fun(Sb@5, 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@4 = Sb@5,
oaspec@util@string_extra:indent(
_pipe@4,
1,
<<<<<<<<"let path = string.replace(path, \"{"/utf8,
(erlang:element(2, P@4))/binary>>/binary,
"}\", "/utf8>>/binary,
To_string_expr/binary>>/binary,
")"/utf8>>
)
end
),
Sb@11 = case gleam@list:is_empty(Query_params) of
true ->
Sb@6;
false ->
Sb@7 = begin
_pipe@5 = Sb@6,
oaspec@util@string_extra:indent(
_pipe@5,
1,
<<"let query_parts = []"/utf8>>
)
end,
Sb@9 = gleam@list:fold(
Query_params,
Sb@7,
fun(Sb@8, P@5) ->
Param_name@1 = oaspec@util@naming:to_snake_case(
erlang:element(2, P@5)
),
case erlang:element(5, P@5) of
true ->
To_str = to_str_for_required(P@5, Param_name@1, Ctx),
_pipe@6 = Sb@8,
oaspec@util@string_extra:indent(
_pipe@6,
1,
<<<<<<<<"let query_parts = [\""/utf8,
(erlang:element(2, P@5))/binary>>/binary,
"=\" <> "/utf8>>/binary,
To_str/binary>>/binary,
", ..query_parts]"/utf8>>
);
false ->
To_str@1 = to_str_for_optional_value(P@5, Ctx),
_pipe@7 = Sb@8,
_pipe@8 = oaspec@util@string_extra:indent(
_pipe@7,
1,
<<<<"let query_parts = case "/utf8,
Param_name@1/binary>>/binary,
" {"/utf8>>
),
_pipe@9 = oaspec@util@string_extra:indent(
_pipe@8,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(2, P@5))/binary>>/binary,
"=\" <> "/utf8>>/binary,
To_str@1/binary>>/binary,
", ..query_parts]"/utf8>>
),
_pipe@10 = oaspec@util@string_extra:indent(
_pipe@9,
2,
<<"None -> query_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@10,
1,
<<"}"/utf8>>
)
end
end
),
Sb@10 = begin
_pipe@11 = Sb@9,
_pipe@12 = oaspec@util@string_extra:indent(
_pipe@11,
1,
<<"let query_string = string.join(query_parts, \"&\")"/utf8>>
),
_pipe@13 = oaspec@util@string_extra:indent(
_pipe@12,
1,
<<"let path = case query_string {"/utf8>>
),
_pipe@14 = oaspec@util@string_extra:indent(
_pipe@13,
2,
<<"\"\" -> path"/utf8>>
),
_pipe@15 = oaspec@util@string_extra:indent(
_pipe@14,
2,
<<"_ -> path <> \"?\" <> query_string"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@15, 1, <<"}"/utf8>>)
end,
Sb@10
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>>
end,
Sb@12 = begin
_pipe@16 = Sb@11,
_pipe@17 = oaspec@util@string_extra:indent(
_pipe@16,
1,
<<"let assert Ok(req) = request.to(config.base_url <> path)"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@17,
1,
<<<<"let req = request.set_method(req, "/utf8, Http_method/binary>>/binary,
")"/utf8>>
)
end,
Sb@13 = case erlang:element(7, Operation) of
{some, Rb} ->
Body_encode_expr = get_body_encode_expr(Rb, Op_id, Ctx),
_pipe@18 = Sb@12,
_pipe@19 = oaspec@util@string_extra:indent(
_pipe@18,
1,
<<"let req = request.set_header(req, \"content-type\", \"application/json\")"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@19,
1,
<<<<"let req = request.set_body(req, "/utf8,
Body_encode_expr/binary>>/binary,
")"/utf8>>
);
_ ->
Sb@12
end,
Sb@15 = gleam@list:fold(
Header_params,
Sb@13,
fun(Sb@14, 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@20 = Sb@14,
oaspec@util@string_extra:indent(
_pipe@20,
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@21 = Sb@14,
_pipe@22 = oaspec@util@string_extra:indent(
_pipe@21,
1,
<<<<"let req = case "/utf8, Param_name@2/binary>>/binary,
" {"/utf8>>
),
_pipe@23 = oaspec@util@string_extra:indent(
_pipe@22,
2,
<<<<<<<<"Some(v) -> request.set_header(req, \""/utf8,
Header_name/binary>>/binary,
"\", "/utf8>>/binary,
To_str@3/binary>>/binary,
")"/utf8>>
),
_pipe@24 = oaspec@util@string_extra:indent(
_pipe@23,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@24, 1, <<"}"/utf8>>)
end
end
),
Sb@19 = case gleam@list:is_empty(Cookie_params) of
true ->
Sb@15;
false ->
Sb@16 = begin
_pipe@25 = Sb@15,
oaspec@util@string_extra:indent(
_pipe@25,
1,
<<"let cookie_parts = []"/utf8>>
)
end,
Sb@18 = gleam@list:fold(
Cookie_params,
Sb@16,
fun(Sb@17, 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@26 = Sb@17,
oaspec@util@string_extra:indent(
_pipe@26,
1,
<<<<<<<<"let cookie_parts = [\""/utf8,
(erlang:element(2, P@7))/binary>>/binary,
"=\" <> "/utf8>>/binary,
To_str@4/binary>>/binary,
", ..cookie_parts]"/utf8>>
);
false ->
To_str@5 = to_str_for_optional_value(P@7, Ctx),
_pipe@27 = Sb@17,
_pipe@28 = oaspec@util@string_extra:indent(
_pipe@27,
1,
<<<<"let cookie_parts = case "/utf8,
Param_name@3/binary>>/binary,
" {"/utf8>>
),
_pipe@29 = oaspec@util@string_extra:indent(
_pipe@28,
2,
<<<<<<<<"Some(v) -> [\""/utf8,
(erlang:element(2, P@7))/binary>>/binary,
"=\" <> "/utf8>>/binary,
To_str@5/binary>>/binary,
", ..cookie_parts]"/utf8>>
),
_pipe@30 = oaspec@util@string_extra:indent(
_pipe@29,
2,
<<"None -> cookie_parts"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@30,
1,
<<"}"/utf8>>
)
end
end
),
_pipe@31 = Sb@18,
_pipe@32 = oaspec@util@string_extra:indent(
_pipe@31,
1,
<<"let req = case cookie_parts {"/utf8>>
),
_pipe@33 = oaspec@util@string_extra:indent(
_pipe@32,
2,
<<"[] -> req"/utf8>>
),
_pipe@34 = oaspec@util@string_extra:indent(
_pipe@33,
2,
<<"_ -> request.set_header(req, \"cookie\", string.join(cookie_parts, \"; \"))"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@34, 1, <<"}"/utf8>>)
end,
Effective_security = gleam@option:unwrap(erlang:element(10, Operation), []),
First_alternative_schemes = case Effective_security of
[First | _] ->
erlang:element(2, First);
[] ->
[]
end,
Sb@21 = gleam@list:fold(
First_alternative_schemes,
Sb@19,
fun(Sb@20, Sec_ref) ->
Field_name = oaspec@util@naming:to_snake_case(
erlang:element(2, Sec_ref)
),
case erlang:element(5, erlang:element(2, Ctx)) of
{some, Components} ->
case gleam_stdlib:map_get(
erlang:element(6, Components),
erlang:element(2, Sec_ref)
) of
{ok, {api_key_scheme, Header_name@1, <<"header"/utf8>>}} ->
_pipe@35 = Sb@20,
_pipe@36 = oaspec@util@string_extra:indent(
_pipe@35,
1,
<<<<"let req = case config."/utf8,
Field_name/binary>>/binary,
" {"/utf8>>
),
_pipe@37 = oaspec@util@string_extra:indent(
_pipe@36,
2,
<<<<"Some(key) -> request.set_header(req, \""/utf8,
(string:lowercase(Header_name@1))/binary>>/binary,
"\", key)"/utf8>>
),
_pipe@38 = oaspec@util@string_extra:indent(
_pipe@37,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@38,
1,
<<"}"/utf8>>
);
{ok, {api_key_scheme, Query_name, <<"query"/utf8>>}} ->
_pipe@39 = Sb@20,
_pipe@40 = oaspec@util@string_extra:indent(
_pipe@39,
1,
<<<<"let req = case config."/utf8,
Field_name/binary>>/binary,
" {"/utf8>>
),
_pipe@41 = oaspec@util@string_extra:indent(
_pipe@40,
2,
<<"Some(key) -> {"/utf8>>
),
_pipe@42 = oaspec@util@string_extra:indent(
_pipe@41,
3,
<<"let sep = case string.contains(req.path, \"?\") {"/utf8>>
),
_pipe@43 = oaspec@util@string_extra:indent(
_pipe@42,
4,
<<"True -> \"&\""/utf8>>
),
_pipe@44 = oaspec@util@string_extra:indent(
_pipe@43,
4,
<<"False -> \"?\""/utf8>>
),
_pipe@45 = oaspec@util@string_extra:indent(
_pipe@44,
3,
<<"}"/utf8>>
),
_pipe@46 = oaspec@util@string_extra:indent(
_pipe@45,
3,
<<<<"request.Request(..req, path: req.path <> sep <> \""/utf8,
Query_name/binary>>/binary,
"=\" <> key)"/utf8>>
),
_pipe@47 = oaspec@util@string_extra:indent(
_pipe@46,
2,
<<"}"/utf8>>
),
_pipe@48 = oaspec@util@string_extra:indent(
_pipe@47,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@48,
1,
<<"}"/utf8>>
);
{ok, {http_scheme, <<"bearer"/utf8>>, _}} ->
_pipe@49 = Sb@20,
_pipe@50 = oaspec@util@string_extra:indent(
_pipe@49,
1,
<<<<"let req = case config."/utf8,
Field_name/binary>>/binary,
" {"/utf8>>
),
_pipe@51 = oaspec@util@string_extra:indent(
_pipe@50,
2,
<<"Some(token) -> request.set_header(req, \"authorization\", \"Bearer \" <> token)"/utf8>>
),
_pipe@52 = oaspec@util@string_extra:indent(
_pipe@51,
2,
<<"None -> req"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@52,
1,
<<"}"/utf8>>
);
_ ->
Sb@20
end;
_ ->
Sb@20
end
end
),
Sb@22 = begin
_pipe@53 = Sb@21,
_pipe@54 = oaspec@util@string_extra:indent(
_pipe@53,
1,
<<"case config.send(req) {"/utf8>>
),
_pipe@55 = oaspec@util@string_extra:indent(
_pipe@54,
2,
<<"Error(e) -> Error(e)"/utf8>>
),
oaspec@util@string_extra:indent(_pipe@55, 2, <<"Ok(resp) -> {"/utf8>>)
end,
Responses = maps:to_list(erlang:element(8, Operation)),
Sb@23 = begin
_pipe@56 = Sb@22,
oaspec@util@string_extra:indent(
_pipe@56,
3,
<<"case resp.status {"/utf8>>
)
end,
Sb@25 = gleam@list:fold(
Responses,
Sb@23,
fun(Sb@24, Entry) ->
{Status_code, Response} = Entry,
Variant_name = <<<<<<"response_types."/utf8,
(oaspec@util@naming:schema_to_type_name(Op_id))/binary>>/binary,
"Response"/utf8>>/binary,
(status_code_suffix(Status_code))/binary>>,
Content_entries = maps:to_list(erlang:element(3, Response)),
case Content_entries of
[] ->
_pipe@57 = Sb@24,
oaspec@util@string_extra:indent(
_pipe@57,
4,
<<<<<<(status_code_to_int_pattern(Status_code))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
);
[{_, Media_type} | _] ->
case erlang:element(2, Media_type) of
{some, Schema_ref} ->
Decode_expr = get_response_decode_expr(
Schema_ref,
Op_id,
Status_code,
Ctx
),
_pipe@58 = Sb@24,
_pipe@59 = oaspec@util@string_extra:indent(
_pipe@58,
4,
<<(status_code_to_int_pattern(Status_code))/binary,
" -> {"/utf8>>
),
_pipe@60 = oaspec@util@string_extra:indent(
_pipe@59,
5,
<<<<"case "/utf8, Decode_expr/binary>>/binary,
" {"/utf8>>
),
_pipe@61 = oaspec@util@string_extra:indent(
_pipe@60,
6,
<<<<"Ok(decoded) -> Ok("/utf8,
Variant_name/binary>>/binary,
"(decoded))"/utf8>>
),
_pipe@62 = oaspec@util@string_extra:indent(
_pipe@61,
6,
<<"Error(_) -> Error(DecodeError(detail: \"Failed to decode response body\"))"/utf8>>
),
_pipe@63 = oaspec@util@string_extra:indent(
_pipe@62,
5,
<<"}"/utf8>>
),
oaspec@util@string_extra:indent(
_pipe@63,
4,
<<"}"/utf8>>
);
_ ->
_pipe@64 = Sb@24,
oaspec@util@string_extra:indent(
_pipe@64,
4,
<<<<<<(status_code_to_int_pattern(Status_code))/binary,
" -> Ok("/utf8>>/binary,
Variant_name/binary>>/binary,
")"/utf8>>
)
end
end
end
),
Has_default = gleam@list:any(
Responses,
fun(Entry@1) ->
{Code, _} = Entry@1,
Code =:= <<"default"/utf8>>
end
),
Sb@26 = case Has_default of
true ->
Sb@25;
false ->
_pipe@65 = Sb@25,
oaspec@util@string_extra:indent(
_pipe@65,
4,
<<"_ -> Error(DecodeError(detail: \"Unexpected status: \" <> int.to_string(resp.status)))"/utf8>>
)
end,
Sb@27 = begin
_pipe@66 = Sb@26,
_pipe@67 = oaspec@util@string_extra:indent(_pipe@66, 3, <<"}"/utf8>>),
_pipe@68 = oaspec@util@string_extra:indent(_pipe@67, 2, <<"}"/utf8>>),
oaspec@util@string_extra:indent(_pipe@68, 1, <<"}"/utf8>>)
end,
_pipe@69 = Sb@27,
_pipe@70 = oaspec@util@string_extra:line(_pipe@69, <<"}"/utf8>>),
oaspec@util@string_extra:blank_line(_pipe@70).
-file("src/oaspec/codegen/client.gleam", 23).
?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
),
Needs_dyn_decode = 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,
gleam@list:any(
maps:to_list(erlang:element(3, Response)),
fun(Ce) ->
{_, Mt} = Ce,
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
),
Needs_json = Needs_dyn_decode orelse 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@1) ->
{_, Mt@1} = Ce@1,
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 = gleam@list:any(
Operations,
fun(Op@3) ->
{_, Operation@3, _, _} = Op@3,
not gleam@list:is_empty(erlang:element(6, Operation@3))
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@1) -> case Entry@1 of
{_, {api_key_scheme, _, <<"query"/utf8>>}} ->
true;
_ ->
false
end end)
end,
Needs_typed_schemas = gleam@list:any(
Operations,
fun(Op@4) ->
{_, Operation@4, _, _} = Op@4,
Has_ref_body = 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@2} = Ce@2,
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@4),
fun(P@2) -> case erlang:element(6, P@2) of
{some, {reference, _}} ->
true;
_ ->
false
end end
),
Has_ref_body orelse Has_ref_params
end
),
Needs_option = gleam@list:any(
Operations,
fun(Op@5) ->
{_, Operation@5, _, _} = Op@5,
gleam@list:any(
erlang:element(6, Operation@5),
fun(P@3) -> not erlang:element(5, P@3) 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,
Sb = begin
_pipe = oaspec@util@string_extra:file_header(<<"0.3.0"/utf8>>),
oaspec@util@string_extra:imports(_pipe, Imports@3)
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@2) ->
{Scheme_name, _} = Entry@2,
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@3) ->
{Scheme_name@1, _} = Entry@3,
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@12 = gleam@list:fold(
Operations,
Sb@10,
fun(Sb@11, Op@6) ->
{Op_id, Operation@6, Path, Method} = Op@6,
generate_client_function(
Sb@11,
Op_id,
Operation@6,
Path,
Method,
Ctx
)
end
),
oaspec@util@string_extra:to_string(Sb@12).
-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}].