HawkEx.CSV.Storage behaviour (hawk_ex v0.1.0)

Copy Markdown View Source

Behaviour and dispatch helpers for CSV export storage adapters.

Storage adapters persist generated CSV content and return a path that can be saved on HawkEx.CSV.Export. The path is adapter-specific: local storage returns a filesystem path, while S3 storage returns an s3://... URI.

HawkEx ships two adapters:

Configuration

# Local disk (default)
config :hawk_ex,
  csv_storage: {HawkEx.CSV.Storage.Local, path: "priv/exports"}

# S3
config :hawk_ex,
  csv_storage: {HawkEx.CSV.Storage.S3,
    bucket: "my-bucket",
    prefix: "exports/"}

Custom adapters

Implement this behaviour to support other storage backends:

defmodule MyApp.CSV.GCSStorage do
  @behaviour HawkEx.CSV.Storage

  @impl true
  def write(filename, content, opts) do
    bucket = Keyword.fetch!(opts, :bucket)
    # write to GCS
    {:ok, "gs://#{bucket}/#{filename}"}
  end

  @impl true
  def read(path, opts) do
    # read from GCS
    {:ok, content}
  end

  @impl true
  def delete(path, opts) do
    # delete from GCS
    :ok
  end
end

Then configure it:

config :hawk_ex,
  csv_storage: {MyApp.CSV.GCSStorage, bucket: "my-bucket"}

Summary

Callbacks

Deletes a stored export.

Reads CSV content from storage.

Writes CSV content to storage.

Functions

Deletes stored CSV content using the configured adapter.

Reads CSV content using the configured adapter.

Writes CSV content using the configured adapter.

Callbacks

delete(path, opts)

@callback delete(path :: String.t(), opts :: keyword()) ::
  :ok | {:error, reason :: term()}

Deletes a stored export.

Returns :ok or {:error, reason}.

read(path, opts)

@callback read(path :: String.t(), opts :: keyword()) ::
  {:ok, content :: String.t()} | {:error, reason :: term()}

Reads CSV content from storage.

Returns {:ok, content} or {:error, reason}.

write(filename, content, opts)

@callback write(filename :: String.t(), content :: String.t(), opts :: keyword()) ::
  {:ok, path :: String.t()} | {:error, reason :: term()}

Writes CSV content to storage.

Returns {:ok, path}, where path is the identifier used to read or delete the export later.

Functions

delete(path)

Deletes stored CSV content using the configured adapter.

read(path)

Reads CSV content using the configured adapter.

write(filename, content)

Writes CSV content using the configured adapter.