Packages

Simple Elixir wrapper for the Stockfighter API

Current section

Files

Jump to
stockastic lib stockastic.ex
Raw

lib/stockastic.ex

defmodule Stockastic do
use HTTPoison.Base
alias Stockastic.Client
@user_agent [{"User-agent", "stockastic"}]
@type response :: {integer, any} | :jsx.json_term
@spec process_response(HTTPoison.Response.t) :: response
def process_response(response) do
status_code = response.status_code
body = response.body
response = unless body == "", do: body |> JSX.decode!,
else: nil
if (status_code == 200), do: response,
else: {status_code, response}
end
def delete(path, client, body \\ "") do
_request(:delete, url(client, path), client.auth, body)
end
def post(path, client, body \\ "") do
_request(:post, url(client, path), client.auth, body)
end
def patch(path, client, body \\ "") do
_request(:patch, url(client, path), client.auth, body)
end
def put(path, client, body \\ "") do
_request(:put, url(client, path), client.auth, body)
end
def get(path, client, params \\ []) do
url = url(client, path)
url = <<url :: binary, build_qs(params) :: binary>>
_request(:get, url, client.auth)
end
def _request(method, url, auth, body \\ "") do
json_request(method, url, body, authorization_header(auth, @user_agent))
end
def json_request(method, url, body \\ "", headers \\ [], options \\ []) do
request!(method, url, JSX.encode!(body), headers, options) |> process_response
end
def raw_request(method, url, body \\ "", headers \\ [], options \\ []) do
request!(method, url, body, headers, options) |> process_response
end
@spec url(client :: Client.t, path :: binary) :: binary
defp url(_client = %Client{endpoint: endpoint}, path) do
endpoint <> path
end
@spec build_qs([{atom, binary}]) :: binary
defp build_qs([]), do: ""
defp build_qs(kvs), do: to_string('?' ++ URI.encode_query(kvs))
def authorization_header(%{access_token: token}, headers) do
headers ++ [{"X-Starfighter-Authorization", token}]
end
def authorization_header(_, headers), do: headers
@doc """
Same as `authorization_header/2` but defaults initial headers to include `@user_agent`.
"""
def authorization_header(options), do: authorization_header(options, @user_agent)
end