-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
111 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Basket.Websocket.Alpaca.Impl do | ||
require Logger | ||
|
||
@subscribe_message %{ | ||
action: :subscribe | ||
} | ||
@unsubscribe_message %{ | ||
action: :unsubscribe | ||
} | ||
|
||
def start_link(state) do | ||
Logger.info("Starting Alpaca websocket client.") | ||
|
||
WebSockex.start_link(iex_feed(), Basket.Websocket.Alpaca, state, extra_headers: auth_headers()) | ||
end | ||
|
||
def subscribe(tickers) do | ||
decoded_message = Jason.encode!(build_message(@subscribe_message, tickers)) | ||
|
||
case WebSockex.send_frame(client_pid(), {:text, decoded_message}) do | ||
:ok -> Logger.debug("Subscription message sent: #{inspect(decoded_message)}") | ||
{:error, error} -> Logger.error("Error sending subscription message: #{inspect(error)}") | ||
end | ||
end | ||
|
||
def unsubscribe(tickers) do | ||
decoded_message = Jason.encode!(build_message(@unsubscribe_message, tickers)) | ||
|
||
case WebSockex.send_frame(client_pid(), {:text, decoded_message}) do | ||
:ok -> | ||
Logger.debug("Subscription removal message sent: #{inspect(decoded_message)}") | ||
|
||
{:error, error} -> | ||
Logger.error("Error sending subscription removal message: #{inspect(error)}") | ||
end | ||
end | ||
|
||
defp auth_headers, do: [{"APCA-API-KEY-ID", api_key()}, {"APCA-API-SECRET-KEY", api_secret()}] | ||
|
||
defp api_key, do: Application.fetch_env!(:basket, :alpaca)[:api_key] | ||
|
||
defp api_secret, do: Application.fetch_env!(:basket, :alpaca)[:api_secret] | ||
|
||
defp iex_feed, do: "#{url()}/iex" | ||
|
||
defp url, do: Application.fetch_env!(:basket, :alpaca)[:market_ws_url] | ||
|
||
defp client_pid do | ||
Supervisor.which_children(Basket.Supervisor) | ||
|> Enum.find(fn c -> | ||
case c do | ||
{Basket.Websocket.Alpaca, _pid, :worker, [Basket.Websocket.Alpaca]} -> | ||
true | ||
|
||
_ -> | ||
false | ||
end | ||
end) | ||
|> elem(1) | ||
end | ||
|
||
defp build_message(message, %{bars: bars, quotes: quotes, trades: trades}) do | ||
message = if bars, do: Map.put(message, :bars, bars), else: message | ||
message = if quotes, do: Map.put(message, :quotes, quotes), else: message | ||
if trades, do: Map.put(message, :trades, trades), else: message | ||
end | ||
end |
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
Empty file.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule BasketWeb.OverviewTest do | ||
use BasketWeb.ConnCase | ||
|
||
import Mox | ||
|
||
test "mount/3" do | ||
Basket.Websocket.MockAlpaca |> expect(:start_link, fn state -> {:ok, 1} end) | ||
|
||
Basket.Websocket.Alpaca.start_link("statee") |> IO.inspect(label: "RESULT") | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Mox.defmock(Basket.Alpaca.Websocket.MockClient, for: Basket.Alpaca.Websocket.Client) | ||
Application.put_env(:basket, :alpaca_websocket_client, Basket.Alpaca.Websocket.MockClient) | ||
Mox.defmock(Basket.Websocket.MockAlpaca, for: Basket.Websocket.Alpaca) | ||
Application.put_env(:basket, :alpaca_ws_client, Basket.Websocket.MockAlpaca) | ||
|
||
ExUnit.start() | ||
Ecto.Adapters.SQL.Sandbox.mode(Basket.Repo, :manual) |