Created
January 31, 2019 19:37
-
-
Save tzumby/42d2740f08932cb3ae21a8f55bab14d2 to your computer and use it in GitHub Desktop.
Bittrex client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule CryptoPrices.Aggregator.Bittrex do | |
@limit 10 | |
@base "http://api.bittrex.com/api/v1.1/public/getorderbook" | |
def compute(pair, _opts) do | |
pair | |
|> fetch_json() | |
|> format_results() | |
end | |
defp format_results({:ok, %{ "result" => %{ "buy" => buy }}}) do | |
buy | |
|> Enum.map(&build_bid/1) | |
|> Enum.take(@limit) | |
end | |
defp build_bid(%{ "Quantity" => quantity, "Rate" => price }) do | |
%{price: price, quantity: quantity} | |
end | |
defp fetch_json(pair) do | |
case HTTPoison.get(url(pair)) do | |
{:ok, %HTTPoison.Response{body: body, status_code: 200}} -> | |
{:ok, Jason.decode!(body) } | |
{:ok, %HTTPoison.Response{status_code: 404}} -> | |
{:error, :not_found } | |
{:ok, %HTTPoison.Error{reason: reason}} -> | |
{:error, reason } | |
end | |
end | |
defp url(pair) do | |
"#{@base}?" <> URI.encode_query(market: pair, type: "both") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment