Current section

Files

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

src/oaspec@codegen@client_response.erl

-module(oaspec@codegen@client_response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/codegen/client_response.gleam").
-export([generate_multi_content_response/6, inline_schema_to_decoder/1, get_response_decode_expr/4, generate_single_content_response/7, array_item_to_string_fn/2, deep_object_array_item_to_string/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/oaspec/codegen/client_response.gleam", 81).
?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(),
oaspec@util@http:http_status_code(),
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_response.gleam", 140).
?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_response.gleam", 101).
?DOC(" Get the decode expression for a response body.\n").
-spec get_response_decode_expr(
oaspec@openapi@schema:schema_ref(),
binary(),
oaspec@util@http:http_status_code(),
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_response.gleam", 12).
?DOC(" Generate response handling for a single content type.\n").
-spec generate_single_content_response(
gleam@string_tree:string_tree(),
oaspec@util@http:http_status_code(),
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 oaspec@util@content_type:from_string(Media_type_name) of
text_plain ->
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 ->
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 ->
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 ->
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_response.gleam", 152).
?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,
oaspec@codegen@context:spec(Ctx)
).
-file("src/oaspec/codegen/client_response.gleam", 157).
?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>>,
oaspec@codegen@context:spec(Ctx)
);
_ ->
<<"item"/utf8>>
end.