Skip to content
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

Dist-10392 add logging functionality #55

Merged
merged 23 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ JSON.parse(response.body)

You can optionally use Amazon's sandbox environment by specifying `client = MuffinMan::Solicitations.new(credentials, sandbox = true)`

### Set Custom Logger

By default MuffinMan will log to standard out. To customize the logger used:

```ruby
MuffinMan.configure do |config|
config.logger = Logger.new('log/sp-api.log')
end
```

### Access Token Caching

You can save and retrieve the LWA refresh token by defining a lambda in your initializers.
Expand Down Expand Up @@ -94,6 +104,10 @@ auth_code = resp['payload']['authorizationCode']
refresh_token = MuffinMan::Lwa::AuthHelper.get_refresh_token(CLIENT_ID, CLIENT_SECRET, auth_code)
```

### Debugging

To use Typheous' verbose mode set env variable `MUFFIN_MAN_DEBUG=true`

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/patterninc/muffin_man. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/patterninc/muffin_man/blob/master/CODE_OF_CONDUCT.md).
Expand Down
8 changes: 6 additions & 2 deletions lib/muffin_man.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ def initialize(auth_response)
end

class << self
attr_accessor :configuration
attr_accessor :configuration, :logger
end

def self.configure
self.configuration ||= Configuration.new
yield(configuration)
self.logger = configuration.logger if configuration.logger
end

class Configuration
attr_accessor :save_access_token, :get_access_token
attr_accessor :save_access_token, :get_access_token, :logger
end
end

# Set default logger
MuffinMan.logger = Logger.new($stdout)
18 changes: 18 additions & 0 deletions lib/muffin_man/enable_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module MuffinMan
module EnableLogger
LOGGING_ENABLED = true

def log_request_and_response(level, res)
log_info = "REQUEST\n
canonical_uri:#{canonical_uri}\n\n
query_params:#{query_params}\n\n
RESPONSE\n
response_headers=#{res.headers}\n\n
response_body=#{res.body}\n\n
pp-surbhigupta marked this conversation as resolved.
Show resolved Hide resolved
"
MuffinMan.logger.send(level, log_info)
end
end
end
4 changes: 4 additions & 0 deletions lib/muffin_man/finances/v0.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "muffin_man/enable_logger"

module MuffinMan
module Finances
class V0 < SpApiClient
include EnableLogger

def list_financial_event_groups(max_results_per_page = nil, financial_event_group_started_before = nil, financial_event_group_started_after = nil, next_token = nil)
@local_var_path = "/finances/v0/financialEventGroups"
@query_params = {}
Expand Down
8 changes: 7 additions & 1 deletion lib/muffin_man/sp_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ def initialize(credentials, sandbox = false)
private

def call_api
Typhoeus.send(request_type.downcase.to_sym, request.url, request_opts)
res = Typhoeus.send(request_type.downcase.to_sym, request.url, request_opts)
if self.class.const_defined?("LOGGING_ENABLED")
level = res.code.to_s.match?(/2\d{2}/) ? :info : :error
log_request_and_response(level, res)
end
res
rescue SpApiAuthError => e
e.auth_response
end

def request_opts
opts = { headers: headers }
opts[:verbose] = true if ENV.fetch("MUFFIN_MAN_DEBUG", nil) == "true"
opts[:body] = request_body.to_json if request_body && request_type != "GET" && request_type != "HEAD"
opts
end
Expand Down
Loading