Skip to content

Commit c65d92c

Browse files
committed
Add Verbose option
This change turns off request and response logging by default and allows users to turn that back on by setting a new `verbose` option to true.
1 parent c728ff5 commit c65d92c

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ v1.7.0
22
- Drop support for ruby 2.7
33
- Add support for ruby 3.2+
44
- Require minimum version of faraday 2.0
5+
- Disabled request/response logging by default. Old behavior can be restored
6+
by setting a new `verbose` configuration option to true
57

68
v1.6.3
79
- Add support for sending end user ip address in request headers

lib/spark_api/configuration.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Configuration
99
end
1010

1111
# valid configuration options
12-
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
13-
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14-
:auth_endpoint, :callback, :compress, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address].freeze
12+
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
13+
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14+
:auth_endpoint, :callback, :compress, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address, :verbose].freeze
1515
OAUTH2_KEYS = [:authorization_uri, :access_uri, :client_id, :client_secret,
1616
# Requirements for authorization_code grant type
1717
:redirect_uri,
@@ -47,6 +47,7 @@ module Configuration
4747
DEFAULT_DICTIONARY_VERSION = nil
4848
DEFAULT_REQUEST_ID_CHAIN = nil
4949
DEFAULT_USER_IP_ADDRESS = nil
50+
DEFAULT_VERBOSE = false
5051

5152
X_SPARK_API_USER_AGENT = "X-SparkApi-User-Agent"
5253
X_USER_IP_ADDRESS = "X-User-IP-Address"
@@ -85,6 +86,7 @@ def reset_configuration
8586
self.dictionary_version = DEFAULT_DICTIONARY_VERSION
8687
self.request_id_chain = DEFAULT_REQUEST_ID_CHAIN
8788
self.user_ip_address = DEFAULT_USER_IP_ADDRESS
89+
self.verbose = DEFAULT_VERBOSE
8890
self
8991
end
9092
end

lib/spark_api/connection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def connection(force_ssl = false)
4040
conn.options[:timeout] = self.timeout
4141
conn.adapter Faraday.default_adapter
4242
end
43-
SparkApi.logger.debug { "Connection: #{conn.inspect}" }
43+
if self.verbose
44+
SparkApi.logger.debug { "Connection: #{conn.inspect}" }
45+
end
4446
conn
4547
end
4648

lib/spark_api/faraday_middleware.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def on_complete(env)
1818
env[:body] = decompress_body(env)
1919

2020
body = MultiJson.decode(env[:body])
21-
SparkApi.logger.debug{ "Response Body: #{body.inspect}" }
21+
if SparkApi.verbose
22+
SparkApi.logger.debug{ "Response Body: #{body.inspect}" }
23+
end
2224
unless body.is_a?(Hash) && body.key?("D")
2325
raise InvalidResponse, "The server response could not be understood"
2426
end

lib/spark_api/request.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def request(method, path, body, options)
9393
response = authenticator.request(method, request_path, nil, request_opts)
9494
else
9595
post_data = process_request_body(body)
96-
SparkApi.logger.debug { "#{method.to_s.upcase} Data: #{post_data}" }
96+
if self.verbose
97+
SparkApi.logger.debug { "#{method.to_s.upcase} Data: #{post_data}" }
98+
end
9799
response = authenticator.request(method, request_path, post_data, request_opts)
98100
end
99101
request_time = Time.now - start_time

spec/unit/spark_api/configuration_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
expect(SparkApi.request_id_chain).to be_nil
1717
expect(SparkApi.user_ip_address).to be_nil
1818
expect(SparkApi.middleware).to eq('spark_api')
19+
expect(SparkApi.verbose).to be false
1920
end
2021
end
2122

@@ -28,7 +29,8 @@
2829
:endpoint => "http://api.wade.dev.fbsdata.com",
2930
:timeout => 15,
3031
:request_id_chain => 'foobar',
31-
:user_ip_address => 'barfoo')
32+
:user_ip_address => 'barfoo',
33+
:verbose => true)
3234

3335
expect(client.api_key).to match("key_of_wade")
3436
expect(client.api_secret).to match("TopSecret")
@@ -39,6 +41,7 @@
3941
expect(client.timeout).to eq(15)
4042
expect(client.request_id_chain).to eq('foobar')
4143
expect(client.user_ip_address).to eq('barfoo')
44+
expect(client.verbose).to be true
4245
end
4346

4447
it "should allow unverified ssl certificates when verification is off" do
@@ -101,6 +104,7 @@
101104
config.endpoint = "test.api.sparkapi.com"
102105
config.user_agent = "my useragent"
103106
config.timeout = 15
107+
config.verbose = true
104108
end
105109

106110
expect(SparkApi.api_key).to match("my_key")
@@ -111,6 +115,7 @@
111115
expect(SparkApi.user_agent).to match("my useragent")
112116
expect(SparkApi.oauth2_enabled?()).to be false
113117
expect(SparkApi.timeout).to eq(15)
118+
expect(SparkApi.verbose).to be true
114119
end
115120

116121
it "should correctly set up the client for oauth2" do

0 commit comments

Comments
 (0)