Current section

Files

Jump to
tinkex lib tinkex api session.ex
Raw

lib/tinkex/api/session.ex

defmodule Tinkex.API.Session do
@moduledoc """
Session management endpoints.
Uses :session pool (critical, keep-alive).
Pool size: 5 connections with infinite idle time.
"""
alias Tinkex.Types.CreateSessionResponse
@doc """
Create a new session.
## Examples
Tinkex.API.Session.create(
%{model_id: "...", config: %{}},
config: config
)
"""
@spec create(map(), keyword()) ::
{:ok, map()} | {:error, Tinkex.Error.t()}
def create(request, opts) do
client = Tinkex.API.client_module(opts)
client.post("/api/v1/create_session", request, Keyword.put(opts, :pool_type, :session))
end
@doc """
Create a new session with typed response.
Returns a properly typed CreateSessionResponse struct.
## Examples
{:ok, response} = Tinkex.API.Session.create_typed(request, config: config)
response.session_id # => "session-abc-123"
"""
@spec create_typed(map(), keyword()) ::
{:ok, CreateSessionResponse.t()} | {:error, Tinkex.Error.t()}
def create_typed(request, opts) do
case create(request, opts) do
{:ok, json} ->
{:ok, CreateSessionResponse.from_json(json)}
{:error, _} = error ->
error
end
end
@doc """
Send heartbeat to keep session alive.
"""
@spec heartbeat(map(), keyword()) ::
{:ok, map()} | {:error, Tinkex.Error.t()}
def heartbeat(request, opts) do
client = Tinkex.API.client_module(opts)
opts =
opts
|> Keyword.put(:pool_type, :session)
|> Keyword.put(:timeout, 10_000)
|> Keyword.put(:max_retries, 0)
client.post("/api/v1/session_heartbeat", request, opts)
end
end