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

enables the use of per-request api-key/secret-key passing while keeping backward compatibility. #105

Merged
merged 1 commit into from
Sep 2, 2024
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
6 changes: 4 additions & 2 deletions lib/binance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ docs
binding = binding()

# merge all passed args together, so opts + passed
all_passed_args = Keyword.merge(binding, opts) |> Keyword.drop([:opts])
all_passed_args = Keyword.merge(binding, opts) |> Keyword.drop([:opts, :api_key, :secret_key])
api_key = Keyword.get(opts, :api_key) || Application.get_env(:binance, :api_key, "")
secret_key = Keyword.get(opts, :secret_key) || Application.get_env(:binance, :secret_key, "")

# if the call requires a timestamp, we add it
adjusted_args =
Expand Down Expand Up @@ -185,7 +187,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 Down
16 changes: 8 additions & 8 deletions lib/binance/rest/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Application.get_env(:binance, :end_point, "https://api.binance.com")
end

defp prepare_request(url, params, secret_key, api_key) do

Check warning on line 6 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (24.3, 1.14.5)

function prepare_request/4 is unused

Check warning on line 6 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (26.1, 1.14.5)

function prepare_request/4 is unused

Check warning on line 6 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (25.3, 1.14.5)

function prepare_request/4 is unused

Check warning on line 6 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / testolder (23.3, 1.14)

function prepare_request/4 is unused
case validate_credentials(secret_key, api_key) do
{:error, _} = error ->
error
Expand Down Expand Up @@ -33,7 +33,7 @@
end
end

defp request_binance(url, body, method) do
defp request_binance(api_key, url, body, method) do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of this function is becoming a bit odd. API key should probably come after url, or in the end within optional args. Or even better - a pattern-matched function with different signature for apikey and no apikey

url = URI.parse("#{endpoint()}#{url}")

encoded_url =
Expand All @@ -48,15 +48,15 @@
HTTPoison.get(
URI.to_string(encoded_url),
[
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)}
{"X-MBX-APIKEY", api_key},
]
)

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

Expand All @@ -65,7 +65,7 @@
URI.to_string(encoded_url),
"",
[
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)}
{"X-MBX-APIKEY", api_key},
]
])
end
Expand All @@ -81,7 +81,7 @@
end
end

def signed_request_binance(url, params, method) do
def signed_request_binance(api_key, secret_key, url, params, method) do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

argument_string =
params
|> prepare_query_params()
Expand All @@ -90,14 +90,14 @@
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a bit odd

Probably better to make it a function with different signature or optional arg

end

@doc """
Expand All @@ -110,10 +110,10 @@
data
|> prepare_query_params()

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

defp validate_credentials(nil, nil),

Check warning on line 116 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (24.3, 1.14.5)

function validate_credentials/2 is unused

Check warning on line 116 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (26.1, 1.14.5)

function validate_credentials/2 is unused

Check warning on line 116 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (25.3, 1.14.5)

function validate_credentials/2 is unused

Check warning on line 116 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / testolder (23.3, 1.14)

function validate_credentials/2 is unused
do: {:error, {:config_missing, "Secret and API key missing"}}

defp validate_credentials(nil, _api_key),
Expand All @@ -125,7 +125,7 @@
defp validate_credentials(_secret_key, _api_key),
do: :ok

defp parse_response({:ok, response}) do

Check warning on line 128 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (24.3, 1.14.5)

function parse_response/1 is unused

Check warning on line 128 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (26.1, 1.14.5)

function parse_response/1 is unused

Check warning on line 128 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (25.3, 1.14.5)

function parse_response/1 is unused

Check warning on line 128 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / testolder (23.3, 1.14)

function parse_response/1 is unused
response.body
|> Poison.decode()
|> parse_response_body
Expand All @@ -135,7 +135,7 @@
{:error, {:http_error, err}}
end

defp parse_response_body({:ok, data}) do

Check warning on line 138 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (24.3, 1.14.5)

function parse_response_body/1 is unused

Check warning on line 138 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (26.1, 1.14.5)

function parse_response_body/1 is unused

Check warning on line 138 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / test (25.3, 1.14.5)

function parse_response_body/1 is unused

Check warning on line 138 in lib/binance/rest/http_client.ex

View workflow job for this annotation

GitHub Actions / testolder (23.3, 1.14)

function parse_response_body/1 is unused
case data do
%{"code" => _c, "msg" => _m} = error -> {:error, error}
_ -> {:ok, data}
Expand Down
Loading