Skip to content

Commit feae8fc

Browse files
authored
bug: Fixed RestClient logger issue (#91)
#70 introduced a bug that would cause requests to fail because of a misconfigured logger, regardless of whether the gem enabled logging.
1 parent b5f5a13 commit feae8fc

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

lib/vero/http_client.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Vero::HttpClient
33

44
def initialize(http_timeout:, logger: nil)
55
@http_timeout = http_timeout
6-
setup_logging!(logger) if logger
6+
@logger = logger
77
end
88

99
def get(url, headers = {})
@@ -19,22 +19,35 @@ def put(url, body, headers = {})
1919
end
2020

2121
def do_request(method, url, body = nil, headers = {})
22-
request_params = {method: method, url: url, headers: default_headers.merge(headers), timeout: @http_timeout}
23-
request_params[:payload] = JSON.dump(body) unless method == :get
24-
RestClient::Request.execute(request_params)
22+
params = request_params(method, url, body, headers)
23+
24+
log_request(params, body) if @logger
25+
26+
req = RestClient::Request.new(params)
27+
req.execute
2528
end
2629

2730
private
2831

32+
def request_params(method, url, body = nil, headers = {})
33+
params = {method: method, url: url, headers: default_headers.merge(headers), timeout: @http_timeout}
34+
params[:payload] = JSON.dump(body) unless method == :get
35+
36+
params
37+
end
38+
2939
def default_headers
3040
{content_type: :json, accept: :json}
3141
end
3242

33-
def setup_logging!(logger)
34-
RestClient.log = Object.new.tap do |proxy|
35-
def proxy.<<(message)
36-
logger.info message
37-
end
43+
def log_request(params, body)
44+
# Clone params to avoid modifying the original
45+
log_params = params.dup
46+
47+
if log_params.key?(:payload)
48+
log_params[:payload] = body.merge(tracking_api_key: "[FILTERED]").to_json
3849
end
50+
51+
@logger.info("Request: #{log_params.inspect}")
3952
end
4053
end

0 commit comments

Comments
 (0)