-
Notifications
You must be signed in to change notification settings - Fork 50
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
enables the use of per-request api-key/secret-key passing while keeping backward compatibility. #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / test (24.3, 1.14.5)
Check warning on line 6 in lib/binance/rest/http_client.ex GitHub Actions / test (26.1, 1.14.5)
Check warning on line 6 in lib/binance/rest/http_client.ex GitHub Actions / test (25.3, 1.14.5)
|
||
case validate_credentials(secret_key, api_key) do | ||
{:error, _} = error -> | ||
error | ||
|
@@ -33,7 +33,7 @@ | |
end | ||
end | ||
|
||
defp request_binance(url, body, method) do | ||
defp request_binance(api_key, url, body, method) do | ||
url = URI.parse("#{endpoint()}#{url}") | ||
|
||
encoded_url = | ||
|
@@ -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}, | ||
] | ||
) | ||
|
||
|
@@ -65,7 +65,7 @@ | |
URI.to_string(encoded_url), | ||
"", | ||
[ | ||
{"X-MBX-APIKEY", Application.get_env(:binance, :api_key)} | ||
{"X-MBX-APIKEY", api_key}, | ||
] | ||
]) | ||
end | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
argument_string = | ||
params | ||
|> prepare_query_params() | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 """ | ||
|
@@ -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 GitHub Actions / test (24.3, 1.14.5)
Check warning on line 116 in lib/binance/rest/http_client.ex GitHub Actions / test (26.1, 1.14.5)
Check warning on line 116 in lib/binance/rest/http_client.ex GitHub Actions / test (25.3, 1.14.5)
|
||
do: {:error, {:config_missing, "Secret and API key missing"}} | ||
|
||
defp validate_credentials(nil, _api_key), | ||
|
@@ -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 GitHub Actions / test (24.3, 1.14.5)
Check warning on line 128 in lib/binance/rest/http_client.ex GitHub Actions / test (26.1, 1.14.5)
Check warning on line 128 in lib/binance/rest/http_client.ex GitHub Actions / test (25.3, 1.14.5)
|
||
response.body | ||
|> Poison.decode() | ||
|> parse_response_body | ||
|
@@ -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 GitHub Actions / test (24.3, 1.14.5)
Check warning on line 138 in lib/binance/rest/http_client.ex GitHub Actions / test (26.1, 1.14.5)
Check warning on line 138 in lib/binance/rest/http_client.ex GitHub Actions / test (25.3, 1.14.5)
|
||
case data do | ||
%{"code" => _c, "msg" => _m} = error -> {:error, error} | ||
_ -> {:ok, data} | ||
|
There was a problem hiding this comment.
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