MPP.Client.Transport.HTTP (mpp v0.6.1)

Copy Markdown View Source

HTTP implementation of MPP.Client.Transport over Req.

Operates on Req.Response / Req.Request structs — this module does not construct Req clients itself. Callers (e.g. the payment-aware Req plugin) feed responses in and receive modified requests out.

Wire format

  • Payment-required response: HTTP status 402
  • Challenges: one or more WWW-Authenticate headers carrying the Payment scheme. Multiple challenges may appear as repeated header values or as a single comma-separated header value; both forms are handled.
  • Credential attachment: Authorization: Payment <base64url-json>, produced via MPP.Headers.format_credential/1.

API Functions

FunctionArityDescriptionParam Kinds
set_credential2Attach a credential to a Req.Request as Authorization: Payment <...>.request: value, credential: value
get_challenges1Parse the Payment challenges from a 402 response's WWW-Authenticate headers.response: value
payment_required?1Return true if the HTTP response is a 402 Payment Required.response: value

Summary

Functions

Parse the Payment challenges from a 402 response's WWW-Authenticate headers.

Return true if the HTTP response is a 402 Payment Required.

Attach a credential to a Req.Request as Authorization: Payment <...>.

Functions

get_challenges(response)

@spec get_challenges(Req.Response.t()) ::
  {:ok, [MPP.Challenge.t()]} | {:error, term()}

Parse the Payment challenges from a 402 response's WWW-Authenticate headers.

Parameters

  • response - Req.Response struct (value)

Returns

{:ok, [challenge]} on success, {:error, reason} otherwise (tagged_tuple)

Errors

  • :no_payment_challenges
  • :missing_www_authenticate
  • :invalid_scheme
  • :missing_required_params
  • :duplicate_param
  • :invalid_auth_params
# descripex:contract
%{
  params: %{response: %{description: "Req.Response struct", kind: :value}},
  errors: [:no_payment_challenges, :missing_www_authenticate, :invalid_scheme,
   :missing_required_params, :duplicate_param, :invalid_auth_params],
  returns: %{
    type: :tagged_tuple,
    description: "`{:ok, [challenge]}` on success, `{:error, reason}` otherwise"
  }
}

payment_required?(response)

@spec payment_required?(Req.Response.t()) :: boolean()
@spec payment_required?(Req.Response.t()) :: false

Return true if the HTTP response is a 402 Payment Required.

Parameters

  • response - Req.Response struct (value)

Returns

true if status is 402 (boolean)

# descripex:contract
%{
  params: %{response: %{description: "Req.Response struct", kind: :value}},
  returns: %{type: :boolean, description: "true if status is 402"}
}

set_credential(request, credential)

@spec set_credential(Req.Request.t(), MPP.Credential.t()) :: Req.Request.t()

Attach a credential to a Req.Request as Authorization: Payment <...>.

Parameters

  • request - Req.Request struct (value)
  • credential - MPP.Credential struct (value)

Returns

Req.Request with the Authorization header set (struct)

# descripex:contract
%{
  params: %{
    request: %{description: "Req.Request struct", kind: :value},
    credential: %{description: "MPP.Credential struct", kind: :value}
  },
  returns: %{
    type: :struct,
    description: "Req.Request with the Authorization header set"
  }
}