Unicom360.Webhook.BodyReader (Unicom360 v0.1.0)

Copy Markdown View Source

A Plug.Parsers body reader that keeps the raw body in conn.assigns.raw_body.

Webhook signatures cover the exact bytes received, which Plug.Parsers consumes. Tell it to read through this module:

plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  json_decoder: Jason,
  body_reader: {Unicom360.Webhook.BodyReader, :read_body, []}

Then conn.assigns.raw_body holds the bytes for Unicom360.Webhook.construct_event/4, and Unicom360.Webhook.Plug finds them there by itself.

This keeps a copy of every parsed body in memory for the length of its request. To limit that to the webhook route, mount a separate Plug.Parsers (or Unicom360.Webhook.Plug alone) ahead of the general one.

Summary

Functions

Reads like Plug.Conn.read_body/2, appending what it read to conn.assigns.raw_body.

Functions

read_body(conn, opts)

@spec read_body(Plug.Conn.t(), keyword()) ::
  {:ok, binary(), Plug.Conn.t()}
  | {:more, binary(), Plug.Conn.t()}
  | {:error, term()}

Reads like Plug.Conn.read_body/2, appending what it read to conn.assigns.raw_body.