Dayron v0.1.1 Dayron.Repo
Defines a rest repository.
A repository maps to an underlying http client, which send requests to a remote server. Currently the only available client is HTTPoison with hackney.
When used, the repository expects the :otp_app
as option.
The :otp_app
should point to an OTP application that has
the repository configuration. For example, the repository:
defmodule MyApp.RestRepo do
use Dayron.Repo, otp_app: :my_app
end
Could be configured with:
config :my_app, MyApp.RestRepo,
url: "https://api.example.com",
headers: [access_token: "token"]
The available configuration is:
:url
- an URL that specifies the server api address:adapter
- a module implementing Dayron.Adapter behaviour, default is HTTPoisonAdapter:headers
- a keywords list with values to be sent on each request header
URLs also support {:system, "KEY"}
to be given, telling Dayron to load
the configuration from the system environment instead:
config :my_app, MyApp.RestRepo,
url: {:system, "API_URL"}
Summary
Functions
Fetches a list of models from the external api, building the request url based on the given model
Deletes a resource given a model and id
Similar to delete/3
but raises:
Dayron.NoResultsError
- if server responds with 404 resource not found.Dayron.ValidationError
- if server responds with 422 unprocessable entity
Fetches a single model from the external api, building the request url based on the given model and id
Similar to get/3
but raises Dayron.NoResultsError
if no resource is
returned in the server response
Inserts a model given a map with resource attributes
Similar to insert/3
but raises a Dayron.ValidationError
if server
responds with a 422 unprocessable entity
Updates a model given an id and a map with resource attributes
Similar to insert/4
but raises:
Dayron.NoResultsError
- if server responds with 404 resource not found.Dayron.ValidationError
- if server responds with 422 unprocessable entity
Functions
Fetches a list of models from the external api, building the request url based on the given model.
Returns an empty list if no result was found or server reponds with an error. Returns a list of model structs if response is valid.
Options are sent directly to the selected adapter. See Dayron.Adapter.get/3
for avaliable options.
Possible Exceptions
Dayron.ServerError
- if server responds with a 500 internal error.Dayron.ClientError
- for any error detected in client side, such as timeout or connection errors.
Deletes a resource given a model and id.
It returns {:ok, model}
if the resource has been successfully
deleted or {:error, error}
if there was a validation
or a known constraint error.
Options are sent directly to the selected adapter.
See Dayron.Adapter.delete/3
for avaliable options.
Possible Exceptions
Dayron.ServerError
- if server responds with a 500 internal error.Dayron.ClientError
- for any error detected in client side, such as timeout or connection errors.
Similar to delete/3
but raises:
Dayron.NoResultsError
- if server responds with 404 resource not found.Dayron.ValidationError
- if server responds with 422 unprocessable entity.
Fetches a single model from the external api, building the request url based on the given model and id.
Returns nil
if no result was found or server reponds with an error.
Returns a model struct with response values if valid.
Options are sent directly to the selected adapter. See Dayron.Adapter.get/3
for avaliable options.
Possible Exceptions
Dayron.ServerError
- if server responds with a 500 internal error.Dayron.ClientError
- for any error detected in client side, such as timeout or connection errors.
Similar to get/3
but raises Dayron.NoResultsError
if no resource is
returned in the server response.
Inserts a model given a map with resource attributes.
Options are sent directly to the selected adapter. See Dayron.Adapter.insert/3 for avaliable options.
Possible Exceptions
Dayron.ServerError
- if server responds with a 500 internal error.Dayron.ClientError
- for any error detected in client side, such as timeout or connection errors.
Example
case RestRepo.insert User, %{name: "Dayse"} do
{:ok, model} -> # Inserted with success
{:error, error} -> # Something went wrong
end
Similar to insert/3
but raises a Dayron.ValidationError
if server
responds with a 422 unprocessable entity.
Updates a model given an id and a map with resource attributes.
Options are sent directly to the selected adapter. See Dayron.Adapter.insert/3 for avaliable options.
Possible Exceptions
Dayron.ServerError
- if server responds with a 500 internal error.Dayron.ClientError
- for any error detected in client side, such as timeout or connection errors.
Example
case RestRepo.update User, "user-id", %{name: "Dayse"} do
{:ok, model} -> # Updated with success
{:error, error} -> # Something went wrong
end
Similar to insert/4
but raises:
Dayron.NoResultsError
- if server responds with 404 resource not found.Dayron.ValidationError
- if server responds with 422 unprocessable entity.