Skip to content

Commit

Permalink
enables the use of per-request api-key/secret-key passing while keepi…
Browse files Browse the repository at this point in the history
…ng backward compatibility.
  • Loading branch information
fbettag committed Oct 28, 2023
1 parent 77e7807 commit 00ddaf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
9 changes: 7 additions & 2 deletions lib/binance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,21 @@ docs
"""

# fx without opts
@spec unquote(fx_name)(unquote_splicing(spec)) ::
@spec unquote(fx_name)(String.t() | nil, String.t() | nil, unquote_splicing(spec)) ::
{:ok, any()} | {:error, any()}

# fx with opts
@spec unquote(fx_name)(
String.t() | nil,
String.t() | nil,
unquote_splicing(spec),
unquote(optional_args)
) ::
{:ok, any()} | {:error, any()}

def unquote(fx_name)(
api_key \\ unquote(Application.get_env(:binance, :api_key)),
secret_key \\ unquote(Application.get_env(:binance, :secret_key)),
unquote_splicing(arg_names),
opts \\ []
) do
Expand Down Expand Up @@ -185,7 +189,7 @@ docs

case unquote(needs_auth) do
true ->
case HTTPClient.signed_request_binance(unquote(url), adjusted_args, unquote(method)) do
case HTTPClient.signed_request_binance(api_key, secret_key, unquote(url), adjusted_args, unquote(method)) do
{:ok, %{"code" => _code, "msg" => _msg} = err} ->
{:error, Binance.Helper.format_error(err)}

Expand All @@ -195,6 +199,7 @@ docs

false ->
case HTTPClient.unsigned_request_binance(
api_key,
unquote(url),
adjusted_args,
unquote(method)
Expand Down
21 changes: 12 additions & 9 deletions lib/binance/rest/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Binance.Rest.HTTPClient do
end
end

defp request_binance(url, body, method) do
defp request_binance(api_key, url, body, method) do
url = URI.parse("#{@endpoint}#{url}")

url =
Expand All @@ -46,15 +46,15 @@ defmodule Binance.Rest.HTTPClient do
HTTPoison.get(
URI.to_string(url),
[
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)}
{"X-MBX-APIKEY", api_key}
]
)

:delete ->
HTTPoison.delete(
URI.to_string(url),
[
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)}
{"X-MBX-APIKEY", api_key}
]
)

Expand All @@ -63,7 +63,7 @@ defmodule Binance.Rest.HTTPClient do
URI.to_string(url),
body,
[
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)}
{"X-MBX-APIKEY", api_key}
]
])
end
Expand All @@ -79,7 +79,10 @@ defmodule Binance.Rest.HTTPClient do
end
end

def signed_request_binance(url, params, method) do
@api_key Application.get_env(:binance, :api_key)
@secret_key Application.get_env(:binance, :secret_key)

def signed_request_binance(api_key \\ @api_key, secret_key \\ @secret_key, url, params, method) do
argument_string =
params
|> prepare_query_params()
Expand All @@ -88,27 +91,27 @@ defmodule Binance.Rest.HTTPClient do
signature =
generate_signature(
:sha256,
Application.get_env(:binance, :secret_key),
secret_key,
argument_string
)
|> Base.encode16()

body = "#{argument_string}&signature=#{signature}"

request_binance(url, body, method)
request_binance(api_key, url, body, method)
end

@doc """
You need to send an empty body and the api key
to be able to create a new listening key.
"""
def unsigned_request_binance(url, data, method) do
def unsigned_request_binance(api_key \\ @api_key, url, data, method) do
argument_string =
data
|> prepare_query_params()

request_binance(url, argument_string, method)
request_binance(api_key, url, argument_string, method)
end

defp validate_credentials(nil, nil),
Expand Down

0 comments on commit 00ddaf7

Please sign in to comment.