Versioning.Controller (Versioning v0.4.1) View Source

A set of functions used with Phoenix controllers.

Typically, this module should be imported into your controller modules. In a normal phoenix application, this can usually be done with the following:

defmodule YourAppWeb do
# ...

  def controller do
    quote do
      use Phoenix.Controller, namespace: MyAppWeb

      # ...

      import Versioning.Controller

      # ...
    end
  end
end

Please see the documentation at Phoenix.Controller for details on how to set up a typical controller.

This module is mainly used to convert raw params from the version that the request data represents, to the latest version that your "context" application expects.

Example

Below is an example of how to use versioning in a typical controller:

defmodule MyController do
  use MyAppWeb, :controller

  plug Versioning.Plug, schema: MyVersioningSchema

  def update(conn, %{"id" => id, "user" => params}) do
    with {:ok, params} <- params_version(conn, params, "User"),
         {:ok, user} <- Blog.fetch_user(id),
         {:ok, user} <- Blog.update_user(user) do
      render(conn, "show.json", user: user)
    end
  end
end

The params_version/3 function accepts a conn, a set of params representing whatever version of the data the user uses, as well as the type of the data. It will run the params through your schema, returning the results.

Link to this section Summary

Functions

Applies a version using the header or fallback.

Fetches the current schema.

Fetches the current schema or errors if empty.

Fetches the current request version.

Fetches the current request version or errors if empty.

Performs versioning on the params using the given type.

Stores the schema for versioning.

Stores the request version.

Link to this section Functions

Link to this function

apply_version(conn, header \\ "x-api-version", fallback \\ :latest)

View Source

Specs

apply_version(Plug.Conn.t(), binary(), :latest | {module(), atom()}) ::
  Plug.Conn.t()

Applies a version using the header or fallback.

The schema must already be stored on the conn to use this function.

The fallback is used if the header is not present. Its value can be :latest, representing the latest version on the schema, or a {module, function}. This module and function will be called with the conn.

Specs

fetch_schema(Plug.Conn.t()) :: {:ok, Versioning.Schema.t()} | :error

Fetches the current schema.

Returns {:ok, schema} on success, or :error if no schema exists.

Examples

iex> conn = Versioning.Controller.put_schema(conn, MySchema)
iex> Versioning.Controller.fetch_schema(conn)
{:ok, MySchema}

Specs

fetch_schema!(Plug.Conn.t()) :: Versioning.Schema.t()

Fetches the current schema or errors if empty.

Returns schema or raises a Versioning.MissingSchemaError.

Examples

iex> conn = Versioning.Controller.put_schema(conn, MySchema)
iex> Versioning.Controller.fetch_schema!(conn)
MySchema

Specs

fetch_version(Plug.Conn.t()) :: {:ok, binary()} | :error

Fetches the current request version.

Returns {:ok, version} on success, or :error if no version exists.

Examples

iex> conn = Versioning.Controller.put_version(conn, "1.0.0")
iex> Versioning.Controller.fetch_version(conn)
{:ok, "1.0.0"}

Specs

fetch_version!(Plug.Conn.t()) :: Versioning.Schema.t()

Fetches the current request version or errors if empty.

Returns version or raises a Versioning.MissingVersionError.

Examples

iex> conn = Versioning.Controller.put_schema(conn, "1.0.0")
iex> Versioning.Controller.fetch_version!(conn)
"1.0.0"
Link to this function

params_version(conn, params, type)

View Source

Specs

params_version(Plug.Conn.t(), map(), binary()) ::
  {:ok, map()} | {:error, :bad_version}

Performs versioning on the params using the given type.

The schema and request version must already be stored on the conn to use this function.

Returns {:ok, params} with the new versioned params, or {:error, :bad_version} if the schema does not contain the version requested.

Link to this function

put_schema(conn, schema)

View Source

Specs

Stores the schema for versioning.

Examples

Versioning.Controller.put_schema(conn, MySchema)
Link to this function

put_version(conn, version)

View Source

Specs

put_version(Plug.Conn.t(), binary()) :: Plug.Conn.t()

Stores the request version.

Examples

Versioning.Controller.put_version(conn, "1.0.0")