apimatic_core 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/apimatic-core/api_call.rb +18 -28
- data/lib/apimatic-core/http/configurations/http_client_configuration.rb +4 -2
- data/lib/apimatic-core/logger/configurations/api_logging_configuration.rb +97 -0
- data/lib/apimatic-core/logger/default_logger.rb +44 -0
- data/lib/apimatic-core/logger/nil_sdk_logger.rb +16 -0
- data/lib/apimatic-core/logger/sdk_logger.rb +78 -0
- data/lib/apimatic-core/request_builder.rb +0 -29
- data/lib/apimatic-core/response_handler.rb +1 -24
- data/lib/apimatic-core/utilities/constants.rb +7 -0
- data/lib/apimatic-core/utilities/logger_helper.rb +82 -0
- data/lib/apimatic_core.rb +7 -1
- metadata +8 -3
- data/lib/apimatic-core/logger/endpoint_logger.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6999e5f75a815faf7120e5f551b3ea2661e6d146a84e087426f64b27863d2fc
|
4
|
+
data.tar.gz: f9c63258f67b4798393569aa35ca04c54bd70b0109f32d2f3bd756b6aed7838d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c86684bed17cbe67d0750a78bda0bc03b2385414a8ebba30088fa7990d45ae2471426cb8c0457ead6d3d1dbcbb5a11003e435fc2105917e234b6e13cd2a292b2
|
7
|
+
data.tar.gz: 5c539fe7981f7aa5196ad81e9f0e76ffaa3d3729d536f824ab3b5c2170e5cce61300f3f6018db93785121487b18db580d0eed60c67443ed4c2b72c8445a89899
|
data/README.md
CHANGED
@@ -72,7 +72,12 @@ gem 'apimatic_core'
|
|
72
72
|
## Logger
|
73
73
|
| Name | Description |
|
74
74
|
|-------------------------------------------------------------------|--------------------------------------------|
|
75
|
-
| [`
|
75
|
+
| [`SdkLogger`](lib/apimatic-core/logger/sdk_logger.rb) | A class responsible for logging request and response of an api call|
|
76
|
+
| [`NilSdkLogger`](lib/apimatic-core/logger/nil_sdk_logger.rb) | A class responsible for no logging|
|
77
|
+
| [`ConsoleLogger`](lib/apimatic-core/logger/default_logger.rb) | Represents default implementation of logger interface|
|
78
|
+
| [`ApiLoggingConfiguration`](lib/apimatic-core/logger/api_logging_configuration.rb) | Represents logging configuration|
|
79
|
+
| [`ApiRequestLoggingConfiguration`](lib/apimatic-core/logger/api_logging_configuration.rb) | Represents request logging configuration.|
|
80
|
+
| [`ApiResponseLoggingConfiguration`](lib/apimatic-core/logger/api_logging_configuration.rb) | Represents response logging configuration.|
|
76
81
|
|
77
82
|
## Types
|
78
83
|
| Name | Description |
|
@@ -4,19 +4,17 @@ module CoreLibrary
|
|
4
4
|
# Creates a new builder instance of the API call with pre-configured global and logging configurations.
|
5
5
|
# @return [ApiCall] The instance of ApiCall object.
|
6
6
|
def new_builder
|
7
|
-
ApiCall.new(@global_configuration
|
7
|
+
ApiCall.new(@global_configuration)
|
8
8
|
end
|
9
9
|
|
10
10
|
# Initializes a new instance of ApiCall.
|
11
11
|
# @param [GlobalConfiguration] global_configuration An instance of GlobalConfiguration.
|
12
|
-
|
13
|
-
def initialize(global_configuration, logger: nil)
|
12
|
+
def initialize(global_configuration)
|
14
13
|
@global_configuration = global_configuration
|
15
14
|
@request_builder = RequestBuilder.new
|
16
15
|
@response_handler = ResponseHandler.new
|
17
|
-
@endpoint_logger = EndpointLogger.new(logger)
|
18
|
-
@endpoint_name_for_logging = nil
|
19
16
|
@endpoint_context = {}
|
17
|
+
initialize_api_logger(@global_configuration.client_configuration.logging_configuration)
|
20
18
|
end
|
21
19
|
|
22
20
|
# The setter for the request builder to be used for building the request of an API call.
|
@@ -35,14 +33,6 @@ module CoreLibrary
|
|
35
33
|
self
|
36
34
|
end
|
37
35
|
|
38
|
-
# The setter for the name of the endpoint controller method to used while logging an endpoint call.
|
39
|
-
# @param [String] endpoint_name_for_logging The name of the endpoint controller method to used while logging.
|
40
|
-
# @return [ApiCall] An updated instance of ApiCall.
|
41
|
-
def endpoint_name_for_logging(endpoint_name_for_logging)
|
42
|
-
@endpoint_name_for_logging = endpoint_name_for_logging
|
43
|
-
self
|
44
|
-
end
|
45
|
-
|
46
36
|
# The setter for the context for an endpoint call.
|
47
37
|
# @param [String] context_key The name of the endpoint context.
|
48
38
|
# @param [Object] context_value The value of the endpoint context.
|
@@ -61,45 +51,45 @@ module CoreLibrary
|
|
61
51
|
raise ArgumentError, 'An HTTP client instance is required to execute an Api call.'
|
62
52
|
end
|
63
53
|
|
64
|
-
_http_request = @request_builder.
|
65
|
-
.endpoint_name_for_logging(@endpoint_name_for_logging)
|
66
|
-
.global_configuration(@global_configuration)
|
54
|
+
_http_request = @request_builder.global_configuration(@global_configuration)
|
67
55
|
.build(@endpoint_context)
|
68
|
-
@
|
56
|
+
@logger.log_request(_http_request)
|
69
57
|
|
70
58
|
_http_callback = _client_configuration.http_callback
|
71
59
|
unless _http_callback.nil?
|
72
60
|
update_http_callback(proc do
|
73
61
|
_http_callback&.on_before_request(_http_request)
|
74
|
-
end
|
62
|
+
end)
|
75
63
|
end
|
76
|
-
|
77
64
|
_http_response = _client_configuration.http_client.execute(_http_request)
|
78
|
-
@
|
65
|
+
@logger.log_response(_http_response)
|
79
66
|
|
80
67
|
unless _http_callback.nil?
|
81
68
|
update_http_callback(proc do
|
82
69
|
_http_callback&.on_after_response(_http_response)
|
83
|
-
end
|
70
|
+
end)
|
84
71
|
end
|
85
72
|
|
86
|
-
_deserialized_response = @response_handler.
|
87
|
-
.endpoint_name_for_logging(@endpoint_name_for_logging)
|
88
|
-
.handle(_http_response, @global_configuration.get_global_errors,
|
73
|
+
_deserialized_response = @response_handler.handle(_http_response, @global_configuration.get_global_errors,
|
89
74
|
@global_configuration.should_symbolize_hash)
|
90
75
|
_deserialized_response
|
91
76
|
rescue StandardError => e
|
92
|
-
@endpoint_logger.error(e)
|
93
77
|
raise e
|
94
78
|
end
|
95
79
|
end
|
96
80
|
|
97
81
|
# Registers request and response with the provided http_callback
|
98
82
|
# @param [Callable] callable The callable to be called for registering into the HttpCallback instance.
|
99
|
-
|
100
|
-
def update_http_callback(callable, log_message)
|
101
|
-
@endpoint_logger.info(log_message)
|
83
|
+
def update_http_callback(callable)
|
102
84
|
callable.call
|
103
85
|
end
|
86
|
+
|
87
|
+
def initialize_api_logger(logging_config)
|
88
|
+
@logger = if logging_config.nil?
|
89
|
+
NilSdkLogger.new
|
90
|
+
else
|
91
|
+
SdkLogger.new(logging_config)
|
92
|
+
end
|
93
|
+
end
|
104
94
|
end
|
105
95
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module CoreLibrary
|
2
2
|
# Configuration for an HttpClient.
|
3
3
|
class HttpClientConfiguration < ClientConfiguration
|
4
|
-
attr_reader :http_client, :http_callback
|
4
|
+
attr_reader :http_client, :http_callback, :logging_configuration
|
5
5
|
|
6
6
|
# Initializes a new instance of HttpClientConfiguration.
|
7
7
|
# @param connection Connection information
|
@@ -20,7 +20,8 @@ module CoreLibrary
|
|
20
20
|
connection: nil, adapter: :net_http_persistent, timeout: 60,
|
21
21
|
max_retries: 0, retry_interval: 1, backoff_factor: 2,
|
22
22
|
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
23
|
-
retry_methods: %i[get put], cache: false, verify: true, http_callback: nil, http_client: nil
|
23
|
+
retry_methods: %i[get put], cache: false, verify: true, http_callback: nil, http_client: nil,
|
24
|
+
logging_configuration: nil
|
24
25
|
)
|
25
26
|
@response_factory = HttpResponseFactory.new
|
26
27
|
@connection = connection
|
@@ -35,6 +36,7 @@ module CoreLibrary
|
|
35
36
|
@verify = verify
|
36
37
|
@cache = cache
|
37
38
|
@http_client = http_client
|
39
|
+
@logging_configuration = logging_configuration
|
38
40
|
end
|
39
41
|
|
40
42
|
# Setter for http_client.
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module CoreLibrary
|
2
|
+
# Represents options for configuring logging behavior.
|
3
|
+
class ApiLoggingConfiguration
|
4
|
+
attr_reader :logger, :log_level, :request_logging_config, :response_logging_config, :mask_sensitive_headers
|
5
|
+
|
6
|
+
# Initializes a new instance of ApiLoggingConfiguration.
|
7
|
+
#
|
8
|
+
# @param logger [LoggerInterface] The logger to use for logging messages.
|
9
|
+
# @param log_level [LogLevel] The log level to determine which messages should be logged.
|
10
|
+
# @param request_logging_config [ApiRequestLoggingConfiguration] Options for logging HTTP requests.
|
11
|
+
# @param response_logging_config [ApiResponseLoggingConfiguration] Options for logging HTTP responses.
|
12
|
+
# @param mask_sensitive_headers [Boolean] Indicates whether sensitive headers should be masked in logged messages.
|
13
|
+
def initialize(
|
14
|
+
logger,
|
15
|
+
log_level,
|
16
|
+
request_logging_config,
|
17
|
+
response_logging_config,
|
18
|
+
mask_sensitive_headers
|
19
|
+
)
|
20
|
+
@logger = logger || ConsoleLogger.new
|
21
|
+
@log_level = log_level || ::Logger::INFO
|
22
|
+
@request_logging_config = request_logging_config
|
23
|
+
@response_logging_config = response_logging_config
|
24
|
+
@mask_sensitive_headers = mask_sensitive_headers
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Represents configuration for logging HTTP messages.
|
29
|
+
class BaseHttpLoggingConfiguration
|
30
|
+
attr_reader :log_body, :log_headers, :headers_to_exclude, :headers_to_include, :headers_to_unmask
|
31
|
+
|
32
|
+
# Initializes a new instance of BaseHttpLoggingConfiguration.
|
33
|
+
#
|
34
|
+
# @param log_body [Boolean] Indicates whether the message body should be logged. Default is false.
|
35
|
+
# @param log_headers [Boolean] Indicates whether the message headers should be logged. Default is false.
|
36
|
+
# @param headers_to_exclude [Array<String>] Array of headers not displayed in logging. Default is an empty array.
|
37
|
+
# @param headers_to_include [Array<String>] Array of headers to be displayed in logging. Default is an empty array.
|
38
|
+
# @param headers_to_unmask [Array<String>] Array of headers which values are non-sensitive to display in logging.
|
39
|
+
# Default is an empty array.
|
40
|
+
def initialize(
|
41
|
+
log_body,
|
42
|
+
log_headers,
|
43
|
+
headers_to_exclude,
|
44
|
+
headers_to_include,
|
45
|
+
headers_to_unmask
|
46
|
+
)
|
47
|
+
@log_body = log_body
|
48
|
+
@log_headers = log_headers
|
49
|
+
@headers_to_exclude = headers_to_exclude || []
|
50
|
+
@headers_to_include = headers_to_include || []
|
51
|
+
@headers_to_unmask = headers_to_unmask || []
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Represents request logging configuration.
|
56
|
+
class ApiRequestLoggingConfiguration < BaseHttpLoggingConfiguration
|
57
|
+
attr_reader :include_query_in_path
|
58
|
+
|
59
|
+
# Initializes a new instance of ApiRequestLoggingConfiguration.
|
60
|
+
#
|
61
|
+
# @param log_body [Boolean] Indicates whether the message body should be logged. Default is false.
|
62
|
+
# @param log_headers [Boolean] Indicates whether the message headers should be logged. Default is false.
|
63
|
+
# @param headers_to_exclude [Array<String>] Array of headers not displayed in logging. Default is an empty array.
|
64
|
+
# @param headers_to_include [Array<String>] Array of headers to be displayed in logging. Default is an empty array.
|
65
|
+
# @param headers_to_unmask [Array<String>] Array of headers which values are non-sensitive to display in logging.
|
66
|
+
# Default is an empty array.
|
67
|
+
def initialize(
|
68
|
+
log_body,
|
69
|
+
log_headers,
|
70
|
+
headers_to_exclude,
|
71
|
+
headers_to_include,
|
72
|
+
headers_to_unmask,
|
73
|
+
include_query_in_path
|
74
|
+
)
|
75
|
+
super(
|
76
|
+
log_body,
|
77
|
+
log_headers,
|
78
|
+
headers_to_exclude,
|
79
|
+
headers_to_include,
|
80
|
+
headers_to_unmask
|
81
|
+
)
|
82
|
+
@include_query_in_path = include_query_in_path
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Represents response logging configuration.
|
87
|
+
class ApiResponseLoggingConfiguration < BaseHttpLoggingConfiguration
|
88
|
+
# Initializes a new instance of ApiResponseLoggingConfiguration.
|
89
|
+
#
|
90
|
+
# @param log_body [Boolean] Indicates whether the message body should be logged. Default is false.
|
91
|
+
# @param log_headers [Boolean] Indicates whether the message headers should be logged. Default is false.
|
92
|
+
# @param headers_to_exclude [Array<String>] Array of headers not displayed in logging. Default is an empty array.
|
93
|
+
# @param headers_to_include [Array<String>] Array of headers to be displayed in logging. Default is an empty array.
|
94
|
+
# @param headers_to_unmask [Array<String>] Array of headers which values are non-sensitive to display in logging.
|
95
|
+
# Default is an empty array.
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Represents a logger implementation that logs messages to the console.
|
2
|
+
class ConsoleLogger < Logger
|
3
|
+
# Initializes a new instance of DefaultLogger.
|
4
|
+
def initialize
|
5
|
+
@logger = ::Logger.new($stdout)
|
6
|
+
@logger.formatter = method(:format_log_message)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Logs a message to the console with the specified log level.
|
10
|
+
# @param level [Symbol] The log level of the message.
|
11
|
+
# @param message [String] The message to log.
|
12
|
+
# @param params [Hash] Additional parameters to include in the log message.
|
13
|
+
def log(level, message, params = {})
|
14
|
+
formatted_message = message_with_params(message, params)
|
15
|
+
case level
|
16
|
+
when Logger::DEBUG
|
17
|
+
@logger.debug(formatted_message)
|
18
|
+
when Logger::INFO
|
19
|
+
@logger.info(formatted_message)
|
20
|
+
when Logger::WARN
|
21
|
+
@logger.warn(formatted_message)
|
22
|
+
when Logger::ERROR
|
23
|
+
@logger.error(formatted_message)
|
24
|
+
when Logger::FATAL
|
25
|
+
@logger.fatal(formatted_message)
|
26
|
+
else
|
27
|
+
@logger.unknown(formatted_message)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_log_message(severity, _datetime, _progname, msg)
|
32
|
+
"#{
|
33
|
+
severity.ljust(5) +
|
34
|
+
msg
|
35
|
+
}\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
def message_with_params(message, params)
|
39
|
+
message.gsub(/\{([\w-]+)\}/) do |match|
|
40
|
+
key = match[1..-2]
|
41
|
+
params[key.to_sym] || params[key.to_s]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CoreLibrary
|
2
|
+
# This class is nil implementation of ApiLogger.
|
3
|
+
class NilSdkLogger < ApiLogger
|
4
|
+
# Logs the details of an HTTP request.
|
5
|
+
# @param request [HttpRequest] The HTTP request to log.
|
6
|
+
def log_request(request)
|
7
|
+
# This function is intentionally left empty because this logger does not perform any logging.
|
8
|
+
end
|
9
|
+
|
10
|
+
# Logs the details of an HTTP response.
|
11
|
+
# @param response [HttpResponse] The HTTP response to log.
|
12
|
+
def log_response(response)
|
13
|
+
# This function is intentionally left empty because this logger does not perform any logging.
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module CoreLibrary
|
2
|
+
# This class is responsible for logging request and response info.
|
3
|
+
class SdkLogger < ApiLogger
|
4
|
+
def initialize(logging_config)
|
5
|
+
@log_level = logging_config.log_level
|
6
|
+
@logger = logging_config.logger
|
7
|
+
@request_logging_config = logging_config.request_logging_config
|
8
|
+
@response_logging_config = logging_config.response_logging_config
|
9
|
+
@mask_sensitive_headers = logging_config.mask_sensitive_headers
|
10
|
+
end
|
11
|
+
|
12
|
+
def log_request(request)
|
13
|
+
content_type_header = LoggerHelper.get_content_type(request.headers)
|
14
|
+
url = @request_logging_config.include_query_in_path ? request.query_url : request.query_url.split('?').first
|
15
|
+
|
16
|
+
@logger.log(@log_level, "Request {#{METHOD}} {#{URL}} {#{CONTENT_TYPE_HEADER}}", {
|
17
|
+
METHOD => request.http_method,
|
18
|
+
URL => url,
|
19
|
+
CONTENT_TYPE_HEADER => content_type_header
|
20
|
+
})
|
21
|
+
|
22
|
+
apply_log_request_options(request)
|
23
|
+
end
|
24
|
+
|
25
|
+
def log_response(response)
|
26
|
+
content_type_header = LoggerHelper.get_content_type(response.headers)
|
27
|
+
content_length_header = LoggerHelper.get_content_length(response.headers)
|
28
|
+
|
29
|
+
@logger.log(@log_level, "Response {#{STATUS_CODE}} {#{CONTENT_LENGTH_HEADER}} {#{CONTENT_TYPE_HEADER}}", {
|
30
|
+
STATUS_CODE => response.status_code,
|
31
|
+
CONTENT_LENGTH_HEADER => content_length_header,
|
32
|
+
CONTENT_TYPE_HEADER => content_type_header
|
33
|
+
})
|
34
|
+
|
35
|
+
apply_log_response_options(response)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def apply_log_request_options(request)
|
41
|
+
headers_to_log = LoggerHelper.extract_headers_to_log(
|
42
|
+
@request_logging_config,
|
43
|
+
request.headers
|
44
|
+
)
|
45
|
+
|
46
|
+
if @request_logging_config.log_headers
|
47
|
+
@logger.log(@log_level, 'Request headers {headers}', {
|
48
|
+
headers: headers_to_log
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
return unless @request_logging_config.log_body
|
53
|
+
|
54
|
+
@logger.log(@log_level, 'Request body {body}', {
|
55
|
+
body: request.parameters
|
56
|
+
})
|
57
|
+
end
|
58
|
+
|
59
|
+
def apply_log_response_options(response)
|
60
|
+
headers_to_log = LoggerHelper.extract_headers_to_log(
|
61
|
+
@response_logging_config,
|
62
|
+
response.headers
|
63
|
+
)
|
64
|
+
|
65
|
+
if @response_logging_config.log_headers
|
66
|
+
@logger.log(@log_level, 'Response headers {headers}', {
|
67
|
+
headers: headers_to_log
|
68
|
+
})
|
69
|
+
end
|
70
|
+
|
71
|
+
return unless @response_logging_config.log_body
|
72
|
+
|
73
|
+
@logger.log(@log_level, 'Response body {body}', {
|
74
|
+
body: response.raw_body
|
75
|
+
})
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -18,8 +18,6 @@ module CoreLibrary
|
|
18
18
|
@auth = nil
|
19
19
|
@array_serialization_format = ArraySerializationFormat::INDEXED
|
20
20
|
@xml_attributes = nil
|
21
|
-
@endpoint_name_for_logging = nil
|
22
|
-
@endpoint_logger = nil
|
23
21
|
end
|
24
22
|
|
25
23
|
# The setter for the server.
|
@@ -154,22 +152,6 @@ module CoreLibrary
|
|
154
152
|
self
|
155
153
|
end
|
156
154
|
|
157
|
-
# The setter for the name of the endpoint controller method to used while logging an endpoint call.
|
158
|
-
# @param [String] endpoint_name_for_logging The name of the endpoint controller method to used while logging.
|
159
|
-
# @return [RequestBuilder] An updated instance of RequestBuilder.
|
160
|
-
def endpoint_name_for_logging(endpoint_name_for_logging)
|
161
|
-
@endpoint_name_for_logging = endpoint_name_for_logging
|
162
|
-
self
|
163
|
-
end
|
164
|
-
|
165
|
-
# The setter for the name of the endpoint controller method to used while logging an endpoint call.
|
166
|
-
# @param [EndpointLogger] endpoint_logger The name of the endpoint controller method to used while logging.
|
167
|
-
# @return [RequestBuilder] An updated instance of RequestBuilder.
|
168
|
-
def endpoint_logger(endpoint_logger)
|
169
|
-
@endpoint_logger = endpoint_logger
|
170
|
-
self
|
171
|
-
end
|
172
|
-
|
173
155
|
# Sets global configuration object for the request builder.
|
174
156
|
def global_configuration(global_configuration)
|
175
157
|
@global_configuration = global_configuration
|
@@ -195,7 +177,6 @@ module CoreLibrary
|
|
195
177
|
# Processes and resolves the endpoint URL.
|
196
178
|
# @return [String] The processed URL.
|
197
179
|
def process_url
|
198
|
-
@endpoint_logger.info("Preparing query URL for #{@endpoint_name_for_logging}.")
|
199
180
|
_base_url = @global_configuration.get_base_uri_executor.call(@server)
|
200
181
|
_updated_url_with_template_params = ApiHelper.append_url_with_template_parameters(@path, @template_params)
|
201
182
|
_url = _base_url + _updated_url_with_template_params
|
@@ -233,10 +214,6 @@ module CoreLibrary
|
|
233
214
|
_has_additional_headers = !_additional_headers.nil? && _additional_headers.any?
|
234
215
|
_has_local_headers = !@header_params.nil? and @header_params.any?
|
235
216
|
|
236
|
-
if _has_global_headers || _has_additional_headers || _has_local_headers
|
237
|
-
@endpoint_logger.info("Preparing headers for #{@endpoint_name_for_logging}.")
|
238
|
-
end
|
239
|
-
|
240
217
|
_request_headers.merge!(_global_headers) if _has_global_headers
|
241
218
|
_request_headers.merge!(_additional_headers) if _has_additional_headers
|
242
219
|
|
@@ -258,12 +235,6 @@ module CoreLibrary
|
|
258
235
|
_has_body_serializer = !@body_serializer.nil?
|
259
236
|
_has_xml_attributes = !@xml_attributes.nil?
|
260
237
|
|
261
|
-
if _has_form_params || _has_additional_form_params
|
262
|
-
@endpoint_logger.info("Preparing form parameters for #{@endpoint_name_for_logging}.")
|
263
|
-
elsif _has_body_param
|
264
|
-
@endpoint_logger.info("Preparing body parameters for #{@endpoint_name_for_logging}.")
|
265
|
-
end
|
266
|
-
|
267
238
|
if _has_xml_attributes
|
268
239
|
return process_xml_parameters
|
269
240
|
elsif _has_form_params || _has_additional_form_params || _has_multipart_param
|
@@ -12,8 +12,6 @@ module CoreLibrary
|
|
12
12
|
@datetime_format = nil
|
13
13
|
@is_xml_response = false
|
14
14
|
@xml_attribute = nil
|
15
|
-
@endpoint_name_for_logging = nil
|
16
|
-
@endpoint_logger = nil
|
17
15
|
@is_primitive_response = false
|
18
16
|
@is_date_response = false
|
19
17
|
@is_response_array = false
|
@@ -84,22 +82,6 @@ module CoreLibrary
|
|
84
82
|
self
|
85
83
|
end
|
86
84
|
|
87
|
-
# Sets the endpoint_name_for_logging property.
|
88
|
-
# @param [String] endpoint_name_for_logging The endpoint method name to be used while logging.
|
89
|
-
# @return [ResponseHandler] An updated instance of ResponseHandler.
|
90
|
-
def endpoint_name_for_logging(endpoint_name_for_logging)
|
91
|
-
@endpoint_name_for_logging = endpoint_name_for_logging
|
92
|
-
self
|
93
|
-
end
|
94
|
-
|
95
|
-
# Sets endpoint logger to be used.
|
96
|
-
# @param [EndpointLogger] endpoint_logger The logger to be used for logging API call steps.
|
97
|
-
# @return [ResponseHandler] An updated instance of ResponseHandler.
|
98
|
-
def endpoint_logger(endpoint_logger)
|
99
|
-
@endpoint_logger = endpoint_logger
|
100
|
-
self
|
101
|
-
end
|
102
|
-
|
103
85
|
# Sets the is_primitive_response property.
|
104
86
|
# @param [Boolean] is_primitive_response Flag if the response is of primitive type.
|
105
87
|
# @return [ResponseHandler] An updated instance of ResponseHandler.
|
@@ -164,13 +146,8 @@ module CoreLibrary
|
|
164
146
|
# @param [Boolean] should_symbolize_hash Flag to symbolize the hash during response deserialization.
|
165
147
|
# @return [Object] The deserialized response of the API Call.
|
166
148
|
def handle(response, global_errors, should_symbolize_hash = false)
|
167
|
-
@endpoint_logger.info("Validating response for #{@endpoint_name_for_logging}.")
|
168
|
-
|
169
149
|
# checking Nullify 404
|
170
|
-
if response.status_code == 404 && @is_nullify404
|
171
|
-
@endpoint_logger.info("Status code 404 received for #{@endpoint_name_for_logging}. Returning None.")
|
172
|
-
return nil
|
173
|
-
end
|
150
|
+
return nil if response.status_code == 404 && @is_nullify404
|
174
151
|
|
175
152
|
# validating response if configured
|
176
153
|
validate(response, global_errors)
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module CoreLibrary
|
2
|
+
# logger helper methods.
|
3
|
+
class LoggerHelper
|
4
|
+
NON_SENSITIVE_HEADERS = %w[
|
5
|
+
accept accept-charset accept-encoding accept-language access-control-allow-origin
|
6
|
+
cache-control connection content-encoding content-language content-length
|
7
|
+
content-location content-md5 content-range content-type date etag expect
|
8
|
+
expires from host if-match if-modified-since if-none-match if-range
|
9
|
+
if-unmodified-since keep-alive last-modified location max-forwards pragma
|
10
|
+
range referer retry-after server trailer transfer-encoding upgrade user-agent
|
11
|
+
vary via warning x-forwarded-for x-requested-with x-powered-by
|
12
|
+
].map(&:downcase).freeze
|
13
|
+
|
14
|
+
def self.get_content_type(headers)
|
15
|
+
return '' if headers.nil?
|
16
|
+
|
17
|
+
headers.find { |key, _| key.downcase == CONTENT_TYPE_HEADER }&.last || ''
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.get_content_length(headers)
|
21
|
+
return '' if headers.nil?
|
22
|
+
|
23
|
+
headers.find { |key, _| key.downcase == CONTENT_LENGTH_HEADER }&.last || ''
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.extract_headers_to_log(http_logging_config, headers)
|
27
|
+
return headers if headers.nil?
|
28
|
+
|
29
|
+
filtered_headers = if http_logging_config.headers_to_include.any?
|
30
|
+
include_headers(headers, http_logging_config.headers_to_include)
|
31
|
+
elsif http_logging_config.headers_to_exclude.any?
|
32
|
+
exclude_headers(headers, http_logging_config.headers_to_exclude)
|
33
|
+
else
|
34
|
+
headers
|
35
|
+
end
|
36
|
+
|
37
|
+
mask_sensitive_headers(filtered_headers, http_logging_config.headers_to_unmask)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.include_headers(headers, headers_to_include)
|
41
|
+
included_headers = {}
|
42
|
+
|
43
|
+
headers_to_include.each do |name|
|
44
|
+
key = headers.keys.find { |header_key| header_key.downcase == name.downcase }
|
45
|
+
included_headers[key] = headers[key] if headers[key]
|
46
|
+
end
|
47
|
+
|
48
|
+
included_headers
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.exclude_headers(headers, headers_to_exclude)
|
52
|
+
excluded_headers = {}
|
53
|
+
|
54
|
+
headers.each do |key, val|
|
55
|
+
excluded_headers[key] = val unless headers_to_exclude.any? do |excluded_name|
|
56
|
+
excluded_name.downcase == key.downcase
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
excluded_headers
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.mask_sensitive_headers(headers, headers_to_unmask)
|
64
|
+
return headers unless @mask_sensitive_headers || !headers.nil?
|
65
|
+
|
66
|
+
headers.each do |key, val|
|
67
|
+
headers[key] = mask_if_sensitive_header(key, val, headers_to_unmask)
|
68
|
+
end
|
69
|
+
|
70
|
+
headers
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.mask_if_sensitive_header(name, value, headers_to_unmask)
|
74
|
+
headers_to_unmask ||= []
|
75
|
+
headers_to_unmask = headers_to_unmask.map(&:downcase)
|
76
|
+
name_downcase = name.downcase
|
77
|
+
|
78
|
+
NON_SENSITIVE_HEADERS.include?(name_downcase) || headers_to_unmask.include?(name_downcase) ?
|
79
|
+
value : '**Redacted**'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/apimatic_core.rb
CHANGED
@@ -3,6 +3,7 @@ require 'json'
|
|
3
3
|
require 'certifi'
|
4
4
|
require 'apimatic_core_interfaces'
|
5
5
|
require 'cgi'
|
6
|
+
require 'logger'
|
6
7
|
|
7
8
|
require_relative 'apimatic-core/request_builder'
|
8
9
|
require_relative 'apimatic-core/response_handler'
|
@@ -16,7 +17,10 @@ require_relative 'apimatic-core/exceptions/auth_validation_exception'
|
|
16
17
|
require_relative 'apimatic-core/exceptions/any_of_validation_exception'
|
17
18
|
require_relative 'apimatic-core/exceptions/one_of_validation_exception'
|
18
19
|
|
19
|
-
require_relative 'apimatic-core/logger/
|
20
|
+
require_relative 'apimatic-core/logger/default_logger'
|
21
|
+
require_relative 'apimatic-core/logger/nil_sdk_logger'
|
22
|
+
require_relative 'apimatic-core/logger/sdk_logger'
|
23
|
+
require_relative 'apimatic-core/logger/configurations/api_logging_configuration'
|
20
24
|
|
21
25
|
require_relative 'apimatic-core/http/configurations/http_client_configuration'
|
22
26
|
require_relative 'apimatic-core/http/request/http_request'
|
@@ -44,6 +48,8 @@ require_relative 'apimatic-core/utilities/xml_helper'
|
|
44
48
|
require_relative 'apimatic-core/utilities/auth_helper'
|
45
49
|
require_relative 'apimatic-core/utilities/union_type_helper'
|
46
50
|
require_relative 'apimatic-core/utilities/json_pointer_helper'
|
51
|
+
require_relative 'apimatic-core/utilities/logger_helper'
|
52
|
+
require_relative 'apimatic-core/utilities/constants'
|
47
53
|
|
48
54
|
require_relative 'apimatic-core/authentication/header_auth'
|
49
55
|
require_relative 'apimatic-core/authentication/query_auth'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apimatic_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- APIMatic Ltd.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: apimatic_core_interfaces
|
@@ -173,7 +173,10 @@ files:
|
|
173
173
|
- lib/apimatic-core/http/request/http_request.rb
|
174
174
|
- lib/apimatic-core/http/response/api_response.rb
|
175
175
|
- lib/apimatic-core/http/response/http_response.rb
|
176
|
-
- lib/apimatic-core/logger/
|
176
|
+
- lib/apimatic-core/logger/configurations/api_logging_configuration.rb
|
177
|
+
- lib/apimatic-core/logger/default_logger.rb
|
178
|
+
- lib/apimatic-core/logger/nil_sdk_logger.rb
|
179
|
+
- lib/apimatic-core/logger/sdk_logger.rb
|
177
180
|
- lib/apimatic-core/request_builder.rb
|
178
181
|
- lib/apimatic-core/response_handler.rb
|
179
182
|
- lib/apimatic-core/types/error_case.rb
|
@@ -190,9 +193,11 @@ files:
|
|
190
193
|
- lib/apimatic-core/utilities/api_helper.rb
|
191
194
|
- lib/apimatic-core/utilities/auth_helper.rb
|
192
195
|
- lib/apimatic-core/utilities/comparison_helper.rb
|
196
|
+
- lib/apimatic-core/utilities/constants.rb
|
193
197
|
- lib/apimatic-core/utilities/date_time_helper.rb
|
194
198
|
- lib/apimatic-core/utilities/file_helper.rb
|
195
199
|
- lib/apimatic-core/utilities/json_pointer_helper.rb
|
200
|
+
- lib/apimatic-core/utilities/logger_helper.rb
|
196
201
|
- lib/apimatic-core/utilities/union_type_helper.rb
|
197
202
|
- lib/apimatic-core/utilities/xml_helper.rb
|
198
203
|
- lib/apimatic_core.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module CoreLibrary
|
2
|
-
# This class is responsible for logging info messages, debug messages, and errors.
|
3
|
-
class EndpointLogger
|
4
|
-
attr_reader :logger
|
5
|
-
|
6
|
-
# Initializes a new instance of EndpointLogger.
|
7
|
-
# @param logger A logger with methods info, debug and error.
|
8
|
-
def initialize(logger)
|
9
|
-
@logger = logger
|
10
|
-
end
|
11
|
-
|
12
|
-
# Logs the info message.
|
13
|
-
# @param [String] info_message The message to be logged.
|
14
|
-
def info(info_message)
|
15
|
-
@logger&.info(info_message)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Logs the debug message.
|
19
|
-
# @param [String] debug_message The message to be logged.
|
20
|
-
def debug(debug_message)
|
21
|
-
@logger&.debug(debug_message)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Logs the error.
|
25
|
-
# @param [Exception] error The exception to be logged.
|
26
|
-
def error(error)
|
27
|
-
@logger&.error(error)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|