ReqFly.Steps (req_fly v1.0.0)

View Source

Custom Req pipeline steps for Fly.io API integration.

This module provides request and response processing steps that integrate with the Req HTTP client pipeline to handle Fly.io-specific requirements like authentication, error handling, and telemetry.

Pipeline Steps

Summary

Functions

Attaches the Authorization Bearer header to the request.

Attaches the base URL to the request from the :fly_base_url option.

Attaches standard headers required for Fly.io API requests.

Attaches telemetry to emit events during request lifecycle.

Handles error responses by converting non-2xx responses to ReqFly.Error.

Functions

attach_auth(request)

@spec attach_auth(Req.Request.t()) :: Req.Request.t()

Attaches the Authorization Bearer header to the request.

Reads the token from the :fly_token option on the request.

Examples

iex> request = %Req.Request{options: %{fly_token: "secret"}}
iex> request = ReqFly.Steps.attach_auth(request)
iex> request.headers["authorization"]
["Bearer secret"]

attach_base_url(request)

@spec attach_base_url(Req.Request.t()) :: Req.Request.t()

Attaches the base URL to the request from the :fly_base_url option.

Examples

iex> request = %Req.Request{options: %{fly_base_url: "https://api.machines.dev/v1"}}
iex> request = ReqFly.Steps.attach_base_url(request)
iex> request.options.base_url
"https://api.machines.dev/v1"

attach_headers(request)

@spec attach_headers(Req.Request.t()) :: Req.Request.t()

Attaches standard headers required for Fly.io API requests.

Adds:

  • User-Agent: "req_fly/0.1.0 (+Req)"
  • Accept: "application/json"

Examples

iex> request = %Req.Request{}
iex> request = ReqFly.Steps.attach_headers(request)
iex> request.headers["user-agent"]
["req_fly/0.1.0 (+Req)"]

attach_telemetry(request)

@spec attach_telemetry(Req.Request.t()) :: Req.Request.t()

Attaches telemetry to emit events during request lifecycle.

Emits the following telemetry events:

  • [:req_fly, :request, :start] - When request starts
  • [:req_fly, :request, :stop] - When request completes successfully
  • [:req_fly, :request, :exception] - When request fails

The telemetry prefix can be customized using the :fly_telemetry_prefix option.

Examples

iex> request = %Req.Request{options: %{fly_telemetry_prefix: [:my_app, :fly]}}
iex> request = ReqFly.Steps.attach_telemetry(request)

handle_error_response(arg)

@spec handle_error_response({:ok, Req.Response.t()} | {:error, Exception.t()}) ::
  {:ok, Req.Response.t()} | {:error, ReqFly.Error.t()}

Handles error responses by converting non-2xx responses to ReqFly.Error.

Examples

iex> response = %Req.Response{status: 404, body: %{"error" => "not_found"}}
iex> {:error, error} = ReqFly.Steps.handle_error_response({:ok, response})
iex> error.status
404