apimatic_core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +26 -0
  3. data/README.md +101 -0
  4. data/lib/apimatic-core/api_call.rb +103 -0
  5. data/lib/apimatic-core/authentication/header_auth.rb +21 -0
  6. data/lib/apimatic-core/authentication/multiple/and_auth_group.rb +28 -0
  7. data/lib/apimatic-core/authentication/multiple/auth_group.rb +45 -0
  8. data/lib/apimatic-core/authentication/multiple/or_auth_group.rb +29 -0
  9. data/lib/apimatic-core/authentication/multiple/single_auth.rb +48 -0
  10. data/lib/apimatic-core/authentication/query_auth.rb +21 -0
  11. data/lib/apimatic-core/configurations/global_configuration.rb +149 -0
  12. data/lib/apimatic-core/exceptions/invalid_auth_credential.rb +5 -0
  13. data/lib/apimatic-core/factories/http_response_factory.rb +14 -0
  14. data/lib/apimatic-core/http/configurations/http_client_configuration.rb +32 -0
  15. data/lib/apimatic-core/http/request/http_request.rb +50 -0
  16. data/lib/apimatic-core/http/response/api_response.rb +35 -0
  17. data/lib/apimatic-core/http/response/http_response.rb +24 -0
  18. data/lib/apimatic-core/logger/endpoint_logger.rb +28 -0
  19. data/lib/apimatic-core/request_builder.rb +361 -0
  20. data/lib/apimatic-core/response_handler.rb +269 -0
  21. data/lib/apimatic-core/types/error_case.rb +37 -0
  22. data/lib/apimatic-core/types/parameter.rb +116 -0
  23. data/lib/apimatic-core/types/sdk/api_exception.rb +15 -0
  24. data/lib/apimatic-core/types/sdk/base_model.rb +22 -0
  25. data/lib/apimatic-core/types/sdk/file_wrapper.rb +11 -0
  26. data/lib/apimatic-core/types/sdk/validation_exception.rb +10 -0
  27. data/lib/apimatic-core/types/xml_attributes.rb +37 -0
  28. data/lib/apimatic-core/utilities/api_helper.rb +551 -0
  29. data/lib/apimatic-core/utilities/auth_helper.rb +49 -0
  30. data/lib/apimatic-core/utilities/comparison_helper.rb +77 -0
  31. data/lib/apimatic-core/utilities/date_time_helper.rb +126 -0
  32. data/lib/apimatic-core/utilities/file_helper.rb +21 -0
  33. data/lib/apimatic-core/utilities/xml_helper.rb +215 -0
  34. data/lib/apimatic_core.rb +44 -0
  35. metadata +215 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c581361453ae63c619ca7b7bcc5b2e4cb5cd2afe4ab1f7584462a0698ab8be3d
