-module(plunk@internal@bridge). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/plunk/internal/bridge.gleam"). -export([normalize_path/1, make_request/4, decode/2, error_decoder/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -file("src/plunk/internal/bridge.gleam", 35). ?DOC(false). -spec set_header(gleam@http@request:request(binary()), binary(), binary()) -> gleam@http@request:request(binary()). set_header(Req, Key, Value) -> _record = Req, {request, erlang:element(2, _record), [{Key, Value} | erlang:element(3, Req)], erlang:element(4, _record), erlang:element(5, _record), erlang:element(6, _record), erlang:element(7, _record), erlang:element(8, _record), erlang:element(9, _record)}. -file("src/plunk/internal/bridge.gleam", 43). ?DOC(false). -spec normalize_path(binary()) -> binary(). normalize_path(Path) -> Path@1 = case gleam_stdlib:string_starts_with(Path, <<"/"/utf8>>) of true -> Path; false -> <<"/"/utf8, Path/binary>> end, case gleam_stdlib:string_ends_with(Path@1, <<"/"/utf8>>) of true -> _pipe = Path@1, _pipe@1 = gleam@string:trim(_pipe), gleam@string:drop_end(_pipe@1, 1); false -> Path@1 end. -file("src/plunk/internal/bridge.gleam", 12). ?DOC(false). -spec make_request( plunk@instance:instance(), binary(), gleam@http:method(), binary() ) -> gleam@http@request:request(binary()). make_request(Instance, Path, Method, Body) -> _pipe = gleam@http@request:new(), _pipe@1 = gleam@http@request:set_method(_pipe, Method), _pipe@2 = gleam@http@request:set_host(_pipe@1, <<"api.useplunk.com"/utf8>>), _pipe@3 = gleam@http@request:set_path( _pipe@2, <<"/v1"/utf8, (normalize_path(Path))/binary>> ), _pipe@4 = (fun(Request) -> case Method of get -> Request; _ -> gleam@http@request:set_body(Request, Body) end end)(_pipe@3), _pipe@5 = set_header( _pipe@4, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ), _pipe@6 = set_header( _pipe@5, <<"Accept"/utf8>>, <<"application/json"/utf8>> ), set_header( _pipe@6, <<"Authorization"/utf8>>, <<"Bearer "/utf8, (erlang:element(2, Instance))/binary>> ). -file("src/plunk/internal/bridge.gleam", 72). ?DOC(false). -spec decode( gleam@http@response:response(binary()), fun(() -> gleam@dynamic@decode:decoder(GPJ)) ) -> {ok, GPJ} | {error, plunk@types:plunk_error()}. decode(Res, Decoder) -> {response, Status, _, Body} = Res, case Status of Status@1 when (Status@1 >= 200) andalso (Status@1 < 300) -> case gleam@json:parse(Body, Decoder()) of {ok, Decoded} -> {ok, Decoded}; {error, Err} -> {error, {j_s_o_n_error, Err}} end; _ -> case gleam@json:parse(Body, error_decoder()) of {ok, Decoded@1} -> {error, Decoded@1}; {error, Err@1} -> {error, {j_s_o_n_error, Err@1}} end end. -file("src/plunk/internal/bridge.gleam", 58). ?DOC(false). -spec error_decoder() -> gleam@dynamic@decode:decoder(plunk@types:plunk_error()). error_decoder() -> gleam@dynamic@decode:field( <<"code"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_int/1}, fun(Code) -> gleam@dynamic@decode:field( <<"error"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Error) -> gleam@dynamic@decode:field( <<"message"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Message) -> gleam@dynamic@decode:field( <<"time"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_int/1}, fun(Time) -> gleam@dynamic@decode:success( {api_error, Code, Error, Message, Time} ) end ) end ) end ) end ).