Packages

Oneflow SDK that manages authentication and includes several structs for order management

Current section

Files

Jump to
oneflow lib oneflow.ex
Raw

lib/oneflow.ex

defmodule Oneflow do
alias Oneflow.{Config, Http.Request, Http.Authorization}
alias HTTPoison.Response
require Logger
@search_topics [
"cases",
"shipments",
"skus",
"stocks",
"shippingmethods",
"printers",
"devices",
"clients"
]
@client Application.get_env(:oneflow, :client)
def get!(path, params \\ %{}, opts \\ []) do
case get(path, params, opts) do
{:ok, res} -> res
{:error, err} -> raise inspect(err)
end
end
defp get(path, params, opts) do
Request.new(:get, path, params, [], opts)
|> call
end
def post!(path, body \\ %{}, params \\ %{}, opts \\ []) do
case post(path, body, params, opts) do
{:ok, res} -> res
{:error, err} -> raise inspect(err)
end
end
defp post(path, body, params, opts) do
Request.new(:post, path, params, body, opts)
|> call
end
def call(%Request{} = req) do
body = Request.body(req)
url = "#{Config.endpoint()}#{req.path}"
headers =
Application.get_env(:oneflow, :auth_headers)
|> set_auth_headers(req)
|> Kernel.++([{"Content-Type", "application/json"}])
if Config.log?() do
Logger.log(
:info,
"[oneflow] #{req.method} #{String.trim_trailing(req.path, "/")} #{inspect(req.params)}"
)
Logger.log(:info, "[oneflow][headers] #{inspect(headers)}")
Logger.log(:info, "[oneflow][url] #{url}")
Logger.log(:info, "[oneflow][body] #{inspect(body)}")
end
with {:ok, %Response{body: body, status_code: status_code}} <-
@client.request(req.method, url, body, headers, req.opts),
{:ok, parsed_body} <- Poison.decode(body, keys: :atoms) do
case status_code do
code when code in [200, 201] -> {:ok, parsed_body}
_ -> {:error, parsed_body}
end
end
end
def search(query, topic \\ "shipments", filters \\ []) when topic in @search_topics do
body = %{query: query, facetFilters: filters}
post!("/search/facet/#{topic}", %{}, body)
end
def submit_order(order) do
body = order
post!("/order", body, %{})
end
defp set_auth_headers(nil, req) do
timestamp = :os.system_time(:seconds)
[
{"x-oneflow-date", timestamp},
{"x-oneflow-authorization", Authorization.header_value(req, timestamp)}
]
end
defp set_auth_headers(headers, _req), do: headers
end