PgRest.Negotiate (PgRest v0.2.0)

Copy Markdown View Source

Picks the response media type of a request from its Accept types and what the endpoint can produce, as PostgREST's content negotiation does.

offered/1 lists the kinds every endpoint answers (a table, or a function of any return type, as recorded from PostgREST): JSON, vnd.pgrst.object, vnd.pgrst.array and CSV. application/geo+json is never offered (PostGIS is not supported), so it gets the ordinary PGRST107.

The plan type

application/vnd.pgrst.plan answers with the execution plan of the statement the request would run for the type it is for= (application/json by default): offered only for a for= this library serves (JSON, */*, object, array, CSV), never for a custom media handler (text/xml, application/vnd.twkb).

negotiate/1 is provisional (it fails early when nothing at all is acceptable and never looks at the gate); resolve/5 picks the final media type once the plan is known, with the plan gate and the after_load/2 restrictions applied to the same choice, so a plan type that did not pass the gate cannot be returned. A plan is offered only when:

  • the plug option plan_enabled allows it (the function is called only when a plan type is in Accept);
  • the :explain operation is allowed on every resource the request touches (PgRest.Authorization);
  • options=analyze of something that writes has Prefer: tx=rollback honoured (tx_end: :commit_allow_override).

A request the gate refuses gets the PGRST107 of a plan that is not there, by any of the three reasons and whatever else is wrong with the request, so a denial is no oracle.

Differences from PostgREST

  • PostgREST also serves application/geo+json; here it is always a PGRST107 (no PostGIS).
  • For a request that reads a resource with after_load/2 (a PgRest concept), CSV and nulls=stripped are not offered: the callbacks need decoded JSON rows with their nulls, and skipping them could leak what they redact. A client that also accepts JSON gets JSON; PostgREST would send CSV.
  • options=analyze on a write (or a call that may write) needs Prefer: tx=rollback, since EXPLAIN ANALYZE runs it. PostgREST runs it and commits unless the client asks for a rollback, so Accept: application/vnd.pgrst.plan; options=analyze on POST /t inserts a row there and is a PGRST107 here. Rollback does not undo sequences or effects outside the transaction, and a custom changeset/3 runs for a plan request as for any write.
  • A plan is also refused when the authorization hook denies :explain; PostgREST has no such operation.
  • A plan for= a custom media handler (text/xml on a scalar function, an aggregate such as application/vnd.twkb) is a PGRST107 here; PostgREST explains the handler.

Summary

Functions

The first of accepts (by descending q) whose kind is in offered; */* is JSON and no Accept types at all is JSON. A plan type is chosen only when :plan is offered, its for= type is one of offered and plan_ok? (a function of the plan media type, default always true) agrees. Else the 406 PGRST107 listing the types by canonical name (as PostgREST does), in the sorted order. Option :stripped? (default true) is false to refuse nulls=stripped types.

The provisional media type, before planning: choose/3 over every kind, plan included, so a request nothing can answer fails before anything is planned. It never looks at the plan gate, which resolve/5 applies to the final choice, so the order of the errors does not depend on it.

The media kinds an endpoint answers. Option :plan? (default false) adds :plan.

The final media type, once the plan is known: the one structural step that decides it.

Functions

choose(accepts, offered, opts \\ [])

@spec choose([PgRest.MediaType.t()], [PgRest.MediaType.kind()], keyword()) ::
  {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}

The first of accepts (by descending q) whose kind is in offered; */* is JSON and no Accept types at all is JSON. A plan type is chosen only when :plan is offered, its for= type is one of offered and plan_ok? (a function of the plan media type, default always true) agrees. Else the 406 PGRST107 listing the types by canonical name (as PostgREST does), in the sorted order. Option :stripped? (default true) is false to refuse nulls=stripped types.

negotiate(request)

@spec negotiate(PgRest.Request.t()) ::
  {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}

The provisional media type, before planning: choose/3 over every kind, plan included, so a request nothing can answer fails before anything is planned. It never looks at the plan gate, which resolve/5 applies to the final choice, so the order of the errors does not depend on it.

offered(opts \\ [])

@spec offered(keyword()) :: [PgRest.MediaType.kind()]

The media kinds an endpoint answers. Option :plan? (default false) adds :plan.

resolve(request, plan, conn, context, opts \\ [])

@spec resolve(
  PgRest.Request.t(),
  PgRest.Plan.Read.t() | nil,
  Plug.Conn.t(),
  map(),
  keyword()
) ::
  {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}

The final media type, once the plan is known: the one structural step that decides it.

plan is the plan the response reads (nil for a write without a representation). A plan with an after_load/2 resource (callbacks work on decoded JSON rows) is not answered as CSV or with the nulls stripped, and the plan type is offered only through the gate (see the moduledoc). Both restrictions shape one choose/3, so a client that also accepts JSON falls back to it and anything else gets the PGRST107 listing every type it asked for.

context carries the plan_enabled setting (false, true, or (conn, context -> boolean); only true counts) and the authorization module. Options: :targets (the resources and function :explain is asked on, default Plan.Read.targets(plan)) and :mutating? (default false: a write, or a call that may write).