Reqord.ContentAnalyzer (reqord v0.4.0)

View Source

Analyzes HTTP response content to determine optimal storage strategy.

This module provides utilities for:

  • Detecting binary vs text content
  • Determining appropriate encoding strategies
  • Identifying streaming responses

Summary

Functions

Analyzes response content and returns encoding recommendation.

Extracts content-type from headers map or list.

Determines if content should be stored externally based on size and type.

Functions

analyze_content(content_type, content)

@spec analyze_content(String.t() | nil, binary()) ::
  {:text | :binary | :stream, binary()}

Analyzes response content and returns encoding recommendation.

Returns

  • {:text, content} - Text content, use Base64 encoding
  • {:binary, content} - Binary content, consider external storage
  • {:stream, content} - Streaming content, requires special handling

Examples

iex> Reqord.ContentAnalyzer.analyze_content("application/json", ~s({"key": "value"}))
{:text, ~s({"key": "value"})}

iex> Reqord.ContentAnalyzer.analyze_content("image/png", <<137, 80, 78, 71>>)
{:binary, <<137, 80, 78, 71>>}

iex> Reqord.ContentAnalyzer.analyze_content("text/event-stream", "data: chunk1\n\n")
{:stream, "data: chunk1\n\n"}

extract_content_type(headers)

@spec extract_content_type(map() | list()) :: String.t() | nil

Extracts content-type from headers map or list.

Examples

iex> Reqord.ContentAnalyzer.extract_content_type(%{"content-type" => "application/json"})
"application/json"

iex> Reqord.ContentAnalyzer.extract_content_type([{"Content-Type", "image/png; charset=utf-8"}])
"image/png"

should_store_externally?(content_type, size)

@spec should_store_externally?(atom(), non_neg_integer()) :: boolean()

Determines if content should be stored externally based on size and type.

Examples

iex> Reqord.ContentAnalyzer.should_store_externally?(:binary, 2_000_000)
true

iex> Reqord.ContentAnalyzer.should_store_externally?(:text, 500_000)
false