bitget.rb 0.8.1 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d909ec3522e84a402eba1efa3b20d486f694d26d80e0f88a12403a79cec1e0c5
4
- data.tar.gz: 5f60da34e79b5b904586ae99f7424cbe60dcf8766e1967ee69680211e17a2362
3
+ metadata.gz: 1c22bb60a209681136a580eecdf81cf0298cf3c78f51b99588a49b6b13c3c6db
4
+ data.tar.gz: '09f25185c72ad7fadf3ffb1bf1264650e0cdf2fd297423e3128b7ddeb5f696da'
5
5
  SHA512:
6
- metadata.gz: e773d67c5641ea60774323f9b9ec62a32442100a6da93332ecdce0a2519d06823d41b4b1f0b2055b7dc0df795830d356fe144ab01f7e62b1cfea8cca19c9ec77
7
- data.tar.gz: 14526440486abd37aa28c2838a3320d8740490853618f0525402ddc577442f59da94a7c9c40bf7d8e1210c25886f9fcaf3459bee787c13da7b5f3a561c801deb
6
+ metadata.gz: e36b1a7ff42846eb1a95f2b4eae6d9ede1b2955a62724e48781e25145cb1079508e6c95e110c41507d6522fc71327089625f4d7496af1866474cdc9c06263f50
7
+ data.tar.gz: b7bea187cba271df689255f4b46411f49808a9aad28b8c40468c3190415b1bd7ced84e3d82586a335fbdcf6eafd0b69436f340c615720f56ededa5236784db3d
data/CHANGELOG CHANGED
@@ -2,6 +2,28 @@
2
2
 
3
3
  ## 20261115
4
4
 
5
+ 0.9.1: Fix the request log exposing the credentials.
6
+
7
+ 1. + Bitget::V2::Client::CREDENTIAL_HEADERS: ACCESS-KEY and ACCESS-PASSPHRASE.
8
+ 2. + Bitget::V2::Client#masked: The credential headers' values are replaced with [FILTERED].
9
+ 3. ~ Bitget::V2::Client#log_request: The headers are logged through #masked. The key and passphrase were written to the log as sent.
10
+ 4. ~ test/V2/Client_test.rb: The logged key and passphrase are masked, and the signature is not.
11
+ 5. ~ README.md: The Logging section says the credential headers are masked.
12
+ 6. ~ Bitget::VERSION: /0.9.0/0.9.1/
13
+
14
+ 0.9.0: ~ Bitget::Client#initialize: - options:; - Bitget.configure, Bitget::Configuration.
15
+
16
+ 1. ~ Bitget::V2::Client#initialize: + debug:, + logger:, - options:. Nothing falls back upon Bitget.configuration, under which a client worked or failed according to what had run before it.
17
+ 2. ~ Bitget::V2::Client#initialize: It yields the client being built.
18
+ 3. + Bitget::V2::Client#validate_args: The credentials not given raise ArgumentError naming each of them, rather than as a TypeError from OpenSSL or a refusal from Bitget.
19
+ 4. ~ Bitget::V2::Client#do_request: The credentials are validated before the request is signed.
20
+ 5. - lib/Bitget/Configuration.rb: Bitget::Configuration, Bitget.configure, .configuration, .configuration= and .reset_configuration!.
21
+ 6. - test/Configuration_test.rb
22
+ 7. ~ test/V2/Client_test.rb: The debug: and logger: keywords and the block set a client; it defaults to nothing; missing credentials raise naming each, before any request; Bitget keeps no configuration.
23
+ 8. ~ test/helper.rb: - the reset of Bitget.configuration before each test.
24
+ 9. ~ README.md: The Setup and Logging sections give everything to the client, and the Configuration section is gone. The installation instructions name the gem bitget, not bitget.rb.
25
+ 10. ~ Bitget::VERSION: /0.8.1/0.9.0/
26
+
5
27
  0.8.1: Fix the tests failing to load WebMock.
6
28
 
7
29
  1. ~ test/helper.rb: + skip_webmocks_http_rb_adapter, so as to disambiguate the http gem from the http.rb gem.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Access the Bitget API with Ruby.
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'bitget.rb'
12
+ gem 'bitget'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -21,7 +21,7 @@ $ bundle
21
21
  Or install it yourself as:
22
22
 
23
23
  ```bash
24
- $ gem install bitget.rb
24
+ $ gem install bitget
25
25
  ```
26
26
 
27
27
  ## Usage
