HuggingfaceClient.Hub.Client (huggingface_client v0.1.0)

Copy Markdown View Source

Core HTTP client for HuggingFace Hub API interactions.

Provides authenticated GET, POST, PATCH, PUT, DELETE with:

  • Bearer token authentication (with env-var fallback)
  • JSON encoding/decoding via Req's built-in middleware
  • Pagination via Link headers
  • Consistent HubApiError on failures
  • Private Hub endpoint support

Configuration

config :huggingface_client,
  hub_url: "https://my-enterprise-hub.co",
  default_token: "hf_xxx"

Environment variable fallback (in order)

HF_TOKEN, HUGGINGFACE_TOKEN, HUGGING_FACE_HUB_TOKEN

Summary

Functions

auth_headers(token)

@spec auth_headers(String.t() | nil | false) :: list()

Builds Authorization header list from a token (or nil/false).

build_headers(token, opts, extras)

default_token()

@spec default_token() :: String.t() | nil

delete(url, opts)

delete(url, token, opts \\ [])

@spec delete(String.t(), String.t() | nil | false, keyword()) ::
  :ok | {:error, Exception.t()}

get(url, opts)

get(url, token, opts \\ [])

@spec get(String.t(), String.t() | nil | false, keyword()) ::
  {:ok, term()} | {:error, Exception.t()}

hub_url()

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

paginated_stream(url, opts)

paginated_stream(url, token, opts \\ [])

@spec paginated_stream(String.t(), String.t() | nil | false, keyword()) ::
  Enumerable.t()

Returns a lazy stream across paginated API responses, following Link headers.

patch(url, body, opts)

patch(url, body, token, opts \\ [])

@spec patch(String.t(), map(), String.t() | nil | false, keyword()) ::
  {:ok, term()} | {:error, Exception.t()}

post(url, body, opts)

post(url, body, token, opts \\ [])

@spec post(String.t(), map() | list() | binary(), String.t() | nil | false, keyword()) ::
  {:ok, term()} | {:error, Exception.t()}

put(url, body, opts)

put(url, body, token, opts \\ [])

@spec put(String.t(), map() | binary(), String.t() | nil | false, keyword()) ::
  {:ok, term()} | {:error, Exception.t()}

stream_raw(url, token, opts \\ [])

@spec stream_raw(String.t(), String.t() | nil | false, keyword()) :: Enumerable.t()

Returns a raw binary chunk stream from an SSE endpoint. Pipe into HuggingfaceClient.Inference.SSE.parse_stream/1 to decode events.