Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#12 Dont guard string fields in json responses #15

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ npm-debug.log

# VSCode
.vscode/

# PLTs
priv/plts/
39 changes: 31 additions & 8 deletions lib/xtb_client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ defmodule XtbClient.Connection do
alias XtbClient.{MainSocket, StreamingSocket, StreamingMessage}

alias XtbClient.Messages.{
Candle,
Candles,
ChartLast,
ChartRange,
DateRange,
ProfitCalculation,
RateInfos,
Quotations,
SymbolInfo,
SymbolVolume,
Expand Down Expand Up @@ -116,12 +118,11 @@ defmodule XtbClient.Connection do
@impl true
def init(opts) do
{:ok, mpid} = MainSocket.start_link(opts)
Process.flag(:trap_exit, true)

Process.sleep(500)
MainSocket.stream_session_id(mpid, self())

Process.flag(:trap_exit, true)

type = get_in(opts, [:type])
url = get_in(opts, [:url])

Expand Down Expand Up @@ -509,7 +510,7 @@ defmodule XtbClient.Connection do
ref_string = inspect(ref)
MainSocket.query(mpid, self(), ref_string, method)

clients = Map.put(clients, ref_string, from)
clients = Map.put(clients, ref_string, {from, method, nil})
state = %State{state | clients: clients}

{:noreply, state}
Expand All @@ -524,17 +525,32 @@ defmodule XtbClient.Connection do
ref_string = inspect(ref)
MainSocket.query(mpid, self(), ref_string, method, params)

clients = Map.put(clients, ref_string, from)
clients = Map.put(clients, ref_string, {from, method, params})
state = %State{state | clients: clients}

{:noreply, state}
end

@impl true
def handle_cast({:response, ref, resp} = _message, %State{clients: clients} = state) do
{client, clients} = Map.pop!(clients, ref)
GenServer.reply(client, resp)
state = %State{state | clients: clients}
def handle_cast(
{:response, ref, %RateInfos{data: data} = resp} = _message,
%State{clients: clients} = state
) do
{_client, _method, %{info: %{symbol: symbol}}} = Map.get(clients, ref)

resp = %RateInfos{
resp
| data: Enum.map(data, &%Candle{&1 | symbol: symbol})
}

state = handle_query_response(ref, resp, state)

{:noreply, state}
end

@impl true
def handle_cast({:response, ref, resp} = _message, %State{} = state) do
state = handle_query_response(ref, resp, state)

{:noreply, state}
end
Expand Down Expand Up @@ -581,6 +597,13 @@ defmodule XtbClient.Connection do
{:noreply, state}
end

defp handle_query_response(ref, response, %State{clients: clients} = state) do
{{client, _method, _params}, clients} = Map.pop!(clients, ref)
GenServer.reply(client, response)

%State{state | clients: clients}
end

@impl true
def handle_info({:EXIT, pid, reason}, state) do
Logger.error(
Expand Down
79 changes: 0 additions & 79 deletions lib/xtb_client/messages.ex

This file was deleted.

83 changes: 36 additions & 47 deletions lib/xtb_client/messages/balance_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule XtbClient.Messages.BalanceInfo do
balance: float(),
cash_stock_value: float(),
credit: float(),
currency: String.t(),
currency: String.t() | nil,
equity: float(),
equity_fx: float(),
margin: float(),
Expand All @@ -47,12 +47,11 @@ defmodule XtbClient.Messages.BalanceInfo do
:stock_lock,
:stock_value
]

@derive Jason.Encoder
defstruct balance: 0.0,
cash_stock_value: 0.0,
credit: 0.0,
currency: "",
currency: nil,
equity: 0.0,
equity_fx: 0.0,
margin: 0.0,
Expand All @@ -61,36 +60,36 @@ defmodule XtbClient.Messages.BalanceInfo do
stock_lock: 0.0,
stock_value: 0.0

def new(%{
"balance" => balance,
"cashStockValue" => cash_stock_value,
"credit" => credit,
"currency" => currency,
"equity" => equity,
"equityFX" => equity_fx,
"margin" => margin,
"margin_free" => margin_free,
"margin_level" => margin_level,
"stockLock" => stock_lock,
"stockValue" => stock_value
})
when is_number(balance) and is_number(cash_stock_value) and is_number(credit) and
is_binary(currency) and is_number(equity) and is_number(equity_fx) and
is_number(margin) and is_number(margin_free) and is_number(margin_level) and
is_number(stock_lock) and is_number(stock_value) do
%__MODULE__{
balance: balance,
cash_stock_value: cash_stock_value,
credit: credit,
currency: currency,
equity: equity,
equity_fx: equity_fx,
margin: margin,
margin_free: margin_free,
margin_level: margin_level,
stock_lock: stock_lock,
stock_value: stock_value
}
def new(%{"currency" => currency} = args) do
value = args |> Map.drop(["currency"]) |> new()

%{value | currency: currency || ""}
end

def new(
%{
"margin_free" => margin_free,
"margin_level" => margin_level
} = args
)
when is_number(margin_free) and is_number(margin_level) do
value = args |> Map.drop(["margin_free", "margin_level"]) |> new()

%{value | margin_free: margin_free, margin_level: margin_level}
end

def new(
%{
"marginFree" => margin_free,
"marginLevel" => margin_level
} = args
)
when is_number(margin_free) and is_number(margin_level) do
args
|> Map.drop(["marginFree", "marginLevel"])
|> Map.put("margin_free", margin_free)
|> Map.put("margin_level", margin_level)
|> new()
end

def new(%{
Expand All @@ -100,35 +99,25 @@ defmodule XtbClient.Messages.BalanceInfo do
"equity" => equity,
"equityFX" => equity_fx,
"margin" => margin,
"marginFree" => margin_free,
"marginLevel" => margin_level,
"stockLock" => stock_lock,
"stockValue" => stock_value
})
when is_number(balance) and is_number(cash_stock_value) and is_number(credit) and
is_number(equity) and is_number(equity_fx) and
is_number(margin) and is_number(margin_free) and is_number(margin_level) and
is_number(margin) and
is_number(stock_lock) and is_number(stock_value) do
%__MODULE__{
balance: balance,
cash_stock_value: cash_stock_value,
credit: credit,
currency: "",
currency: nil,
equity: equity,
equity_fx: equity_fx,
margin: margin,
margin_free: margin_free,
margin_level: margin_level,
margin_free: 0.0,
margin_level: 0.0,
stock_lock: stock_lock,
stock_value: stock_value
}
end

def match(method, data) when method in ["getBalance", "getMarginLevel"] do
{:ok, __MODULE__.new(data)}
end

def match(_method, _data) do
{:no_match}
end
end
20 changes: 8 additions & 12 deletions lib/xtb_client/messages/calendar_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ defmodule XtbClient.Messages.CalendarInfo do
}

