PostHog.API.Client behaviour (posthog v2.0.0)

View Source

Behaviour and the default implementation of a PostHog API client. Uses Req.

Users are unlikely to interact with this module directly, but here's an example just in case:

Example

> client = PostHog.API.Client.client("phc_abcdedfgh", "https://us.i.posthog.com")
%PostHog.API.Client{
  client: %Req.Request{...},
  module: PostHog.API.Client
}

> client.module.request(client.client, :post, "/flags", json: %{distinct_id: "user123"}, params: %{v: 2, config: true})
{:ok, %Req.Response{status: 200, body: %{...}}}

Summary

Types

Arbitrary term that is passed as the first argument to the request/4 callback.

t()

Callbacks

Creates a struct that encapsulates all information required for making requests to PostHog's public endpoints.

Sends an API request.

Types

client()

@type client() :: any()

Arbitrary term that is passed as the first argument to the request/4 callback.

For the default client, this is a Req.Request.t/0 struct.

response()

@type response() ::
  {:ok, %{status: non_neg_integer(), body: any()}} | {:error, Exception.t()}

t()

@type t() :: %PostHog.API.Client{client: client(), module: atom()}

Callbacks

client(api_key, cloud)

@callback client(api_key :: String.t(), cloud :: String.t()) :: t()

Creates a struct that encapsulates all information required for making requests to PostHog's public endpoints.

request(client, method, url, opts)

@callback request(
  client :: client(),
  method :: atom(),
  url :: String.t(),
  opts :: keyword()
) :: response()

Sends an API request.

Things such as the API token are expected to be baked into the client argument.