-module(finch). -compile([no_auto_import, nowarn_unused_vars]). -export([stream/5, start_link/1, build/2, request/2]). -export_type([method/0, request_option/0, request/0, response/0, exception/0, response_body_key/0, response_headers_key/0, response_status_key/0]). -type method() :: head | get | put | post | patch | delete | options. -type request_option() :: {pool_timeout, integer()} | {request_timeout, integer()}. -type request() :: any(). -type response() :: any(). -type exception() :: any(). -type response_body_key() :: body. -type response_headers_key() :: headers. -type response_status_key() :: status. -spec stream( request(), gleam@erlang@atom:atom_(), JFM, fun((finch@stream:stream_block(), JFM) -> JFM), list(request_option()) ) -> {ok, JFM} | {error, exception()}. stream(Req, Name, Acc, Fun, Opts) -> 'Elixir.Finch':stream(Req, Name, Acc, Fun, Opts). -spec start_link(list(finch@otp:instance_option())) -> finch@otp:on_start(). start_link(Opts) -> 'Elixir.Finch':start_link(Opts). -spec gleam_method_to_finch_method(gleam@http:method()) -> method(). gleam_method_to_finch_method(Method) -> case Method of get -> get; head -> head; post -> post; put -> put; patch -> patch; delete -> delete; options -> options; _ -> get end. -spec build(gleam@http@request:request(binary()), list(request_option())) -> request(). build(Req, Opts) -> Url_str = begin _pipe = Req, _pipe@1 = gleam@http@request:to_uri(_pipe), gleam@uri:to_string(_pipe@1) end, 'Elixir.Finch':build( gleam_method_to_finch_method(erlang:element(2, Req)), Url_str, erlang:element(3, Req), erlang:element(4, Req), Opts ). -spec response_body(response()) -> binary(). response_body(Resp) -> 'Elixir.Map':'fetch!'(Resp, body). -spec response_status(response()) -> integer(). response_status(Resp) -> 'Elixir.Map':'fetch!'(Resp, status). -spec response_headers(response()) -> list({binary(), binary()}). response_headers(Resp) -> 'Elixir.Map':'fetch!'(Resp, headers). -spec request(request(), gleam@erlang@atom:atom_()) -> {ok, gleam@http@response:response(binary())} | {error, exception()}. request(Req, Name) -> gleam@result:then( 'Elixir.Finch':request(Req, Name), fun(Resp) -> {ok, {response, response_status(Resp), response_headers(Resp), response_body(Resp)}} end ).