@enforce_keys [:country, :current, :forecast, :impact, :period, :previous, :time, :title]

@derive Jason.Encoder
defstruct country: "",
current: "",
Expand All @@ -45,19 +44,16 @@ defmodule XtbClient.Messages.CalendarInfo do
"previous" => previous,
"time" => time_value,
"title" => title
})
when is_binary(country) and is_binary(current) and is_binary(forecast) and
is_binary(impact) and is_binary(period) and is_binary(previous) and
is_number(time_value) and is_binary(title) do
}) do
%__MODULE__{
country: country,
current: current,
forecast: forecast,
impact: impact,
period: period,
previous: previous,
country: country || "",
current: current || "",
forecast: forecast || "",
impact: impact || "",
period: period || "",
previous: previous || "",
time: DateTime.from_unix!(time_value, :millisecond),
title: title
title: title || ""
}
end
end
17 changes: 5 additions & 12 deletions lib/xtb_client/messages/calendar_infos.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
defmodule XtbClient.Messages.CalendarInfos do
alias XtbClient.Messages.{CalendarInfo}

@moduledoc """
Query result for list of `XtbClient.Messages.CalendarInfo`s.

## Parameters
- `data` array or results.

## Handled Api methods
- `getCalendar`
"""

alias XtbClient.Messages.CalendarInfo

@type t :: %__MODULE__{
data: [CalendarInfo.t()]
}

@enforce_keys [:data]
@derive Jason.Encoder
defstruct data: []

def new(data) when is_list(data) do
Expand All @@ -25,12 +26,4 @@ defmodule XtbClient.Messages.CalendarInfos do
|> Enum.map(&CalendarInfo.new(&1))
}
end

def match(method, data) when method in ["getCalendar"] do
{:ok, __MODULE__.new(data)}
end

def match(_method, _data) do
{:no_match}
end
end
Loading
Loading