@@ -36,43 +36,52 @@ bitget_client = Bitget::Client.new(
36
36
  )
37
37
  ```
38
38
 
39
- ### Configuration
40
-
41
- Settings may be declared once, and every client built afterwards reads them as its defaults.
39
+ Everything a client uses is given to it: `api_key:`, `api_secret:`, `api_passphrase:`,
40
+ `debug:` and `logger:`. Each has a setter of its own, and the client is yielded while it is
41
+ being built, so these are the same:
42
42
 
43
43
  ```ruby
44
- Bitget.configure do |config|
45
- config.api_key = 'api_key0'
46
- config.api_secret = 'api_secret0'
47
- config.api_passphrase = 'api_passphrase0'
44
+ bitget_client = Bitget::Client.new do |c|
45
+ c.api_key = 'api_key0'
46
+ c.api_secret = 'api_secret0'
47
+ c.api_passphrase = 'api_passphrase0'
48
48
  end
49
49
 
50
50
  bitget_client = Bitget::Client.new
51
+ bitget_client.api_key = 'api_key0'
52
+ bitget_client.api_secret = 'api_secret0'
53
+ bitget_client.api_passphrase = 'api_passphrase0'
51
54
  ```
52
55
 
53
- The settings are `api_key`, `api_secret`, `api_passphrase`, `debug` and `logger`.
54
-
55
- Any of them may still be given per client, which wins over the configured value. The
56
- credentials are named arguments; the rest go under `options`.
56
+ The client keeps nothing in common with any other, and reads nothing from the environment
57
+ or from a file: where the settings come from is the program's business. Credentials not
58
+ given raise at the first request, before anything is sent, naming each of them:
57
59
 
58
60
  ```ruby
59
- bitget_client = Bitget::Client.new(
60
- api_key: 'api_key1',
61
- options: {logger: Logger.new('bitget.log', 'daily')}
62
- )
61
+ Bitget::Client.new(api_key: 'api_key0', api_secret: 'api_secret0').spot_public_coins
62
+ # => ArgumentError: api_passphrase is missing.
63
63
  ```
64
64
 
65
+ Before 0.9.0 the settings could be declared once with `Bitget.configure`, and every client
66
+ fell back upon them, the rest going under `options:`. Pass them to each client instead.
67
+
65
68
  ### Logging
66
69
 
67
70
  A client logs when it has somewhere to log to, and not at all when it does not. Hand it any
68
71
  object answering to `#info` and `#error`; the standard library's `Logger` will do.
69
72
 
73
+ A request is logged with its headers, the key and the passphrase being written as
74
+ `[FILTERED]`.
75
+
70
76
  ```ruby
71
77
  require 'logger'
72
78
 
73
- Bitget.configure do |config|
74
- config.logger = Logger.new($stdout)
75
- end
79
+ bitget_client = Bitget::Client.new(
80
+ api_key: 'api_key0',
81
+ api_secret: 'api_secret0',
82
+ api_passphrase: 'api_passphrase0',
83
+ logger: Logger.new($stdout)
84
+ )
76
85
  ```
77
86
 
78
87
  `Logger` rotates by itself, so a daily log file wants no more than its second argument. It
@@ -85,9 +94,7 @@ require 'logger'
85
94
  log_file_path = File.expand_path(File.join(%w{~ log bitget log.txt}))
86
95
  FileUtils.mkdir_p(File.dirname(log_file_path))
87
96
 
