MPP.Client.Transport.WebSocket (mpp v0.14.0)

Copy Markdown View Source

WebSocket implementation of MPP.Client.Transport.

Operates on decoded MPP WebSocket frame maps (the same JSON objects MPP.Transports.WebSocket produces). Payment required is a challenge frame; credentials are attached as a credential frame whose payload is Payment <base64url> — matching mpp-rs client::ws::WsTransport (refs/mpp-rs/src/client/ws.rs).

Reconnect / payment-retry policy lives in MPP.Client.Transport.WebSocket.Retry. A dropped socket after a credential was sent but before the receipt is fatal: it must not pay again.

JSON-RPC -32042 envelopes are also recognized so a caller that unwraps a message frame can reuse this transport for per-call payment-required.

API Functions

FunctionArityDescriptionParam Kinds
error?1Return true if the frame is a server protocol error.response: value
receipt?1Return true if the frame is a server receipt.response: value
set_credential2Replace the request with a WS credential frame (Payment <base64url>).request: value, credential: value
get_challenges1Parse Payment challenges from a challenge frame or JSON-RPC payment-required message.response: value
payment_required?1Return true if the frame or JSON-RPC envelope signals payment required.response: value

Summary

Functions

Return true if the frame is a server protocol error.

Parse Payment challenges from a challenge frame or JSON-RPC payment-required message.

Return true if the frame or JSON-RPC envelope signals payment required.

Return true if the frame is a server receipt.

Replace the request with a WS credential frame (Payment <base64url>).

Functions

error?(arg1)

@spec error?(term()) :: boolean()

Return true if the frame is a server protocol error.

Parameters

  • response - Decoded WS frame (value)

Returns

true for an error frame (boolean)

# descripex:contract
%{
  params: %{response: %{description: "Decoded WS frame", kind: :value}},
  returns: %{type: :boolean, description: "true for an `error` frame"}
}

get_challenges(response)

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

Parse Payment challenges from a challenge frame or JSON-RPC payment-required message.

Parameters

  • response - Decoded WS frame or JSON-RPC response (value)

Returns

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

Errors

  • :malformed_envelope
  • :no_challenges
  • :invalid_challenge
# descripex:contract
%{
  params: %{
    response: %{
      description: "Decoded WS frame or JSON-RPC response",
      kind: :value
    }
  },
  errors: [:malformed_envelope, :no_challenges, :invalid_challenge],
  returns: %{
    type: :tagged_tuple,
    description: "`{:ok, [challenge]}` on success, `{:error, reason}` otherwise"
  }
}

payment_required?(response)

@spec payment_required?(term()) :: boolean()
@spec payment_required?(term()) :: false

Return true if the frame or JSON-RPC envelope signals payment required.

Parameters

  • response - Decoded WS frame or JSON-RPC response (value)

Returns

true for a challenge frame or -32042 (boolean)

# descripex:contract
%{
  params: %{
    response: %{
      description: "Decoded WS frame or JSON-RPC response",
      kind: :value
    }
  },
  returns: %{
    type: :boolean,
    description: "true for a `challenge` frame or `-32042`"
  }
}

receipt?(arg1)

@spec receipt?(term()) :: boolean()

Return true if the frame is a server receipt.

Parameters

  • response - Decoded WS frame (value)

Returns

true for a receipt frame (boolean)

# descripex:contract
%{
  params: %{response: %{description: "Decoded WS frame", kind: :value}},
  returns: %{type: :boolean, description: "true for a `receipt` frame"}
}

set_credential(request, credential)

@spec set_credential(term(), MPP.Credential.t()) :: map()

Replace the request with a WS credential frame (Payment <base64url>).

Parameters

  • request - Prior request or frame; discarded, matching mpp-rs WsTransport (value)
  • credential - MPP.Credential struct (value)

Returns

Credential frame (map)

# descripex:contract
%{
  params: %{
    request: %{
      description: "Prior request or frame; discarded, matching mpp-rs WsTransport",
      kind: :value
    },
    credential: %{description: "MPP.Credential struct", kind: :value}
  },
  returns: %{type: :map, description: "Credential frame"}
}