HfHub.HTTP (HfHub v0.2.0)

Copy Markdown View Source

HTTP client for HuggingFace Hub API.

Provides low-level HTTP request functionality with authentication, rate limiting, and error handling.

Summary

Functions

Performs a DELETE request.

Downloads a file from a URL with streaming support.

Makes a GET request to the HuggingFace Hub API.

Makes a paginated GET request and collects all pages.

Makes a HEAD request to fetch headers without body.

Performs a PATCH request with JSON body.

Makes a POST request to the HuggingFace Hub API.

Performs a POST request expecting no response body.

Performs a PUT request with JSON body.

Functions

delete(path, opts \\ [])

@spec delete(
  String.t(),
  keyword()
) :: :ok | {:ok, map()} | {:error, term()}

Performs a DELETE request.

DELETE requests typically don't have a body but may return data.

download_file(url, destination, opts \\ [])

@spec download_file(String.t(), Path.t(), keyword()) :: :ok | {:error, term()}

Downloads a file from a URL with streaming support.

Arguments

  • url - Full URL to download
  • destination - Local file path
  • opts - Download options

Options

  • :token - Authentication token
  • :resume - Resume interrupted download. Defaults to false.
  • :progress_callback - Function called with download progress. The callback receives (bytes_downloaded, total_bytes) where total_bytes may be nil if the server doesn't provide Content-Length.

Examples

:ok = HfHub.HTTP.download_file(
  "https://huggingface.co/bert-base-uncased/resolve/main/config.json",
  "/tmp/config.json"
)

# With progress tracking
:ok = HfHub.HTTP.download_file(
  "https://huggingface.co/bert-base-uncased/resolve/main/model.bin",
  "/tmp/model.bin",
  progress_callback: fn downloaded, total ->
    if total, do: IO.puts("#{round(downloaded / total * 100)}%")
  end
)

get(path, opts \\ [])

@spec get(
  String.t(),
  keyword()
) :: {:ok, map() | [map()]} | {:error, term()}

Makes a GET request to the HuggingFace Hub API.

Arguments

  • path - API path (e.g., "/api/models/bert-base-uncased")
  • opts - Request options

Options

  • :token - Authentication token
  • :headers - Additional headers
  • :params - Query parameters

Examples

{:ok, response} = HfHub.HTTP.get("/api/models/bert-base-uncased")

get_paginated(path, opts \\ [])

@spec get_paginated(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Makes a paginated GET request and collects all pages.

Pagination follows the Link header with rel="next".

Options

  • :token - Authentication token
  • :headers - Additional headers
  • :params - Query parameters

head(url, opts \\ [])

@spec head(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Makes a HEAD request to fetch headers without body.

Used for ETag-based cache validation.

Arguments

  • url - Full URL to request
  • opts - Request options

Options

  • :headers - Request headers
  • :follow_redirects - Whether to follow redirects. Defaults to true.

Examples

{:ok, response} = HfHub.HTTP.head("https://huggingface.co/model/file")

patch(path, body \\ nil, opts \\ [])

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

Performs a PATCH request with JSON body.

post(path, body \\ nil, opts \\ [])

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

Makes a POST request to the HuggingFace Hub API.

Arguments

  • path - API path
  • body - Request body (will be JSON-encoded)
  • opts - Request options

Examples

{:ok, response} = HfHub.HTTP.post("/api/endpoint", %{data: "value"})

post_action(path, body \\ nil, opts \\ [])

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

Performs a POST request expecting no response body.

Used for actions that return 200/204 with no content.

put(path, body \\ nil, opts \\ [])

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

Performs a PUT request with JSON body.