88
- Bitget.configure do |config|
89
- config.logger = Logger.new(log_file_path, 'daily')
90
- end
97
+ bitget_client.logger = Logger.new(log_file_path, 'daily')
91
98
  ```
92
99
 
93
100
  Before 0.6.0 the client chose the path, created the directory and rotated the file itself. It
@@ -8,7 +8,6 @@ require 'json'
8
8
  require 'logger'
9
9
  require 'openssl'
10
10
 
11
- require_relative '../Configuration'
12
11
  require_relative '../Error'
13
12
 
14
13
  module Bitget
@@ -16,6 +15,7 @@ module Bitget
16
15
  class Client
17
16
 
18
17
  API_HOST = 'api.bitget.com'
18
+ CREDENTIAL_HEADERS = %w{ACCESS-KEY ACCESS-PASSPHRASE}
19
19
 
20
20
  class << self
21
21
 
@@ -1812,17 +1812,24 @@ module Bitget
1812
1812
 
1813
1813
  private
1814
1814
 
1815
- # Anything not given here is taken from Bitget.configuration, so that a
1816
- # setting declared once need not be repeated at every call site. The
1817
- # credentials fall back upon nil, having no meaningful nil value of their
1818
- # own; the options ask whether the key was given, nil being a value a
1819
- # caller may mean for a logger.
1820
- def initialize(api_key: nil, api_secret: nil, api_passphrase: nil, options: {})
1821
- @api_key = api_key || Bitget.configuration.api_key
1822
- @api_secret = api_secret || Bitget.configuration.api_secret
1823
- @api_passphrase = api_passphrase || Bitget.configuration.api_passphrase
1824
- @debug = options.key?(:debug) ? options[:debug] : Bitget.configuration.debug
1825
- @logger = options.key?(:logger) ? options[:logger] : Bitget.configuration.logger
1815
+ def initialize(api_key: nil, api_secret: nil, api_passphrase: nil, debug: false, logger: nil)
1816
+ @api_key = api_key
1817
+ @api_secret = api_secret
1818
+ @api_passphrase = api_passphrase
1819
+ @debug = debug
1820
+ @logger = logger
1821
+ yield(self) if block_given?
1822
+ end
1823
+
1824
+ def validate_args(args)
1825
+ missing_args = []
1826
+ args.each do |arg_name, value|
1827
+ missing_args << arg_name unless value
1828
+ end
1829
+ return if missing_args.empty?
1830
+ aggregate_error = missing_args.join(', ')
1831
+ copula = missing_args.count > 1 ? 'are' : 'is'
1832
+ raise ArgumentError, "#{aggregate_error} #{copula} missing."
1826
1833
  end
1827
1834
 
1828
1835
  def full_path(path)
@@ -1888,10 +1895,14 @@ module Bitget
1888
1895
  if log_args?(args)
1889
1896
  log_string << " Args: #{args}\n"
1890
1897
  end
1891
- log_string << " Headers: #{headers}\n"
1898
+ log_string << " Headers: #{masked(headers)}\n"
1892
1899
  logger.info(log_string)
1893
1900
  end
1894
1901
 
1902
+ def masked(headers)
1903
+ headers.to_h{|name, value| [name, CREDENTIAL_HEADERS.include?(name) ? '[FILTERED]' : value]}
1904
+ end
1905
+
1895
1906
  def log_response(code:, message:, body:)
1896
1907
  log_string = "Code: #{code}\n"
1897
1908
  log_string << "Message: #{message}\n"
@@ -1907,6 +1918,7 @@ module Bitget
1907
1918
  end
1908
1919
 
1909
1920
  def do_request(verb:, path:, args: {})
1921
+ validate_args(api_key: @api_key, api_secret: @api_secret, api_passphrase: @api_passphrase)
1910
1922
  sorted_args = args.reject{|_, v| v.nil?}.sort.to_h
1911
1923
  message = message(verb: verb, path: path, args: sorted_args)
1912
1924
  signature = signature(message)
@@ -2,5 +2,5 @@
2
2
  # Bitget::VERSION
3
3
 
4
4
  module Bitget
5
- VERSION = '0.8.1'
5
+ VERSION = '0.9.1'
6
6
  end
@@ -13,6 +13,72 @@ describe Bitget::V2::Client do
13
13
  )
14
14
  end
15
15
 
16
+ describe "#initialize" do
17
+ it "takes debug and logger as keywords" do
18
+ logger = Logger.new(StringIO.new)
19
+ client = Bitget::V2::Client.new(debug: true, logger: logger)
20
+ _(client.debug).must_equal(true)
21
+ _(client.logger).must_equal(logger)
22
+ end
23
+
24
+ it "defaults to no credentials, no debugging and no logger" do
25
+ client = Bitget::V2::Client.new
26
+ _(client.api_key).must_be_nil
27
+ _(client.api_secret).must_be_nil
28
+ _(client.api_passphrase).must_be_nil
29
+ _(client.debug).must_equal(false)
30
+ _(client.logger).must_be_nil
31
+ _(client.send(:use_logging?)).must_equal(false)
32
+ end
33
+
34
+ it "yields the client being built" do
35
+ yielded = nil
36
+ client = Bitget::V2::Client.new do |c|
37
+ yielded = c
38
+ c.api_key = 'a_key'
39
+ c.api_secret = 'a_secret'
40
+ c.api_passphrase = 'a_passphrase'
41
+ end
42
+ _(yielded).must_be_same_as(client)
43
+ _(client.api_key).must_equal('a_key')
44
+ _(client.api_secret).must_equal('a_secret')
45
+ _(client.api_passphrase).must_equal('a_passphrase')
46
+ end
47
+
48
+ it "is the same for Bitget::Client" do
49
+ client = Bitget::Client.new(api_key: 'a_key'){|c| c.api_secret = 'a_secret'}
50
+ _(client.api_key).must_equal('a_key')
51
+ _(client.api_secret).must_equal('a_secret')
52
+ end
53
+ end
54
+
55
+ describe "missing credentials" do
56
+ let(:credentials){{api_key: 'a_key', api_secret: 'a_secret', api_passphrase: 'a_passphrase'}}
57
+
58
+ %i{api_key api_secret api_passphrase}.each do |name|
59
+ it "raises naming #{name} before anything is sent" do
60
+ client = Bitget::V2::Client.new(**credentials.reject{|k, _| k == name})
61
+ error = _{client.spot_public_coins}.must_raise(ArgumentError)
62
+ _(error.message).must_equal("#{name} is missing.")
63
+ end
64
+ end
65
+
66
+ it "raises naming every one missing" do
67
+ client = Bitget::V2::Client.new(api_secret: 'a_secret')
68
+ error = _{client.spot_public_coins}.must_raise(ArgumentError)
69
+ _(error.message).must_equal('api_key, api_passphrase are missing.')
70
+ end
71
+ end
72
+
73
+ describe "Bitget" do
74
+ it "keeps no configuration of its own" do
75
+ %i{configure configuration configuration= reset_configuration!}.each do |name|
76
+ _(Bitget.respond_to?(name)).must_equal(false)
77
+ end
78
+ _(defined?(Bitget::Configuration)).must_be_nil
79
+ end
80
+ end
81
+
16
82
  describe "where a request is addressed" do
17
83
  # Written out rather than built from API_HOST and .path_prefix, which would
18
84
  # pass however wrong either of them was.
@@ -1101,6 +1167,18 @@ describe Bitget::V2::Client do
1101
1167
  _(log.string).must_match(/Args: \{coin: "BTC"}/)
1102
1168
  _(log.string).must_match(/Headers: .+"Content-Type" => "application\/json"/)
1103
1169
  end
1170
+
1171
+ it "masks the credentials and not the signature" do
1172
+ VCR.use_cassette("v2/spot/public/coins-when_coin_is_supplied") do
1173
+ client.spot_public_coins(coin: "BTC")
1174
+ end
1175
+ %w{ACCESS-KEY ACCESS-PASSPHRASE}.each do |name|
1176
+ _(log.string).must_include(%{"#{name}" => "[FILTERED]"})
1177
+ end
1178
+ _(log.string).wont_include(api_key)
1179
+ _(log.string).wont_include(api_passphrase)
1180
+ _(log.string).must_match(/"ACCESS-SIGN" => "[^\[]/)
1181
+ end
1104
1182
  end
1105
1183
 
1106
1184
  describe "response logging" do
data/test/helper.rb CHANGED
@@ -32,10 +32,3 @@ VCR.configure do |config|
32
32
  config.filter_sensitive_data('<API_SECRET>'){ENV['BITGET_API_SECRET'] }
33
33
  config.filter_sensitive_data('<API_PASSPHRASE>'){ENV['BITGET_API_PASSPHRASE']}
34
34
  end
35
-
36
- class Minitest::Test
37
- def before_setup
38
- super
39
- Bitget.reset_configuration!
40
- end
41
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitget.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -36,7 +36,6 @@ files:
36
36
  - Rakefile
37
37
  - bitget.rb.gemspec
38
38
  - lib/Bitget/Client.rb
39
- - lib/Bitget/Configuration.rb
40
39
  - lib/Bitget/ERROR_CODES.rb
41
40
  - lib/Bitget/Error.rb
42
41
  - lib/Bitget/V2/Client.rb
@@ -44,7 +43,6 @@ files:
44
43
  - lib/String/url_encode.rb
45
44
  - lib/Thoran/String/UrlEncode/url_encode.rb
46
45
  - lib/bitget.rb
47
- - test/Configuration_test.rb
48
46
  - test/ERROR_CODES_test.rb
49
47
  - test/Error_test.rb
50
48
  - test/V2/Client_test.rb
@@ -1,32 +0,0 @@
1
- # Bitget/Configuration.rb
2
- # Bitget::Configuration
3
-
4
- module Bitget
5
- class Configuration
6
- attr_accessor :api_key, :api_secret, :api_passphrase, :debug, :logger
7
-
8
- def initialize
9
- @api_key = nil
10
- @api_secret = nil
11
- @api_passphrase = nil
12
- @debug = false
13
- @logger = nil
14
- end
15
- end
16
-
17
- class << self
18
- attr_writer :configuration
19
-
20
- def configuration
21
- @configuration ||= Configuration.new
22
- end
23
-
24
- def configure
25
- yield(configuration)
26
- end
27
-
28
- def reset_configuration!
29
- @configuration = Configuration.new
30
- end
31
- end
32
- end
@@ -1,87 +0,0 @@
1
- require_relative './helper'
2
-
3
- describe Bitget::Configuration do
4
- describe "#initialize" do
5
- it "sets default values" do
6
- config = Bitget::Configuration.new
7
- _(config.debug).must_equal(false)
8
- _(config.api_key).must_be_nil
9
- _(config.api_secret).must_be_nil
10
- _(config.api_passphrase).must_be_nil
11
- _(config.logger).must_be_nil
12
- end
13
-
14
- it "declares a setting for everything a client reads" do
15
- _(Bitget::Configuration.new.public_methods(false).grep(/[^=]\z/).sort) \
16
- .must_equal(%i{api_key api_passphrase api_secret debug logger})
17
- end
18
- end
19
-
20
- # The credentials are named arguments and fall back upon nil, having no
21
- # meaningful nil of their own. The options are given under options: and fall
22
- # back upon the key being absent, nil being a value a caller may mean.
23
- describe "the credentials, which are named arguments" do
24
- before do
25
- Bitget.configure do |config|
26
- config.api_key = 'configured_key'
27
- config.api_secret = 'configured_secret'
28
- config.api_passphrase = 'configured_passphrase'
29
- end
30
- end
31
-
32
- it "supplies every one, so that a client needs no arguments at all" do
33
- client = Bitget::Client.new
34
- _(client.api_key).must_equal('configured_key')
35
- _(client.api_secret).must_equal('configured_secret')
36
- _(client.api_passphrase).must_equal('configured_passphrase')
37
- end
38
-
39
- it "yields to each one given, and only to that one" do
40
- _(Bitget::Client.new(api_key: 'given').api_key).must_equal('given')
41
- _(Bitget::Client.new(api_key: 'given').api_secret).must_equal('configured_secret')
42
- _(Bitget::Client.new(api_secret: 'given').api_secret).must_equal('given')
43
- _(Bitget::Client.new(api_secret: 'given').api_key).must_equal('configured_key')
44
- _(Bitget::Client.new(api_passphrase: 'given').api_passphrase).must_equal('given')
45
- _(Bitget::Client.new(api_passphrase: 'given').api_key).must_equal('configured_key')
46
- end
47
-
48
- it "is unreachable where nothing is configured and nothing is given" do
49
- Bitget.reset_configuration!
50
- client = Bitget::Client.new
51
- _(client.api_key).must_be_nil
52
- _(client.api_secret).must_be_nil
53
- _(client.api_passphrase).must_be_nil
54
- end
55
- end
56
-
57
- describe "the options, which are given under options:" do
58
- let(:logger){Logger.new(StringIO.new)}
59
-
60
- before do
61
- Bitget.configure do |config|
62
- config.debug = true
63
- config.logger = logger
64
- end
65
- end
66
-
67
- it "supplies every one" do
68
- client = Bitget::Client.new
69
- _(client.debug).must_equal(true)
70
- _(client.logger).must_equal(logger)
71
- end
72
-
73
- it "yields to each one given, and only to that one" do
74
- other = Logger.new(StringIO.new)
75
- _(Bitget::Client.new(options: {debug: false}).debug).must_equal(false)
76
- _(Bitget::Client.new(options: {debug: false}).logger).must_equal(logger)
77
- _(Bitget::Client.new(options: {logger: other}).logger).must_equal(other)
78
- _(Bitget::Client.new(options: {logger: other}).debug).must_equal(true)
79
- end
80
-
81
- it "takes nil as a value rather than as an absence" do
82
- client = Bitget::Client.new(options: {logger: nil})
83
- _(client.logger).must_be_nil
84
- _(client.send(:use_logging?)).must_equal(false)
85
- end
86
- end
87
- end