Apical.Plugs.Accept (apical v0.3.1)

Copy Markdown View Source

Plug module for validating Accept headers against response content types.

This plug validates that the client's Accept header matches at least one of the content types defined in the OpenAPI schema's response section.

init options

  • [available_types, operation_id]

    where available_types is a list of media type strings from the response content definition, and operation_id is the operation identifier.

Behavior

  • If no Accept header is present, the request passes (RFC 7231: client accepts any)
  • If Accept header is */*, the request passes
  • If any accepted type matches an available type (including wildcards), passes
  • If no match found, raises Apical.Exceptions.NotAcceptableError (406)

Quality factors

Accept headers with quality factors (q=X) are supported:

  • Types with q=0 are excluded from matching
  • Other quality values are used for preference ordering (but any match passes)

Summary

Functions

Checks if any of the accepted types matches any of the available types.

Extracts all content types from all responses of an operation.

Parses an Accept header into a list of {type, subtype, params, quality} tuples.

Functions

any_type_matches?(accepted_types, available_types)

Checks if any of the accepted types matches any of the available types.

Handles wildcards:

  • */* matches anything
  • type/* matches any subtype of that type

extract_response_content_types(operation_pointer, schema)

Extracts all content types from all responses of an operation.

Looks at the responses object and collects content types from all response codes.

make(pointer, schema, operation_id, plug_opts)

@spec make(
  JsonPtr.t(),
  schema :: map(),
  operation_id :: String.t(),
  plug_opts :: keyword()
) ::
  Macro.t() | nil

parse_accept_header(header)

Parses an Accept header into a list of {type, subtype, params, quality} tuples.

Types with q=0 are filtered out as they explicitly reject that type.