ExAzureCore.Http.Streaming.StreamHandler (ex_azure_core v0.2.0)

Copy Markdown

Utilities for handling streaming HTTP responses.

Provides helper functions for streaming large responses like blob downloads, event streams, and other streaming Azure APIs.

Streaming to File

StreamHandler.download_to_file(client, url, "/path/to/file.bin")

Streaming with Callback

StreamHandler.stream_with_callback(client, url, fn chunk, acc ->
  # Process each chunk
  {:cont, acc + byte_size(chunk)}
end, 0)

Streaming to Process

StreamHandler.stream_to_self(client, url)
receive do
  {:data, chunk} -> process(chunk)
end

Summary

Functions

Downloads a response body directly to a file.

Streams response chunks to the calling process.

Creates a request struct configured for streaming.

Functions

download_to_file(client, url, file_path, opts \\ [])

@spec download_to_file(
  ExAzureCore.Http.Client.client(),
  String.t(),
  Path.t(),
  keyword()
) ::
  {:ok, map()} | {:error, term()}

Downloads a response body directly to a file.

Options

  • :headers - Additional headers for the request
  • :params - Query parameters

Returns

  • {:ok, response} - Response metadata (status, headers)
  • {:error, reason} - Download failed

stream_to_self(client, url, opts \\ [])

@spec stream_to_self(ExAzureCore.Http.Client.client(), String.t(), keyword()) ::
  {:ok, reference()} | {:error, term()}

Streams response chunks to the calling process.

The calling process will receive messages in the format:

  • {:chunk, data} - A chunk of data
  • {:done, response} - Streaming completed

Returns

  • {:ok, ref} - Reference to the streaming request
  • {:error, reason} - Request failed to start

stream_with_callback(client, url, callback, initial_acc, opts \\ [])

@spec stream_with_callback(
  ExAzureCore.Http.Client.client(),
  String.t(),
  (binary(), acc -> {:cont, acc} | {:halt, acc}),
  acc,
  keyword()
) :: {:ok, {map(), acc}} | {:error, term()}
when acc: term()

Streams response with a callback function.

The callback receives each chunk and an accumulator, and should return {:cont, new_acc} to continue or {:halt, final_acc} to stop.

Returns

  • {:ok, {response, final_acc}} - Streaming completed
  • {:error, reason} - Streaming failed

streaming_request(url, opts \\ [])

@spec streaming_request(
  String.t(),
  keyword()
) :: ExAzureCore.Http.Request.t()

Creates a request struct configured for streaming.