From 00ddaf7515f4a007d8a3c4dade202d5a998c9e03 Mon Sep 17 00:00:00 2001 From: Franz Bettag Date: Sat, 28 Oct 2023 21:07:02 +0200 Subject: [PATCH] enables the use of per-request api-key/secret-key passing while keeping backward compatibility. --- lib/binance.ex | 9 +++++++-- lib/binance/rest/http_client.ex | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/binance.ex b/lib/binance.ex index f6ba311..9670ffd 100644 --- a/lib/binance.ex +++ b/lib/binance.ex @@ -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 @@ -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)} @@ -195,6 +199,7 @@ docs false -> case HTTPClient.unsigned_request_binance( + api_key, unquote(url), adjusted_args, unquote(method) diff --git a/lib/binance/rest/http_client.ex b/lib/binance/rest/http_client.ex index f9b10cd..b051263 100644 --- a/lib/binance/rest/http_client.ex +++ b/lib/binance/rest/http_client.ex @@ -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 = @@ -46,7 +46,7 @@ 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} ] ) @@ -54,7 +54,7 @@ defmodule Binance.Rest.HTTPClient do HTTPoison.delete( URI.to_string(url), [ - {"X-MBX-APIKEY", Application.get_env(:binance, :api_key)} + {"X-MBX-APIKEY", api_key} ] ) @@ -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 @@ -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() @@ -88,14 +91,14 @@ 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 """ @@ -103,12 +106,12 @@ defmodule Binance.Rest.HTTPClient do 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),