JsonApiClient v3.0.4 JsonApiClient.Request View Source

Describes a JSON API HTTP Request

Link to this section Summary

Functions

Specify which fields to include

Specify the filter param for the request

Retruns the HTTP body of the request

Get the query parameters for the request

Get the url for the request

Add a a header to the request.”

Add an id to the request

Specify which relationships to include

Specify the HTTP method for the request

Create a request

Create a request with the given base URL

Specify the page param for the request

Add query params to the request

Associate a path with this request

Associate a resource with this request

Associate a service_name with this request

Specify the sort param for the request

Link to this section Types

Link to this type http_methods() View Source
http_methods() ::
  :get
  | :post
  | :update
  | :delete
  | :put
  | :head
  | :options
  | :connect
  | :trace
  | :patch
Link to this type param_enum() View Source
param_enum() :: [{name(), param_value()}] | %{optional(name()) => param_value()}
Link to this type param_value() View Source
param_value() :: atom() | binary() | number() | param_enum() | list()
Link to this type t() View Source
t() :: %JsonApiClient.Request{
  attributes: map(),
  base_url: String.t() | nil,
  headers: map(),
  id: binary() | nil,
  method: http_methods(),
  options: map(),
  params: map() | nil,
  resource: JsonApiClient.Resource.t() | nil,
  service_name: atom() | nil
}

Link to this section Functions

Link to this function fields(req, fields_to_add) View Source
fields(
  req :: JsonApiClient.Request.t(),
  fields_to_add :: [{atom(), name() | [name()]}]
) :: any()

Specify which fields to include

Takes a request and the fields you want to include as a keyword list where the keys are types and the values are a comma separated string or a list of field names.

fields(%Request{}, user: ~(name, email), comment: ~(body))
fields(%Request{}, user: "name,email", comment: "body")

Specify the filter param for the request.

Retruns the HTTP body of the request

Link to this function get_query_params(req) View Source
get_query_params(JsonApiClient.Request.t()) :: [{binary(), binary()}]

Get the query parameters for the request

Retruns an Enumerable suitable for passing to URI.encode_query.

The “params” stored in the Request struct are represented as nested hashed and arrays. This function flattens out the hashes and converts the values for attributes that take lists like incldues and fields and converts them to the comma separated strings that JSON API expects.

Examples

iex> req = new("http://api.net")
iex> req |> fields(type1: [:a, :b, :c]) |> get_query_params
[{"fields[type1]", "a,b,c"}]
iex> req |> params(a: %{b: %{c: "foo"}}) |> get_query_params
[{"a[b][c]", "foo"}]

Get the url for the request

The URL returned does not include the query string

Examples

iex> new("http://api.net") |> id("123") |> get_url
"http://api.net/123"
iex> post = %JsonApiClient.Resource{type: "posts", id: "123"}
iex> new("http://api.net") |> resource(post) |> get_url
"http://api.net/posts/123"
Link to this function header(req, header_name, header_value) View Source
header(
  req :: JsonApiClient.Request.t(),
  header_name :: name(),
  header_value :: String.t()
) :: JsonApiClient.Request.t()

Add a a header to the request.”.

header(%Request{}, "X-My-Header", "My header value")

Add an id to the request.

Link to this function include(req, relationship_list) View Source
include(req :: JsonApiClient.Request.t(), [name()] | name()) ::
  JsonApiClient.Request.t()

Specify which relationships to include

Takes a request and the relationships you want to include. Relationships can be expressed as a string or a list of relationship strings.

include(%Request{}, "coments.author")
include(%Request{}, ["author", "comments.author"])

Specify the HTTP method for the request.

Create a request with the given base URL

Specify the page param for the request.

Add query params to the request.

Will add to existing params when called multiple times with different keys, but individual parameters will be overwritten. Supports nested attributes.

iex> req = new("http://api.net") |> params(a: "foo", b: "bar")
iex> req |> get_query_params |> URI.encode_query
"a=foo&b=bar"

iex> req = new("http://api.net")   \
...> |> params(a: "foo", b: "bar") \
...> |> params(a: "new", c: "baz")
iex> req |> get_query_params |> URI.encode_query
"a=new&b=bar&c=baz"

Associate a path with this request

Associate a resource with this request

Link to this function service_name(req, service_name) View Source
service_name(req :: JsonApiClient.Request.t(), service_name :: atom()) ::
  JsonApiClient.Request.t()

Associate a service_name with this request

Specify the sort param for the request.