-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", 378). ?DOC(" Convert a parameter to its Gleam type.\n"). -spec param_to_type(oaspec@openapi@spec:parameter()) -> 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}} -> oaspec@util@naming:schema_to_type_name( oaspec@openapi@resolver:ref_to_name(Ref) ); _ -> <<"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", 350). ?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(). build_param_list( Path_params, Query_params, Header_params, Cookie_params, Operation ) -> All_params = begin _pipe = lists:append(Path_params, Query_params), _pipe@1 = lists:append(_pipe, Header_params), lists:append(_pipe@1, Cookie_params) end, 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), <<<<<<", "/utf8, Param_name/binary>>/binary, ": "/utf8>>/binary, Param_type/binary>> end ), Body_param = case erlang:element(7, Operation) of {some, _} -> [<<", body: String"/utf8>>]; _ -> [] end, gleam@string:join(lists:append(Param_strs, Body_param), <<""/utf8>>). -file("src/oaspec/codegen/client.gleam", 395). ?DOC(" Convert a required param to string for query building.\n"). -spec to_str_for_required(oaspec@openapi@spec:parameter(), binary()) -> binary(). to_str_for_required(Param, Param_name) -> case erlang:element(6, Param) of {some, {inline, {integer_schema, _, _, _, _, _}}} -> <<<<"int.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>>; _ -> Param_name end. -file("src/oaspec/codegen/client.gleam", 116). ?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, _) -> 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 ), Params = build_param_list( Path_params, Query_params, Header_params, Cookie_params, Operation ), 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(ClientResponse, 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 = case erlang:element(6, P@4) of {some, {inline, {integer_schema, _, _, _, _, _}}} -> <<<<"int.to_string("/utf8, Param_name/binary>>/binary, ")"/utf8>>; _ -> Param_name end, _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) ), To_str = case erlang:element(6, P@5) of {some, {inline, {integer_schema, _, _, _, _, _}}} -> <<"int.to_string(v)"/utf8>>; _ -> <<"v"/utf8>> end, case erlang:element(5, P@5) of true -> _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_for_required(P@5, Param_name@1))/binary>>/binary, ", ..query_parts]"/utf8>> ); false -> _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/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, Has_body = gleam@option:is_some(erlang:element(7, Operation)), 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 Has_body of true -> _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, body)"/utf8>> ); false -> 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 -> _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, Param_name@2/binary>>/binary, ")"/utf8>> ); false -> _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, "\", v)"/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@17 = gleam@list:fold( Cookie_params, Sb@15, fun(Sb@16, P@7) -> Param_name@3 = oaspec@util@naming:to_snake_case( erlang:element(2, P@7) ), case erlang:element(5, P@7) of true -> _pipe@25 = Sb@16, oaspec@util@string_extra:indent( _pipe@25, 1, <<<<<<<<"let req = request.set_header(req, \"cookie\", \""/utf8, (erlang:element(2, P@7))/binary>>/binary, "=\" <> "/utf8>>/binary, Param_name@3/binary>>/binary, ")"/utf8>> ); false -> _pipe@26 = Sb@16, _pipe@27 = oaspec@util@string_extra:indent( _pipe@26, 1, <<<<"let req = case "/utf8, Param_name@3/binary>>/binary, " {"/utf8>> ), _pipe@28 = oaspec@util@string_extra:indent( _pipe@27, 2, <<<<"Some(v) -> request.set_header(req, \"cookie\", \""/utf8, (erlang:element(2, P@7))/binary>>/binary, "=\" <> v)"/utf8>> ), _pipe@29 = oaspec@util@string_extra:indent( _pipe@28, 2, <<"None -> req"/utf8>> ), oaspec@util@string_extra:indent(_pipe@29, 1, <<"}"/utf8>>) end end ), Sb@18 = begin _pipe@30 = Sb@17, oaspec@util@string_extra:indent( _pipe@30, 1, <<"config.send(req)"/utf8>> ) end, _pipe@31 = Sb@18, _pipe@32 = oaspec@util@string_extra:line(_pipe@31, <<"}"/utf8>>), oaspec@util@string_extra:blank_line(_pipe@32). -file("src/oaspec/codegen/client.gleam", 20). ?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), Needs_types = gleam@list:any( Operations, fun(Op) -> {_, Operation, _, _} = Op, gleam@list:any( erlang:element(6, Operation), fun(P) -> case erlang:element(6, P) of {some, {reference, _}} -> true; _ -> false end end ) end ), Base_imports = [<<"gleam/http/request"/utf8>>, <<"gleam/http"/utf8>>, <<"gleam/int"/utf8>>, <<"gleam/option.{type Option, None, Some}"/utf8>>, <<"gleam/string"/utf8>>], Imports = case Needs_types of true -> lists:append( Base_imports, [<<(erlang:element(5, erlang:element(3, Ctx)))/binary, "/types"/utf8>>] ); false -> Base_imports end, Sb = begin _pipe = oaspec@util@string_extra:file_header(<<"0.1.2"/utf8>>), oaspec@util@string_extra:imports(_pipe, Imports) 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>> ), _pipe@6 = oaspec@util@string_extra:indent( _pipe@5, 2, <<"send: fn(request.Request(String)) -> Result(ClientResponse, ClientError),"/utf8>> ), _pipe@7 = oaspec@util@string_extra:indent(_pipe@6, 1, <<")"/utf8>>), _pipe@8 = oaspec@util@string_extra:line(_pipe@7, <<"}"/utf8>>), oaspec@util@string_extra:blank_line(_pipe@8) end, Sb@2 = begin _pipe@9 = Sb@1, _pipe@10 = oaspec@util@string_extra:doc_comment( _pipe@9, <<"Raw HTTP response from the server."/utf8>> ), _pipe@11 = oaspec@util@string_extra:line( _pipe@10, <<"pub type ClientResponse {"/utf8>> ), _pipe@12 = oaspec@util@string_extra:indent( _pipe@11, 1, <<"ClientResponse("/utf8>> ), _pipe@13 = oaspec@util@string_extra:indent( _pipe@12, 2, <<"status: Int,"/utf8>> ), _pipe@14 = oaspec@util@string_extra:indent( _pipe@13, 2, <<"body: String,"/utf8>> ), _pipe@15 = oaspec@util@string_extra:indent(_pipe@14, 1, <<")"/utf8>>), _pipe@16 = oaspec@util@string_extra:line(_pipe@15, <<"}"/utf8>>), oaspec@util@string_extra:blank_line(_pipe@16) end, Sb@3 = begin _pipe@17 = Sb@2, _pipe@18 = oaspec@util@string_extra:doc_comment( _pipe@17, <<"HTTP client errors."/utf8>> ), _pipe@19 = oaspec@util@string_extra:line( _pipe@18, <<"pub type ClientError {"/utf8>> ), _pipe@20 = oaspec@util@string_extra:indent( _pipe@19, 1, <<"ConnectionError(detail: String)"/utf8>> ), _pipe@21 = oaspec@util@string_extra:indent( _pipe@20, 1, <<"TimeoutError"/utf8>> ), _pipe@22 = oaspec@util@string_extra:indent( _pipe@21, 1, <<"DecodeError(detail: String)"/utf8>> ), _pipe@23 = oaspec@util@string_extra:line(_pipe@22, <<"}"/utf8>>), oaspec@util@string_extra:blank_line(_pipe@23) end, Sb@4 = begin _pipe@24 = Sb@3, _pipe@25 = oaspec@util@string_extra:doc_comment( _pipe@24, <<"Create a new client configuration."/utf8>> ), _pipe@26 = oaspec@util@string_extra:line( _pipe@25, <<"pub fn new("/utf8>> ), _pipe@27 = oaspec@util@string_extra:indent( _pipe@26, 1, <<"base_url: String,"/utf8>> ), _pipe@28 = oaspec@util@string_extra:indent( _pipe@27, 1, <<"send: fn(request.Request(String)) -> Result(ClientResponse, ClientError),"/utf8>> ), _pipe@29 = oaspec@util@string_extra:line( _pipe@28, <<") -> ClientConfig {"/utf8>> ), _pipe@30 = oaspec@util@string_extra:indent( _pipe@29, 1, <<"ClientConfig(base_url:, send:)"/utf8>> ), _pipe@31 = oaspec@util@string_extra:line(_pipe@30, <<"}"/utf8>>), oaspec@util@string_extra:blank_line(_pipe@31) end, Operations@1 = oaspec@codegen@types:collect_operations(Ctx), Sb@6 = gleam@list:fold( Operations@1, Sb@4, fun(Sb@5, Op@1) -> {Op_id, Operation@1, Path, Method} = Op@1, generate_client_function( Sb@5, Op_id, Operation@1, Path, Method, Ctx ) end ), oaspec@util@string_extra:to_string(Sb@6). -file("src/oaspec/codegen/client.gleam", 13). ?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}].