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
@@ -0,0 +1,269 @@
1
+ module CoreLibrary
2
+ # This class is for handling of the http response for an API call.
3
+ class ResponseHandler
4
+ def initialize
5
+ @deserializer = nil
6
+ @convertor = nil
7
+ @deserialize_into = nil
8
+ @is_api_response = false
9
+ @is_nullify404 = false
10
+ @local_errors = {}
11
+ @datetime_format = nil
12
+ @is_xml_response = false
13
+ @xml_attribute = nil
14
+ @endpoint_name_for_logging = nil
15
+ @endpoint_logger = nil
16
+ @is_primitive_response = false
17
+ @is_date_response = false
18
+ @is_response_array = false
19
+ @is_response_void = false
20
+ @type_group = nil
21
+ end
22
+
23
+ # Sets deserializer for the response.
24
+ # @param [Method] deserializer The method to be called for deserializing the response.
25
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
26
+ def deserializer(deserializer)
27
+ @deserializer = deserializer
28
+ self
29
+ end
30
+
31
+ # Sets converter for the response.
32
+ # @param [Method] convertor The method to be called while converting the deserialized response.
33
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
34
+ def convertor(convertor)
35
+ @convertor = convertor
36
+ self
37
+ end
38
+
39
+ # Sets the model to deserialize into.
40
+ # @param [Method] deserialize_into The method to be called while deserializing.
41
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
42
+ def deserialize_into(deserialize_into)
43
+ @deserialize_into = deserialize_into
44
+ self
45
+ end
46
+
47
+ # Sets local_errors hash key value.
48
+ # @param [String] error_code The error code to check against.
49
+ # @param [String] description The reason for the exception.
50
+ # @param [ApiException] exception_type The type of the exception to raise.
51
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
52
+ def local_error(error_code, description, exception_type)
53
+ @local_errors[error_code.to_s] = ErrorCase.new.description(description).exception_type(exception_type)
54
+ self
55
+ end
56
+
57
+ # Sets the datetime format.
58
+ # @param [DateTimeFormat] datetime_format The date time format to deserialize against.
59
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
60
+ def datetime_format(datetime_format)
61
+ @datetime_format = datetime_format
62
+ self
63
+ end
64
+
65
+ # Set the xml_attribute property.
66
+ # @param [XmlAttributes] xml_attribute The xml configuration if the response is XML.
67
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
68
+ def xml_attribute(xml_attribute)
69
+ @xml_attribute = xml_attribute
70
+ self
71
+ end
72
+
73
+ # Sets the endpoint_name_for_logging property.
74
+ # @param [String] endpoint_name_for_logging The endpoint method name to be used while logging.
75
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
76
+ def endpoint_name_for_logging(endpoint_name_for_logging)
77
+ @endpoint_name_for_logging = endpoint_name_for_logging
78
+ self
79
+ end
80
+
81
+ # Sets endpoint logger to be used.
82
+ # @param [EndpointLogger] endpoint_logger The logger to be used for logging API call steps.
83
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
84
+ def endpoint_logger(endpoint_logger)
85
+ @endpoint_logger = endpoint_logger
86
+ self
87
+ end
88
+
89
+ # Sets the is_primitive_response property.
90
+ # @param [Boolean] is_primitive_response Flag if the response is of primitive type.
91
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
92
+ # rubocop:disable Naming/PredicateName
93
+ def is_primitive_response(is_primitive_response)
94
+ @is_primitive_response = is_primitive_response
95
+ self
96
+ end
97
+
98
+ # Sets the is_api_response property.
99
+ # @param [Boolean] is_api_response Flag to return the complete http response.
100
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
101
+ def is_api_response(is_api_response)
102
+ @is_api_response = is_api_response
103
+ self
104
+ end
105
+
106
+ # Sets the is_nullify404 property.
107
+ # @param [Boolean] is_nullify404 Flag to return early in case of 404 error code.
108
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
109
+ def is_nullify404(is_nullify404)
110
+ @is_nullify404 = is_nullify404
111
+ self
112
+ end
113
+
114
+ # Set the is_xml_response property.
115
+ # @param [Boolean] is_xml_response Flag if the response is XML.
116
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
117
+ def is_xml_response(is_xml_response)
118
+ @is_xml_response = is_xml_response
119
+ self
120
+ end
121
+
122
+ # Sets the is_date_response property.
123
+ # @param [Boolean] is_date_response Flag if the response is a date.
124
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
125
+ def is_date_response(is_date_response)
126
+ @is_date_response = is_date_response
127
+ self
128
+ end
129
+
130
+ # Sets the is_response_array property.
131
+ # @param [Boolean] is_response_array Flag if the response is an array.
132
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
133
+ def is_response_array(is_response_array)
134
+ @is_response_array = is_response_array
135
+ self
136
+ end
137
+
138
+ # Sets the is_response_void property.
139
+ # @param [Boolean] is_response_void Flag if the response is void.
140
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
141
+ def is_response_void(is_response_void)
142
+ @is_response_void = is_response_void
143
+ self
144
+ end
145
+ # rubocop:enable Naming/PredicateName
146
+
147
+ # Sets type group for the response.
148
+ # @param [String] type_group The oneOf/anyOf type group template.
149
+ # @return [ResponseHandler] An updated instance of ResponseHandler.
150
+ def type_group(type_group)
151
+ @type_group = type_group
152
+ self
153
+ end
154
+
155
+ # Main method to handle the response with all the set properties.
156
+ # @param [HttpResponse] response The response received.
157
+ # @param [Hash] global_errors The global errors object.
158
+ # @param [Module] sdk_module The module of the SDK core library is being used for.
159
+ # @param [Boolean] should_symbolize_hash Flag to symbolize the hash during response deserialization.
160
+ # @return [Object] The deserialized response of the API Call.
161
+ # rubocop:disable Style/OptionalBooleanParameter
162
+ def handle(response, global_errors, sdk_module, should_symbolize_hash = false)
163
+ @endpoint_logger.info("Validating response for #{@endpoint_name_for_logging}.")
164
+
165
+ # checking Nullify 404
166
+ if response.status_code == 404 && @is_nullify404
167
+ @endpoint_logger.info("Status code 404 received for #{@endpoint_name_for_logging}. Returning None.")
168
+ return nil
169
+ end
170
+
171
+ # validating response if configured
172
+ validate(response, global_errors)
173
+
174
+ return if @is_response_void
175
+
176
+ # applying deserializer if configured
177
+ deserialized_value = apply_deserializer(response, sdk_module, should_symbolize_hash)
178
+
179
+ # applying api_response if configured
180
+ deserialized_value = apply_api_response(response, deserialized_value)
181
+
182
+ # applying convertor if configured
183
+ deserialized_value = apply_convertor(deserialized_value)
184
+
185
+ deserialized_value
186
+ end
187
+ # rubocop:enable Style/OptionalBooleanParameter
188
+
189
+ # Validates the response provided and throws an error from global_errors if it fails.
190
+ # @param response The received response.
191
+ # @param global_errors Global errors hash.
192
+ def validate(response, global_errors)
193
+ return unless response.status_code < 200 || response.status_code > 208
194
+
195
+ actual_status_code = response.status_code.to_s
196
+
197
+ contains_local_errors = (!@local_errors.nil? and !@local_errors[actual_status_code].nil?)
198
+ if contains_local_errors
199
+ error_case = @local_errors[actual_status_code]
200
+ raise error_case.get_exception_type.new error_case.get_description, response
201
+ end
202
+
203
+ contains_local_default_error = (!@local_errors.nil? and !@local_errors['default'].nil?)
204
+ if contains_local_default_error
205
+ error_case = @local_errors['default']
206
+ raise error_case.get_exception_type.new error_case.get_description, response
207
+ end
208
+
209
+ contains_global_errors = (!global_errors.nil? and !global_errors[actual_status_code].nil?)
210
+ if contains_global_errors
211
+ error_case = global_errors[actual_status_code]
212
+ raise error_case.get_exception_type.new error_case.get_description, response
213
+ end
214
+
215
+ error_case = global_errors['default']
216
+ raise error_case.get_exception_type.new error_case.get_description, response unless error_case.nil?
217
+ end
218
+
219
+ # Applies xml deserializer to the response.
220
+ def apply_xml_deserializer(response)
221
+ unless @xml_attribute.get_array_item_name.nil?
222
+ return @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name,
223
+ @xml_attribute.get_array_item_name, @deserialize_into, @datetime_format)
224
+ end
225
+
226
+ @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name, @deserialize_into, @datetime_format)
227
+ end
228
+
229
+ # Applies deserializer to the response.
230
+ # @param sdk_module Module of the SDK using the core library.
231
+ # @param [Boolean] should_symbolize_hash Flag to symbolize the hash during response deserialization.
232
+ def apply_deserializer(response, sdk_module, should_symbolize_hash)
233
+ return apply_xml_deserializer(response) if @is_xml_response
234
+ return response.raw_body if @deserializer.nil?
235
+
236
+ if !@type_group.nil?
237
+ @deserializer.call(@type_group, response.raw_body, sdk_module, should_symbolize_hash)
238
+ elsif @datetime_format
239
+ @deserializer.call(response.raw_body, @datetime_format, @is_response_array, should_symbolize_hash)
240
+ elsif @is_date_response
241
+ @deserializer.call(response.raw_body, @is_response_array, should_symbolize_hash)
242
+ elsif !@deserialize_into.nil? || @is_primitive_response
243
+ @deserializer.call(response.raw_body, @deserialize_into, @is_response_array, should_symbolize_hash)
244
+ else
245
+ @deserializer.call(response.raw_body, should_symbolize_hash)
246
+ end
247
+ end
248
+
249
+ # Applies API response.
250
+ # @param response The actual HTTP response.
251
+ # @param deserialized_value The deserialized value.
252
+ def apply_api_response(response, deserialized_value)
253
+ if @is_api_response
254
+ errors = ApiHelper.map_response(deserialized_value, ['errors'])
255
+ return ApiResponse.new(response, data: deserialized_value, errors: errors)
256
+ end
257
+
258
+ deserialized_value
259
+ end
260
+
261
+ # Applies converter to the response.
262
+ # @param deserialized_value The deserialized value.
263
+ def apply_convertor(deserialized_value)
264
+ return @convertor.call(deserialized_value) unless @convertor.nil?
265
+
266
+ deserialized_value
267
+ end
268
+ end
269
+ end
@@ -0,0 +1,37 @@
1
+ module CoreLibrary
2
+ # This data class represents the expected errors to be handled after the API call.
3
+ class ErrorCase
4
+ def initialize
5
+ @description = nil
6
+ @exception_type = nil
7
+ end
8
+
9
+ # The setter for the description of the error message.
10
+ # @param [String] description The description of the error message.
11
+ # @return [ErrorCase] An updated instance of ErrorCase.
12
+ def description(description)
13
+ @description = description
14
+ self
15
+ end
16
+
17
+ # The getter for the description of the error message.
18
+ # @return [String] The description of the error message.
19
+ def get_description
20
+ @description
21
+ end
22
+
23
+ # The setter for the type of the exception to be thrown.
24
+ # @param [Object] exception_type The type of the exception to be thrown.
25
+ # @return [ErrorCase] An updated instance of ErrorCase.
26
+ def exception_type(exception_type)
27
+ @exception_type = exception_type
28
+ self
29
+ end
30
+
31
+ # The getter for the type of the exception to be thrown.
32
+ # @return [Object] The type of the exception to be thrown.
33
+ def get_exception_type
34
+ @exception_type
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,116 @@
1
+ module CoreLibrary
2
+ # This data class represents the parameter to be sent in the request.
3
+ class Parameter
4
+ def initialize
5
+ @key = nil
6
+ @value = nil
7
+ @is_required = false
8
+ @should_encode = false
9
+ @default_content_type = nil
10
+ @value_convertor = nil
11
+ @template = nil
12
+ end
13
+
14
+ # The setter for the parameter key.
15
+ # @param [String] key The parameter key to send.
16
+ # @return [Parameter] An updated instance of Parameter.
17
+ def key(key)
18
+ @key = key
19
+ self
20
+ end
21
+
22
+ # The getter for the parameter key.
23
+ # @return [String] The parameter key to send.
24
+ def get_key
25
+ @key
26
+ end
27
+
28
+ # The setter for the parameter value.
29
+ # @param [Object] value The parameter value to send.
30
+ # @return [Parameter] An updated instance of Parameter.
31
+ def value(value)
32
+ @value = value
33
+ self
34
+ end
35
+
36
+ # The getter for the parameter's actual/converted value where applicable.
37
+ # @return [Object] The parameter value to send.
38
+ def get_value
39
+ return @value_convertor.call(@value) unless @value_convertor.nil?
40
+
41
+ @value
42
+ end
43
+
44
+ # The setter for the flag if the parameter is required.
45
+ # @param [Boolean] is_required true if the parameter is required otherwise false, by default the value is false.
46
+ # @return [Parameter] An updated instance of Parameter.
47
+ # rubocop:disable Naming/PredicateName
48
+ def is_required(is_required)
49
+ @is_required = is_required
50
+ self
51
+ end
52
+ # rubocop:enable Naming/PredicateName
53
+
54
+ # The setter for the flag if the parameter value is to be encoded.
55
+ # @param [Boolean] should_encode true if the parameter value is to be encoded otherwise false, default is false.
56
+ # @return [Parameter] An updated instance of Parameter.
57
+ def should_encode(should_encode)
58
+ @should_encode = should_encode
59
+ self
60
+ end
61
+
62
+ # The setter for the function of converting value for form params.
63
+ # @param [Callable] value_convertor The function to execute for conversion.
64
+ # @return [Parameter] An updated instance of Parameter.
65
+ def value_convertor(value_convertor)
66
+ @value_convertor = value_convertor
67
+ self
68
+ end
69
+
70
+ # The getter for the flag if the parameter value is to be encoded.
71
+ # @return [Boolean] true if the parameter value is to be encoded otherwise false, by default the value is false.
72
+ def need_to_encode
73
+ @should_encode
74
+ end
75
+
76
+ # The setter for the default content type of the multipart request.
77
+ # @param [String] default_content_type The content type to be used, applicable for multipart request parameters.
78
+ # @return [Parameter] An updated instance of Parameter.
79
+ def default_content_type(default_content_type)
80
+ @default_content_type = default_content_type
81
+ self
82
+ end
83
+
84
+ # The getter for the default content type of the multipart request.
85
+ # @return [String] The default content type to be used applicable for multipart request parameters.
86
+ def get_default_content_type
87
+ @default_content_type
88
+ end
89
+
90
+ # Template in case of oneOf or anyOf
91
+ def template(template)
92
+ @template = template
93
+ self
94
+ end
95
+
96
+ def get_template
97
+ @template
98
+ end
99
+
100
+ # Validates the parameter value to be sent in the request.
101
+ # @raise [ValueError] The value error if the parameter is required but the value is nil.
102
+ def validate
103
+ raise ArgumentError, "Required parameter #{@key} cannot be nil." if @is_required && @value.nil?
104
+ end
105
+
106
+ # Validates the oneOf/anyOf parameter value to be sent in the request.
107
+ # @param [Module] sdk_module The module configured by the SDK.
108
+ # @param [Boolean] should_symbolize_hash Whether to symbolize the hash during the deserialization of the value.
109
+ # @raise [ValidationException] The validation error if value violates the oneOf/anyOf constraints.
110
+ # rubocop:disable Style/OptionalBooleanParameter
111
+ def validate_template(sdk_module, should_symbolize_hash = false)
112
+ ApiHelper.validate_types(@value, @template, sdk_module, should_symbolize_hash) unless @value.nil?
113
+ end
114
+ # rubocop:enable Style/OptionalBooleanParameter
115
+ end
116
+ end
@@ -0,0 +1,15 @@
1
+ module CoreLibrary
2
+ # Class for exceptions when there is a network error, status code error, etc.
3
+ class ApiException < StandardError
4
+ attr_reader :response, :response_code
5
+
6
+ # The constructor.
7
+ # @param [String] reason The reason for raising an exception.
8
+ # @param [HttpResponse] response The HttpResponse of the API call.
9
+ def initialize(reason, response)
10
+ super(reason)
11
+ @response = response
12
+ @response_code = response.status_code
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ module CoreLibrary
2
+ # The Base class of all custom types.
3
+ class BaseModel
4
+ # Use to allow additional model properties.
5
+ def method_missing(method_sym, *arguments, &block)
6
+ method = method_sym.to_s
7
+ if method.end_with? '='
8
+ instance_variable_set(format('@%s', [method.chomp('=')]),
9
+ arguments.first)
10
+ elsif instance_variable_defined?("@#{method}") && arguments.empty?
11
+ instance_variable_get("@#{method}")
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ # Override for additional model properties.
18
+ def respond_to_missing?(method_sym, include_private = false)
19
+ instance_variable_defined?("@#{method_sym}") ? true : super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module CoreLibrary
2
+ # A utility to allow users to set the content-type for files
3
+ class FileWrapper
4
+ attr_reader :content_type, :file
5
+
6
+ def initialize(file, content_type: 'application/octet-stream')
7
+ @file = file
8
+ @content_type = content_type
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # type_combinator_simple
2
+ #
3
+ # This file was automatically generated by APIMATIC v2.0
4
+ # ( https://apimatic.io ).
5
+
6
+ module CoreLibrary
7
+ # Class for exceptions when there is a schema validation error.
8
+ class ValidationException < StandardError
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ module CoreLibrary
2
+ # The class to hold the configuration for XML parameter in request and response.
3
+ class XmlAttributes
4
+ def initialize
5
+ @value = nil
6
+ @root_element_name = nil
7
+ @array_item_name = nil
8
+ end
9
+
10
+ def value(value)
11
+ @value = value
12
+ self
13
+ end
14
+
15
+ def root_element_name(root_element_name)
16
+ @root_element_name = root_element_name
17
+ self
18
+ end
19
+
20
+ def array_item_name(array_item_name)
21
+ @array_item_name = array_item_name
22
+ self
23
+ end
24
+
25
+ def get_root_element_name
26
+ @root_element_name
27
+ end
28
+
29
+ def get_value
30
+ @value
31
+ end
32
+
33
+ def get_array_item_name
34
+ @array_item_name
35
+ end
36
+ end
37
+ end