Skip to content
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ It's recommended to pass in the `base_url` parameter, even in production as Bina
- `https://api2.binance.com`
- `https://api3.binance.com`

### Proxy

If you need to use a proxy you can define `proxy_url`

```
client = Binance::Spot.new(base_url: 'https://api1.binance.com', proxy_url: 'http://myproxy.com')
```

### RSA Signature

Binance support both HMAC and RSA signature. Please find the example at `examples/trade/account.rb` for how to sign in both solutions.
Expand Down
5 changes: 4 additions & 1 deletion lib/binance/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Session

def initialize(options = {})
@base_url = options[:base_url] || 'https://api.binance.com'
@proxy_url = options[:proxy_url]
@auth = Authentication.new(options[:key], options[:secret], options[:private_key], options[:private_key_pass_phrase])
@logger = options[:logger]
@show_weight_usage = options[:show_weight_usage] || false
Expand Down Expand Up @@ -84,7 +85,9 @@ def signed_conn
end

def build_connection
Faraday.new(url: @base_url) do |client|
params = { url: @base_url }
params.merge!(proxy: @proxy_url) if @proxy_url
Faraday.new(params) do |client|
prepare_headers(client)
client.options.timeout = @timeout
client.options.params_encoder = Binance::Utils::Faraday::CustomParamsEncoder
Expand Down
29 changes: 28 additions & 1 deletion spec/session_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

class Session < Binance::Session
attr_reader :base_url, :headers, :auth, :logger, :show_weight_usage, :show_header, :timeout
attr_reader :base_url, :proxy_url, :headers, :auth, :logger, :show_weight_usage, :show_header, :timeout
end

RSpec.describe Binance::Session do
context 'should init' do
where(:options, :expected_options) do
[
[{ base_url: 'base_url' }, { base_url: 'base_url', auth: Binance::Authentication.new(nil, nil), show_weight_usage: false, show_header: false, timeout: nil }],
[{ base_url: 'base_url', proxy_url: 'proxy_url' }, { base_url: 'base_url', proxy_url: 'proxy_url', auth: Binance::Authentication.new(nil, nil), show_weight_usage: false, show_header: false, timeout: nil }],
[{ key: 'key', secret: 'secret' }, { base_url: 'https://api.binance.com', auth: Binance::Authentication.new('key', 'secret'), show_weight_usage: false, show_header: false, timeout: nil }],
[{ show_weight_usage: true }, { base_url: 'https://api.binance.com', auth: Binance::Authentication.new(nil, nil), show_weight_usage: true, show_header: false, timeout: nil }],
[{ show_header: true }, { base_url: 'https://api.binance.com', auth: Binance::Authentication.new(nil, nil), show_weight_usage: false, show_header: true, timeout: nil }],
Expand All @@ -19,6 +20,7 @@ class Session < Binance::Session
subject { Session.new(options) }
it 'include the updated options' do
expect(subject.base_url).to eq(expected_options[:base_url])
expect(subject.proxy_url).to eq(expected_options[:proxy_url])
expect(subject.auth.key).to eq(expected_options[:auth].key)
expect(subject.auth.secret).to eq(expected_options[:auth].secret)
expect(subject.logger).to eq(nil)
Expand All @@ -33,6 +35,7 @@ class Session < Binance::Session
subject { Session.new({ logger: Logger.new($stdout) }) }
it 'include the updated options' do
expect(subject.base_url).to eq('https://api.binance.com')
expect(subject.proxy_url).to eq(nil)
expect(subject.auth.key).to eq(nil)
expect(subject.auth.secret).to eq(nil)
expect(subject.logger).to be_a(Logger)
Expand All @@ -41,4 +44,28 @@ class Session < Binance::Session
expect(subject.timeout).to eq(nil)
end
end

context 'when init with proxy' do
before do
allow(Faraday).to receive(:new).with(url: 'https://api.binance.com', proxy: 'proxy_url')
end

subject { Session.new(proxy_url: 'proxy_url' ) }
it 'include the proxy options' do
subject.send(:build_connection)
expect(Faraday).to have_received(:new).with(url: 'https://api.binance.com', proxy: 'proxy_url')
end
end

context 'when init without proxy' do
before do
allow(Faraday).to receive(:new).with(url: 'https://api.binance.com')
end

subject { Session.new( url: 'test') }
it 'not include the proxy options' do
subject.send(:build_connection)
expect(Faraday).to have_received(:new).with(url: 'https://api.binance.com')
end
end
end