4
+ data.tar.gz: 51c745d2ac8600da653324628f1cf4e00e2059ceb12e22f150f49ec2ed3a9f03
5
+ SHA512:
6
+ metadata.gz: 1a87d5f62193c6f46e4fda2289d5c4a8cae5d716ee35723f030d6ad97ffc569ffec23019e89065a5882ea806aec791ea121bc8f0e326496a923a49feb0c1b484
7
+ data.tar.gz: 44acde48930a6e621608d30fb646e9f467e0fba89c07520016e55e2b017b17f239ede9bcc0de1e79537e5303da469b91f1fd2f1844a212f10bd92a40b48cd103
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ APIMATIC REFERENCE SOURCE LICENSE
2
+
3
+ This license governs the use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
4
+
5
+ 1. Definitions
6
+ The terms "reproduce," "reproduction" and "distribution" have the same meaning here as under U.S. copyright law.
7
+
8
+ "You" means the licensee of the software.
9
+
10
+ "Your company" means the company you worked for when you downloaded the software.
11
+
12
+ "Reference use" means the use of the software within your company as a reference, in read-only form, for the sole purposes of debugging your products, maintaining your products, or enhancing the interoperability of your products with the software, and specifically excludes the right to distribute the software outside of your company.
13
+
14
+ "Licensed patents" means any Licensor patent claims which read directly on the software as distributed by the Licensor under this license.
15
+
16
+ 2. Grant of Rights
17
+ (A) Copyright Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free copyright license to reproduce the software for reference use.
18
+
19
+ (B) Patent Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free patent license under licensed patents for reference use.
20
+
21
+ 3. Limitations
22
+ (A) No Trademark License- This license does not grant you any rights to use the Licensor's name, logo, or trademarks.
23
+
24
+ (B) If you begin patent litigation against the Licensor over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.
25
+
26
+ (C) The software is licensed "as-is." You bear the risk of using it. The Licensor gives no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the Licensor excludes the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # apimatic-core
2
+ [//]: # ([![PyPI][rubygems-version]][rubygems-apimatic-faraday-client-adapter-url])
3
+ [![Tests][test-badge]][test-url]
4
+ [![Linting][lint-badge]][lint-url]
5
+ [![Maintainability][maintainability-url]][code-climate-url]
6
+ [![Test Coverage][test-coverage-url]][code-climate-url]
7
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
8
+ [![Licence][license-badge]][license-url]
9
+
10
+ ## Introduction
11
+ Core library ruby does the job of congregating common and core functionality from ruby SDKs. This includes functionalities like the ability to create HTTP requests, handle responses, apply authentication schemes, convert API responses back to object instances, and validate user and server data.
12
+
13
+
14
+ ## Installation
15
+ You will need `2.6 <= Ruby version < 3.1` to support this package.
16
+
17
+ Installation is quite simple, just execute the following command:
18
+ ```
19
+ gem install apimatic_core
20
+ ```
21
+
22
+ If you'd rather install apimatic_core using bundler, add a line for it in your Gemfile:
23
+ ```
24
+ gem 'apimatic_core'
25
+ ```
26
+
27
+ ## API Call Classes
28
+ | Name | Description |
29
+ |-----------------------------------------------------------------------|----------------------------------------------------|
30
+ | [`RequestBuilder`](lib/apimatic-core/http/request/http_request.rb) | Builder class used to build an API Request |
31
+ | [`APICall`](lib/apimatic-core/api_call.rb) | Class used to create an API Call object |
32
+ | [`ResponseHandler`](lib/apimatic-core/http/response/http_response.rb) | Used to handle the response returned by the server |
33
+
34
+
35
+ ## Authentication
36
+ | Name | Description |
37
+ |------------------------------------------------------------------------|-----------------------------------------------------------------------------|
38
+ | [`HeaderAuth`](lib/apimatic-core/authentication/header_auth.rb) | Supports HTTP authentication through HTTP Headers |
39
+ | [`QueryAuth`](lib/apimatic-core/authentication/query_auth.rb) | Supports HTTP authentication through query parameters |
40
+ | [`AuthGroup`](lib/apimatic-core/authentication/multiple/auth_group.rb) | Helper class to support multiple authentication operation |
41
+ | [`And`](lib/apimatic-core/authentication/multiple/and_auth_group.rb) | Helper class to support AND operation between multiple authentication types |
42
+ | [`Or`](lib/apimatic-core/authentication/multiple/or_auth_group.rb) | Helper class to support OR operation between multiple authentication types |
43
+ | [`Single`](lib/apimatic-core/authentication/multiple/single_auth.rb) | Helper class to support single authentication |
44
+
45
+
46
+ ## Configurations
47
+ | Name | Description |
48
+ |------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|
49
+ | [`GlobalConfiguration`](lib/apimatic-core/configurations/global_configuration.rb ) | Class holding the global configuration properties to make a successful API Call |
50
+
51
+ ## Factories
52
+ | Name | Description |
53
+ |-------------------------------------------------------------------------------|------------------------------------------|
54
+ | [`HttpResponseFactory`](lib/apimatic-core/factories/http_response_factory.rb) | Factory class to create an HTTP Response |
55
+
56
+ ## HTTP
57
+ | Name | Description |
58
+ |-------------------------------------------------------------------------------------------------|----------------------------------------------------------|
59
+ | [`HttpClientConfiguration`](lib/apimatic-core/http/configurations/http_client_configuration.rb) | Class used for configuring SDK by a user |
60
+ | [`HttpRequest`](lib/apimatic-core/http/request/http_request.rb) | Class which contains information about the HTTP Request |
61
+ | [`ApiResponse`](lib/apimatic-core/http/response/api_response.rb) | Wrapper class for Api Response |
62
+ | [`HttpResponse`](lib/apimatic-core/http/response/http_response.rb) | Class which contains information about the HTTP Response |
63
+
64
+ ## Logger
65
+ | Name | Description |
66
+ |-------------------------------------------------------------------|--------------------------------------------|
67
+ | [`EndpointLogger`](lib/apimatic-core/logger/endpoint_logger.rb) | A class to provide logging for an API call |
68
+
69
+ ## Types
70
+ | Name | Description |
71
+ |------------------------------------------------------------------------------|-------------------------------------------------------------------------------|
72
+ | [`ApiException`](lib/apimatic-core/types/sdk/api_exception.rb) | Basic exception type for the SDK |
73
+ | [`ValidationException`](lib/apimatic-core/types/sdk/validation_exception.rb) | Exception thrown in case of validation error or failure |
74
+ | [`ErrorCase`](lib/apimatic-core/types/error_case.rb) | A class to represent Exception types |
75
+ | [`FileWrapper`](lib/apimatic-core/types/sdk/file_wrapper.rb) | A wrapper to allow passing in content type for file uploads |
76
+ | [`Parameter`](lib/apimatic-core/types/parameter.rb) | A class to represent information about a Parameter passed in an endpoint |
77
+ | [`XmlAttributes`](lib/apimatic-core/types/xml_attributes.rb) | A class to represent information about an XML Parameter passed in an endpoint |
78
+
79
+ ## Utilities
80
+ | Name | Description |
81
+ |------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
82
+ | [`ApiHelper`](lib/apimatic-core/utilities/api_helper.rb) | A Helper Class with various functions associated with making an API Call |
83
+ | [`AuthHelper`](lib/apimatic-core/utilities/auth_helper.rb) | A Helper Class with various functions associated with authentication in API Calls |
84
+ | [`ComparisonHelper`](lib/apimatic-core/utilities/comparison_helper.rb) | A Helper Class used for the comparison of expected and actual API response |
85
+ | [`FileHelper`](lib/apimatic-core/utilities/file_helper.rb) | A Helper Class for files |
86
+ | [`XmlHelper`](lib/apimatic-core/utilities/xml_helper.rb ) | A Helper class that holds utility methods for xml serialization and deserialization. |
87
+
88
+ ## Links
89
+ * [apimatic_core_interfaces](link here)
90
+
91
+ [rubygems-version]: https://img.shields.io/pypi/v/apimatic-requests-client-adapter
92
+ [rubygems-apimatic-faraday-client-adapter-url]: https://pypi.org/project/apimatic-requests-client-adapter/
93
+ [test-badge]: https://github.com/apimatic/core-lib-ruby/actions/workflows/test-runner.yml/badge.svg
94
+ [test-url]: https://github.com/apimatic/core-lib-ruby/actions/workflows/test-runner.yml
95
+ [lint-badge]: https://github.com/apimatic/core-lib-ruby/actions/workflows/lint-runner.yml/badge.svg
96
+ [lint-url]: https://github.com/apimatic/core-lib-ruby/actions/workflows/lint-runner.yml
97
+ [code-climate-url]: https://codeclimate.com/github/apimatic/core-lib-ruby
98
+ [maintainability-url]: https://api.codeclimate.com/v1/badges/85d658cd4e879f057e7d/maintainability
99
+ [test-coverage-url]: https://api.codeclimate.com/v1/badges/85d658cd4e879f057e7d/test_coverage
100
+ [license-badge]: https://img.shields.io/badge/licence-APIMATIC-blue
101
+ [license-url]: LICENSE
@@ -0,0 +1,103 @@
1
+ module CoreLibrary
2
+ # This class is responsible for executing an API call using HttpClient, RequestBuilder and ResponseHandler objects.
3
+ class ApiCall
4
+ # Creates a new builder instance of the API call with pre-configured global and logging configurations.
5
+ # @return [ApiCall] The instance of ApiCall object.
6
+ def new_builder
7
+ ApiCall.new(@global_configuration, logger: @endpoint_logger.logger)
8
+ end
9
+
10
+ def initialize(global_configuration, logger: nil)
11
+ @global_configuration = global_configuration
12
+ @request_builder = RequestBuilder.new
13
+ @response_handler = ResponseHandler.new
14
+ @endpoint_logger = EndpointLogger.new(logger)
15
+ @endpoint_name_for_logging = nil
16
+ @endpoint_context = {}
17
+ end
18
+
19
+ # The setter for the request builder to be used for building the request of an API call.
20
+ # @param [RequestBuilder] request_builder The request builder instance.
21
+ # @return [ApiCall] An updated instance of ApiCall.
22
+ def request(request_builder)
23
+ @request_builder = request_builder
24
+ self
25
+ end
26
+
27
+ # The setter for the response handler to be used for handling the response of an API call.
28
+ # @param [ResponseHandler] response_handler The response handler instance.
29
+ # @return [ApiCall] An updated instance of ApiCall.
30
+ def response(response_handler)
31
+ @response_handler = response_handler
32
+ self
33
+ end
34
+
35
+ # The setter for the name of the endpoint controller method to used while logging an endpoint call.
36
+ # @param [String] endpoint_name_for_logging The name of the endpoint controller method to used while logging.
37
+ # @return [ApiCall] An updated instance of ApiCall.
38
+ def endpoint_name_for_logging(endpoint_name_for_logging)
39
+ @endpoint_name_for_logging = endpoint_name_for_logging
40
+ self
41
+ end
42
+
43
+ # The setter for the context for an endpoint call.
44
+ # @param [String] context_key The name of the endpoint context.
45
+ # @param [Object] context_value The value of the endpoint context.
46
+ # @return [ApiCall] An updated instance of ApiCall.
47
+ def endpoint_context(context_key, context_value)
48
+ @endpoint_context[context_key] = context_value
49
+ self
50
+ end
51
+
52
+ # Executes the API call using provided HTTP client, request builder and response handler objects.
53
+ # @return The deserialized endpoint response.
54
+ def execute
55
+ _client_configuration = @global_configuration.client_configuration
56
+ begin
57
+ if _client_configuration.http_client.nil?
58
+ raise ArgumentError, 'An HTTP client instance is required to execute an Api call.'
59
+ end
60
+
61
+ _http_request = @request_builder.endpoint_logger(@endpoint_logger)
62
+ .endpoint_name_for_logging(@endpoint_name_for_logging)
63
+ .global_configuration(@global_configuration)
64
+ .build(@endpoint_context)
65
+ @endpoint_logger.debug("Raw request for #{@endpoint_name_for_logging} is: #{_http_request.inspect}")
66
+
67
+ _http_callback = _client_configuration.http_callback
68
+ unless _http_callback.nil?
69
+ update_http_callback(proc do
70
+ _http_callback&.on_before_request(_http_request)
71
+ end, "Calling the on_before_request method of http_call_back for #{@endpoint_name_for_logging}.")
72
+ end
73
+
74
+ _http_response = _client_configuration.http_client.execute(_http_request)
75
+ @endpoint_logger.debug("Raw response for #{@endpoint_name_for_logging} is: #{_http_response.inspect}")
76
+
77
+ unless _http_callback.nil?
78
+ update_http_callback(proc do
79
+ _http_callback&.on_after_response(_http_response)
80
+ end, "Calling the on_after_response method of http_call_back for #{@endpoint_name_for_logging}.")
81
+ end
82
+
83
+ _deserialized_response = @response_handler.endpoint_logger(@endpoint_logger)
84
+ .endpoint_name_for_logging(@endpoint_name_for_logging)
85
+ .handle(_http_response, @global_configuration.get_global_errors,
86
+ @global_configuration.get_sdk_module,
87
+ @global_configuration.should_symbolize_hash)
88
+ _deserialized_response
89
+ rescue StandardError => e
90
+ @endpoint_logger.error(e)
91
+ raise e
92
+ end
93
+ end
94
+
95
+ # Registers request and response with the provided http_callback
96
+ # @param [Callable] callable The callable to be called for registering into the HttpCallback instance.
97
+ # @param [String] log_message The message to be logged if HttpCallback is set.
98
+ def update_http_callback(callable, log_message)
99
+ @endpoint_logger.info(log_message)
100
+ callable.call
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,21 @@
1
+ module CoreLibrary
2
+ # This class is responsible for adding authentication in request header parameter.
3
+ class HeaderAuth < Authentication
4
+ def initialize(auth_params)
5
+ @auth_params = auth_params
6
+ @error_message = nil
7
+ end
8
+
9
+ # Checks whether this authentication scheme is valid or not.
10
+ # @return [Boolean] True if the auth instance is valid to be applied on the request.
11
+ def valid
12
+ AuthHelper.valid_auth?(@auth_params)
13
+ end
14
+
15
+ # Applies the authentication scheme on the given HTTP request.
16
+ # @param [HttpRequest] _http_request The HTTP request to apply the authentication on.
17
+ def apply(_http_request)
18
+ AuthHelper.apply(@auth_params, _http_request.method(:add_header))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module CoreLibrary
2
+ # The class to handle the AND combination of multiple authentications.
3
+ class And < AuthGroup
4
+ # Getter for the error message for auth.
5
+ # @return [String] The error message while applying the auth.
6
+ def error_message
7
+ @error_messages.join(' and ')
8
+ end
9
+
10
+ def initialize(*auth_group)
11
+ super auth_group
12
+ @is_valid_group = true
13
+ end
14
+
15
+ # Checks if the associated auth is valid.
16
+ # @return [Boolean] True if the associated auth is valid, false otherwise.
17
+ def valid
18
+ @mapped_group.each do |participant|
19
+ unless participant.valid
20
+ @error_messages.append(participant.error_message)
21
+ @is_valid_group = false
22
+ end
23
+ end
24
+
25
+ @is_valid_group
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ module CoreLibrary
2
+ # The parent class of multiple authentication groups (i.e. Or & And groups).
3
+ # This parent class is responsible for handling multiple auths on a particular request.
4
+ class AuthGroup < Authentication
5
+ attr_accessor :auth_participants, :mapped_group, :error_messages, :is_valid_group
6
+
7
+ def initialize(auth_group)
8
+ @auth_participants = []
9
+ auth_group.each do |auth_participant|
10
+ if !auth_participant.nil? && auth_participant.is_a?(String)
11
+ @auth_participants.append(Single.new(auth_participant))
12
+ elsif !auth_participant.nil?
13
+ @auth_participants.append(auth_participant)
14
+ end
15
+ @mapped_group = []
16
+ @error_messages = []
17
+ @is_valid_group = nil
18
+ end
19
+ end
20
+
21
+ # Extracts out the auth from the given auth managers.
22
+ # @param [Hash] auth_managers The hash of auth managers.
23
+ # @return [Single] An updated instance of itself.
24
+ def with_auth_managers(auth_managers)
25
+ @auth_participants.each do |participant|
26
+ @mapped_group.append(participant.with_auth_managers(auth_managers))
27
+ end
28
+ self
29
+ end
30
+
31
+ # Checks if the associated auth is valid.
32
+ # @return [Boolean] True if the associated auth is valid, false otherwise.
33
+ def valid
34
+ raise NotImplementedError, 'This method needs to be implemented in a child class.'
35
+ end
36
+
37
+ # Applies the associated auth to the HTTP request.
38
+ # @param [HttpRequest] http_request The HTTP request on which the auth is to be applied.
39
+ def apply(http_request)
40
+ return unless @is_valid_group
41
+
42
+ @mapped_group.each { |participant| participant.apply(http_request) }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,29 @@
1
+ module CoreLibrary
2
+ # The class to handle the OR combination of multiple authentications for a particular request.
3
+ class Or < AuthGroup
4
+ # Getter for the error message for auth.
5
+ # @return [String] The error message while applying the auth.
6
+ def error_message
7
+ @error_messages.join(' or ')
8
+ end
9
+
10
+ def initialize(*auth_group)
11
+ super auth_group
12
+ @is_valid_group = false
13
+ end
14
+
15
+ # Checks if the associated auth is valid.
16
+ # @return [Boolean] True if the associated auth is valid, false otherwise.
17
+ def valid
18
+ @mapped_group.each do |participant|
19
+ if participant.valid
20
+ @is_valid_group = true
21
+ else
22
+ @error_messages.append(participant.error_message)
23
+ end
24
+ end
25
+
26
+ @is_valid_group
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ module CoreLibrary
2
+ # The class to handle the single authentication for a particular request.
3
+ class Single < Authentication
4
+ # Getter for the error message for auth.
5
+ # @return [String] The error message while applying the auth.
6
+ def error_message
7
+ "[#{@error_message}]"
8
+ end
9
+
10
+ def initialize(auth_participant)
11
+ @auth_participant = auth_participant
12
+ @mapped_auth = nil
13
+ @error_message = nil
14
+ @is_valid = true
15
+ end
16
+
17
+ # Extracts out the auth from the given auth managers.
18
+ # @param [Hash] auth_managers The hash of auth managers.
19
+ # @return [Single] An updated instance of itself.
20
+ def with_auth_managers(auth_managers)
21
+ raise ArgumentError, 'Auth key is invalid.' unless auth_managers.key?(@auth_participant)
22
+
23
+ @mapped_auth = auth_managers[@auth_participant]
24
+ self
25
+ end
26
+
27
+ # Checks if the associated auth is valid.
28
+ # @return [Boolean] True if the associated auth is valid, false otherwise.
29
+ def valid
30
+ raise ArgumentError, 'The auth manager entry must not have a nil value.' if @mapped_auth.nil?
31
+
32
+ unless @mapped_auth.valid
33
+ @error_message = @mapped_auth.error_message
34
+ @is_valid = false
35
+ end
36
+
37
+ @is_valid
38
+ end
39
+
40
+ # Applies the associated auth to the HTTP request.
41
+ # @param [HttpRequest] http_request The HTTP request on which the auth is to be applied.
42
+ def apply(http_request)
43
+ return unless @is_valid
44
+
45
+ @mapped_auth.apply(http_request)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ module CoreLibrary
2
+ # This class is responsible for adding authentication in request query parameter.
3
+ class QueryAuth < Authentication
4
+ def initialize(auth_params)
5
+ @auth_params = auth_params
6
+ @error_message = nil
7
+ end
8
+
9
+ # Checks whether this authentication scheme is valid or not.
10
+ # @return [Boolean] True if the auth instance is valid to be applied on the request.
11
+ def valid
12
+ AuthHelper.valid_auth?(@auth_params)
13
+ end
14
+
15
+ # Applies the authentication scheme on the given HTTP request.
16
+ # @param [HttpRequest] _http_request The HTTP request to apply the authentication on.
17
+ def apply(_http_request)
18
+ AuthHelper.apply(@auth_params, _http_request.method(:add_query_parameter))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,149 @@
1
+ module CoreLibrary
2
+ # A class to hold the global configurations for the core library. This class is initiated from the SDK.
3
+ class GlobalConfiguration
4
+ attr_reader :client_configuration
5
+
6
+ def initialize(client_configuration: HttpClientConfiguration.new)
7
+ @client_configuration = client_configuration
8
+ @global_errors = {}
9
+ @global_headers = {}
10
+ @additional_headers = {}
11
+ @auth_managers = {}
12
+ @base_uri_executor = nil
13
+ @sdk_module = nil
14
+ @symbolize_hash = false
15
+ end
16
+
17
+ # The setter for the global errors.
18
+ # @param [Hash] global_errors The hash of global errors.
19
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
20
+ def global_errors(global_errors)
21
+ @global_errors = global_errors
22
+ self
23
+ end
24
+
25
+ # The getter for the global errors.
26
+ # @return [Hash] The hash of global errors.
27
+ def get_global_errors
28
+ @global_errors
29
+ end
30
+
31
+ # Sets the current SDK module core library is being used for.
32
+ def sdk_module(sdk_module)
33
+ @sdk_module = sdk_module
34
+ self
35
+ end
36
+
37
+ def get_sdk_module
38
+ @sdk_module
39
+ end
40
+
41
+ # The setter for the global headers to be attached with all requests.
42
+ # @param [Hash] global_headers The hash of global headers.
43
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
44
+ def global_headers(global_headers)
45
+ @global_headers = global_headers
46
+ self
47
+ end
48
+
49
+ # The setter for a global header to be attached with all requests.
50
+ # @param [String] key The key of a global header.
51
+ # @param [Object] value The value of a global header.
52
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
53
+ def global_header(key, value)
54
+ @global_headers[key] = value
55
+ self
56
+ end
57
+
58
+ # The getter for the global headers.
59
+ # @return [Hash] The hash of global headers.
60
+ def get_global_headers
61
+ @global_headers
62
+ end
63
+
64
+ # The setter for the additional headers to be attached with all requests.
65
+ # @param [Hash] additional_headers The hash of additional headers.
66
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
67
+ def additional_headers(additional_headers)
68
+ @additional_headers = additional_headers
69
+ self
70
+ end
71
+
72
+ # The setter for a additional header to be attached with all requests.
73
+ # @param [String] key The key of a additional header.
74
+ # @param [Object] value The value of a additional header.
75
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
76
+ def additional_header(key, value)
77
+ @additional_headers[key] = value
78
+ self
79
+ end
80
+
81
+ # The getter for the additional headers.
82
+ # @return [Hash] The hash of additional headers.
83
+ def get_additional_headers
84
+ @additional_headers
85
+ end
86
+
87
+ # The setter for the auth managers.
88
+ # @param [Hash] auth_managers The hash of auth managers.
89
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
90
+ def auth_managers(auth_managers)
91
+ @auth_managers = auth_managers
92
+ self
93
+ end
94
+
95
+ # The getter for the auth managers.
96
+ # @return [Hash] The hash of auth managers.
97
+ def get_auth_managers
98
+ @auth_managers
99
+ end
100
+
101
+ # The setter for the user agent information to be attached with all requests.
102
+ # @param [String] user_agent The user agent template string.
103
+ # @param [Hash, Optional] agent_parameters The agent configuration to be replaced in the actual user agent template.
104
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
105
+ def user_agent(user_agent, agent_parameters: {})
106
+ add_useragent_in_headers(user_agent, agent_parameters)
107
+ self
108
+ end
109
+
110
+ # The setter for the base URI extractor callable.
111
+ # @param [Callable] base_uri_executor The callable for the base URI extractor.
112
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
113
+ def base_uri_executor(base_uri_executor)
114
+ @base_uri_executor = base_uri_executor
115
+ self
116
+ end
117
+
118
+ # The getter for the base URI extractor.
119
+ # @return [Callable] The base URI extractor.
120
+ def get_base_uri_executor
121
+ @base_uri_executor
122
+ end
123
+
124
+ # The setter for the flag of symbolizing hash while deserialization.
125
+ # @param [Boolean] symbolize_hash The flag of symbolizing hash while deserialization.
126
+ # @return [GlobalConfiguration] An updated instance of GlobalConfiguration.
127
+ def symbolize_hash(symbolize_hash)
128
+ @symbolize_hash = symbolize_hash
129
+ self
130
+ end
131
+
132
+ # The setter for the flag of wrapping the body parameters in a hash.
133
+ # @return [Boolean] True if symbolizing hash is allowed during deserialization of response.
134
+ def should_symbolize_hash
135
+ @symbolize_hash
136
+ end
137
+
138
+ # Updates the user agent template with the provided parameters and adds user agent in the global_headers.
139
+ # @param [String] user_agent The user agent template string.
140
+ # @param [Hash, Optional] agent_parameters The agent configurations to be replaced in the actual user agent value.
141
+ def add_useragent_in_headers(user_agent, agent_parameters)
142
+ if !agent_parameters.nil? && agent_parameters.any?
143
+ user_agent = ApiHelper.update_user_agent_value_with_parameters(user_agent,
144
+ agent_parameters).gsub(' ', ' ')
145
+ end
146
+ @global_headers['user-agent'] = user_agent unless user_agent.nil?
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,5 @@
1
+ module CoreLibrary
2
+ # Class for exceptions when there is an invalid state while applying the auth credentials.
3
+ class InvalidAuthCredential < StandardError
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module CoreLibrary
2
+ # Factory to create an instance of HttpResponse
3
+ class HttpResponseFactory < ResponseFactory
4
+ # The factory method for creating HttpResponse type.
5
+ # @param [Integer] _status_code The status code returned by the server.
6
+ # @param [String] _reason_phrase The reason phrase returned by the server.
7
+ # @param [Hash] _headers headers The headers sent by the server in the response.
8
+ # @param [String] _raw_body The raw body of the response.
9
+ # @param [HttpRequest] _request The request that resulted in this response.
10
+ def create(_status_code, _reason_phrase, _headers, _raw_body, _request)
11
+ HttpResponse.new(_status_code, _reason_phrase, _headers, _raw_body, _request)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module CoreLibrary
2
+ # Configuration for an HttpClient.
3
+ class HttpClientConfiguration < ClientConfiguration
4
+ attr_reader :http_client, :http_callback
5
+
6
+ def initialize(
7
+ connection: nil, adapter: :net_http_persistent, timeout: 60,
8
+ max_retries: 0, retry_interval: 1, backoff_factor: 2,
9
+ retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
10
+ retry_methods: %i[get put], cache: false, verify: true, http_callback: nil, http_client: nil
11
+ )
12
+ @response_factory = HttpResponseFactory.new
13
+ @connection = connection
14
+ @adapter = adapter
15
+ @retry_interval = retry_interval
16
+ @http_callback = http_callback
17
+ @timeout = timeout
18
+ @max_retries = max_retries
19
+ @backoff_factor = backoff_factor
20
+ @retry_statuses = retry_statuses
21
+ @retry_methods = retry_methods
22
+ @verify = verify
23
+ @cache = cache
24
+ @http_client = http_client
25
+ end
26
+
27
+ # Setter for http_client.
28
+ def set_http_client(http_client)
29
+ @http_client = http_client
30
+ end
31
+ end
32
+ end