View Source Cosmox.RestClient (Cosmox v0.1.1)

Set of helpers to manage request and response management while interfacing with CosmosDB REST API.

Link to this section Summary

Functions

Decodes the response body, trying to parse it to the given struct.

Gets the error message when, while creating an object, there is a naming conflict.

Gets the error message obtained when the object was not found through the API.

Hides the response from the caller, preventing it from being leaked outside the scope of the API.

Perform the request to the CosmosDB REST API. Returns a common error that resumes both the client call and the deserialisation error.

Decodes the response body, trying to parse it to the given struct.

Link to this section Types

@type body() :: iodata() | {:stream, Enumerable.t()} | nil
@type headers() :: Mint.Types.headers()
@type method() :: :get | :post | :put | :patch | :delete
@type url() :: String.t() | URI.t()

Link to this section Functions

Link to this function

decode_response(map, struct, opts \\ [])

View Source
@spec decode_response(response :: Finch.Response.t(), struct :: term(), list()) ::
  {:ok, term()} | {:error, Cosmox.Response.ErrorMessage.t()}

Decodes the response body, trying to parse it to the given struct.

List of options available through the opts parameter:

  • :error_handler: a function that accepts a status code and the body in a binary string form, and returns the type ErrorMessage.t()
Link to this function

get_error_naming_conflict()

View Source
@spec get_error_naming_conflict() :: binary()

Gets the error message when, while creating an object, there is a naming conflict.

Link to this function

get_error_not_found_message()

View Source
@spec get_error_not_found_message() :: binary()

Gets the error message obtained when the object was not found through the API.

Link to this function

handle_response_error(status, body)

View Source
@spec handle_response_error(status :: non_neg_integer(), body :: binary()) ::
  {:error, Cosmox.Response.ErrorMessage.t()}
Link to this function

hide_response(error_response)

View Source
@spec hide_response(
  response ::
    {:ok, Finch.Response.t()} | {:error, Cosmox.Response.ErrorMessage.t()}
) ::
  :ok | {:error, Cosmox.Response.ErrorMessage.t()}

Hides the response from the caller, preventing it from being leaked outside the scope of the API.

Link to this function

perform_request(method, url, headers, body \\ nil)

View Source
@spec perform_request(
  method :: method(),
  url :: url(),
  headers :: headers(),
  body :: body() | binary() | nil
) :: {:ok, Finch.Response.t()} | {:error, Cosmox.Response.ErrorMessage.t()}

Perform the request to the CosmosDB REST API. Returns a common error that resumes both the client call and the deserialisation error.

Link to this function

try_decode_response(response, struct, opts \\ [])

View Source
@spec try_decode_response(
  response ::
    {:ok, Finch.Response.t()} | {:error, Cosmox.Response.ErrorMessage.t()},
  struct :: term(),
  opts :: Keyword.t()
) :: {:ok, term()} | {:error, Cosmox.Response.ErrorMessage.t()}

Decodes the response body, trying to parse it to the given struct.

For the list of options for the opts keyword, refer to the method decode_response/3