JsonApiClient v0.5.2 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 an id to the request

Specify which relationships to include

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

Associate a resource with this request

Specify the sort param for the request

Link to this section Functions

Link to this function fields(req, fields_to_add) View Source

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

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"

Add an id to the request.

Link to this function include(req, relationship_list) View Source

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 resource with this request

Specify the sort param for the request.