UnifiApi.Network.Networks (UnifiApi v0.4.0)

Copy Markdown View Source

UniFi Network API — network configuration.

Create, read, update, and delete network configurations (VLANs, subnets, etc.) on a site.

Summary

Functions

Creates a new network on a site.

Gets a specific network by ID.

Lists all networks on a site.

Returns a lazy stream that auto-paginates through all networks on a site.

Updates an existing network.

Functions

create(client, site_id, body)

@spec create(Req.Request.t(), String.t(), map()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Creates a new network on a site.

Examples

{:ok, network} = UnifiApi.Network.Networks.create(client, site_id, %{
  name: "Guest VLAN",
  vlanId: 100
})

delete(client, site_id, network_id, opts \\ [])

@spec delete(Req.Request.t(), String.t(), String.t(), keyword()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Deletes a network.

Examples

{:ok, _} = UnifiApi.Network.Networks.delete(client, site_id, network_id)

get(client, site_id, network_id)

@spec get(Req.Request.t(), String.t(), String.t()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Gets a specific network by ID.

Examples

{:ok, network} = UnifiApi.Network.Networks.get(client, site_id, network_id)
network["name"]   # => "LAN"
network["vlanId"] # => 1

list(client, site_id, opts \\ [])

@spec list(Req.Request.t(), String.t(), keyword()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Lists all networks on a site.

Options

Supports pagination: :offset, :limit, :filter.

Examples

{:ok, networks} = UnifiApi.Network.Networks.list(client, site_id)

stream(client, site_id, opts \\ [])

@spec stream(Req.Request.t(), String.t(), keyword()) :: Enumerable.t()

Returns a lazy stream that auto-paginates through all networks on a site.

Error contract

A mid-stream error does not raise by default: the stream halts and yields {:error, %UnifiApi.StreamError{}, last_offset} as its final element, so the enumerable is heterogeneous. Match the tail:

case Enum.to_list(stream) do
  items when is_list(items) ->
    case List.last(items) do
      {:error, error, cursor} -> {:error, error, cursor}
      _ -> {:ok, items}
    end
end

Pass raise_errors: true to raise UnifiApi.StreamError instead.

Options

  • :limit — items per page (default: 200)
  • :filter — UniFi filter expression
  • :max_pages — halt after this many successful pages (default: unbounded).
  • :max_items — halt once this many items have been yielded (default: unbounded).
  • :raise_errors — raise UnifiApi.StreamError on error instead of yielding the error tuple (default: false).

Examples

UnifiApi.Network.Networks.stream(client, site_id, raise_errors: true)
|> Enum.map(& &1["name"])

update(client, site_id, network_id, body)

@spec update(Req.Request.t(), String.t(), String.t(), map()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Updates an existing network.

Examples

{:ok, _} = UnifiApi.Network.Networks.update(client, site_id, network_id, %{
  name: "Updated Network Name"
})