1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- # frozen_string_literal: true
- module ActiveResource
- class DetailedLogSubscriber < ActiveSupport::LogSubscriber
- VERSION_EOL_WARNING_HEADER = 'x-shopify-api-version-warning'
- VERSION_DEPRECATION_HEADER = 'x-shopify-api-deprecated-reason'
- def request(event)
- log_request_response_details(event)
- warn_on_deprecated_header_or_version_eol_header(event)
- end
- def logger
- ActiveResource::Base.logger
- end
- private
- def log_request_response_details(event)
- data = event.payload[:data]
- headers = data.extract_options!
- request_body = data.first
- info("Request:\n#{request_body}") if request_body
- info("Headers: #{headers.inspect}")
- info("Response:\n#{event.payload[:response].body}")
- end
- def warn_on_deprecated_header_or_version_eol_header(event)
- payload = event.payload
- payload[:response].each do |header_name, header_value|
- case header_name.downcase
- when VERSION_DEPRECATION_HEADER
- warning_message = <<-MSG
- [DEPRECATED] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call made
- use of a deprecated endpoint, behaviour, or parameter. See #{header_value} for more details.
- MSG
- warn(warning_message)
- when VERSION_EOL_WARNING_HEADER
- warning_message = <<-MSG
- [API Version Warning] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call used
- an API version that is unsupported or will become unsupported within 30 days. See #{header_value} for more details.
- MSG
- warn(warning_message)
- end
- end
- end
- end
- end
- ActiveResource::DetailedLogSubscriber.attach_to(:active_resource_detailed)
|