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
http_methods() :: :get | :post | :update | :delete | :put | :head | :options | :connect | :trace | :patch
param_enum() :: [{name(), param_value()}] | %{optional(name()) => param_value()}
param_value() :: atom() | binary() | number() | param_enum() | list()
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
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")
filter(req :: JsonApiClient.Request.t(), filter :: param_value()) :: JsonApiClient.Request.t()
Specify the filter param for the request.
Retruns the HTTP body of the request
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"
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")
id(req :: JsonApiClient.Request.t(), id :: binary()) :: JsonApiClient.Request.t()
Add an id to the request.
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"])
method(req :: JsonApiClient.Request.t(), method :: atom()) :: JsonApiClient.Request.t()
Specify the HTTP method for the request.
Create a request
new(base_url :: String.t()) :: JsonApiClient.Request.t()
Create a request with the given base URL
page(req :: JsonApiClient.Request.t(), page :: param_value()) :: JsonApiClient.Request.t()
Specify the page param for the request.
params(req :: JsonApiClient.Request.t(), list :: param_enum()) :: JsonApiClient.Request.t()
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"
path(req :: JsonApiClient.Request.t(), JsonApiClient.Resource.t() | name()) :: JsonApiClient.Request.t()
Associate a path with this request
resource( req :: JsonApiClient.Request.t(), resource :: JsonApiClient.Resource.t() ) :: JsonApiClient.Request.t()
Associate a resource with this request
service_name(req :: JsonApiClient.Request.t(), service_name :: atom()) :: JsonApiClient.Request.t()
Associate a service_name with this request
sort(req :: JsonApiClient.Request.t(), sort :: param_value()) :: JsonApiClient.Request.t()
Specify the sort param for the request.