TerminusDB.Client (terminusdb_ex v0.1.0)

Copy Markdown View Source

The HTTP wire module for terminusdb_ex.

TerminusDB.Client is the only module that issues HTTP requests. Every higher-level API module (TerminusDB.Database, TerminusDB.Document, …) composes a request and delegates here. Centralizing the wire logic keeps auth, headers, JSON, telemetry, retries, and error mapping in one place.

Built on Req. Connection context is carried by an immutable TerminusDB.Config struct.

Functions

Options

In addition to the telemetry-only :area and :raw flags, request options are forwarded to Req: :json (JSON body), :body (raw body), :params (query string), :into (response streaming target), :form, :form_multipart, :decode_body.

Examples

# Using the Database API (preferred)
{:ok, body} = TerminusDB.Database.create(config, "mydb", label: "My DB")

# Using the raw client directly
{:ok, body} =
  TerminusDB.Client.request(config, :post, "db/admin/mydb",
    json: %{label: "My DB", comment: "demo", schema: true},
    area: :database
  )

Summary

Functions

Performs an HTTP request and returns {:ok, decoded_body} or {:error, Error.t()}.

Performs an HTTP request and returns the decoded body, or raises TerminusDB.Error.

Performs an HTTP request and returns {:ok, Req.Response.t()} with the full response (status, headers, body). Use this when you need headers or a streamed body (:into).

Convenience that builds the common organization/database resource segment.

Types

method()

@type method() :: :get | :post | :put | :patch | :delete | :head

Functions

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

@spec request(TerminusDB.Config.t(), method(), String.t(), keyword()) ::
  {:ok, term()} | {:error, TerminusDB.Error.t()}

Performs an HTTP request and returns {:ok, decoded_body} or {:error, Error.t()}.

The body is auto-decoded by Req when the response is JSON (string keys). For non-2xx responses, an TerminusDB.Error is built from the structured api:* body when present, or a generic :http error otherwise.

Options

See the module documentation. :area sets the telemetry event area (default :connection). :raw returns the full Req.Response.t() instead of the body.

request!(config, method, path, opts \\ [])

@spec request!(TerminusDB.Config.t(), method(), String.t(), keyword()) :: term()

Performs an HTTP request and returns the decoded body, or raises TerminusDB.Error.

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req ->
...>     {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})}
...>   end
...> )
iex> TerminusDB.Client.request!(config, :get, "ok")
%{"api:status" => "api:success"}

request_response(config, method, path, opts \\ [])

@spec request_response(TerminusDB.Config.t(), method(), String.t(), keyword()) ::
  {:ok, Req.Response.t()} | {:error, TerminusDB.Error.t()}

Performs an HTTP request and returns {:ok, Req.Response.t()} with the full response (status, headers, body). Use this when you need headers or a streamed body (:into).

resource_path(org, db)

@spec resource_path(String.t(), String.t()) :: String.t()

Convenience that builds the common organization/database resource segment.

iex> TerminusDB.Client.resource_path("admin", "mydb")
"admin/mydb"