FoPost.Client (FoPost v0.1.0)

Copy Markdown View Source

The client struct every resource function takes as its first argument.

Build one with FoPost.new/1 and pass it around explicitly — there is no global client and no process state, so a client is just data and is safe to share between processes.

client = FoPost.new(api_key: "fp_...")

Transport

Requests go out over Req, which pools connections through the Finch instance Req starts with its own application. Nothing needs to be added to a host application's supervision tree. To use your own pool, pass it through:

FoPost.new(api_key: key, req_options: [finch: MyApp.Finch])

:req_options is merged last, so it wins over everything the SDK sets — including the retry policy. It is the escape hatch for proxies, custom TLS, and test stubs.

Summary

Functions

Builds a client. See FoPost.new/1 for the options.

Sends one authenticated request and decodes the response.

The SDK version, as reported in the User-Agent header.

Types

t()

@type t() :: %FoPost.Client{
  api_key: String.t(),
  base_url: String.t(),
  max_retries: non_neg_integer(),
  receive_timeout: pos_integer(),
  req_options: keyword(),
  user_agent: String.t()
}

Functions

new(opts \\ [])

@spec new(keyword()) :: t()

Builds a client. See FoPost.new/1 for the options.

request(client, method, path, opts \\ [])

@spec request(t(), atom(), String.t(), keyword()) ::
  {:ok, term()} | {:error, FoPost.Error.t()}

Sends one authenticated request and decodes the response.

method is an atom (:get, :post, :put, :delete), path is relative to the client's base URL. Options are passed straight through to Req, so :params, :json, and :form_multipart all work, plus one of our own:

  • :unwrap — peel the {"data": ...} envelope off a successful response. Defaults to true; list endpoints that also need meta pass false.

Answers {:ok, body} or {:error, %FoPost.Error{}}.

version()

@spec version() :: String.t()

The SDK version, as reported in the User-Agent header.