Current section

Files

Jump to
logflare_api_client lib logflare_api_client.ex
Raw

lib/logflare_api_client.ex

defmodule LogflareApiClient do
@moduledoc false
use Tesla, only: [:post], docs: false
adapter Tesla.Adapter.Finch,
name: LogflareApiClient.Finch,
receive_timeout: 30_000
@default_api_path "/logs/elixir/logger"
@callback post_logs(Tesla.Client.t(), list(map), String.t()) ::
{:ok, Tesla.Env.t()} | {:error, term}
@spec new(%{url: String.t(), api_key: String.t()}) :: Tesla.Client.t()
def new(%{url: url, api_key: api_key}) when is_binary(url) and is_binary(api_key) do
middlewares = [
Tesla.Middleware.FollowRedirects,
{Tesla.Middleware.Headers,
[
{"x-api-key", api_key},
{"content-type", "application/bert"}
]},
{Tesla.Middleware.BaseUrl, url},
{Tesla.Middleware.Compression, format: "gzip"}
]
Tesla.client(middlewares)
end
@spec post_logs(Tesla.Client.t(), [map], String.t()) :: {:ok, Tesla.Env.t()} | {:error, term}
def post_logs(%Tesla.Client{} = client, batch, source_id) when is_list(batch) do
body = Bertex.encode(%{"batch" => batch, "source" => source_id})
Tesla.post(client, api_path(), body)
end
def api_path, do: @default_api_path
end