onlinepayments-sdk-ruby 4.16.0 → 4.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0bb67c94bd5f8e000fb9688e71e346837dde955160058f9b6db9950797ebc5a
4
- data.tar.gz: 4aadeaeba5d777d2c857bf082fe142349b61c6fe7f4d29b169b20d6dadd7b5c8
3
+ metadata.gz: 8e68d841d58a8709600390682acb7d45a026ce6996b259bb37291a63d84078ba
4
+ data.tar.gz: 99c0448333f7ccdacd4fd945b6d712e075dcb39ad27f4d541f66ddbc1558fad0
5
5
  SHA512:
6
- metadata.gz: 30f4dfeb625a981c46c9955d9087a44c6e3938695e07b912b1b40fe6b4989d2cc39c0daf822238f1968f37f4be6915f00b39ed19f1aac3e52876ca119fb83d45
7
- data.tar.gz: 7b2ca1a1b946893ec1044e19104a112158a087500adab1049295d00cf86ac2da9130a44f9ceeb47f3ae405e19fa7866fdbe3b9b12a2b4a3daf0761d50dc849a3
6
+ metadata.gz: 9caf954ae7682487cfd861661aacf9c504bd21cc63bbb0c0deb526b434df8778cd0ba9483104a1875fd7e356c1198a6885bbc749eda4409b3b917853376b43eb
7
+ data.tar.gz: da2a8e03c1ee39abab4e5713b01f9e175a8213fe5efddfb8d8fc641ccecfb03fffcf98a60ed0665c8a3d8dcbaaa92916c0105db1daaf125aee4771c06bd6a754
@@ -104,7 +104,8 @@ module OnlinePayments::SDK
104
104
  # @param relative_path [String] Path relative to the API endpoint
105
105
  # @param request_headers [Array<OnlinePayments::SDK::RequestHeader>, nil] Optional array of request headers
106
106
  # @param request_parameters [OnlinePayments::SDK::ParamRequest, nil] Optional request parameters
107
- # @param request_body [OnlinePayments::SDK::DataObject] The optional request body
107
+ # @param request_body [OnlinePayments::SDK::DataObject, OnlinePayments::SDK::MultipartFormDataObject, OnlinePayments::SDK::MultipartFormDataRequest]
108
+ # The optional request body
108
109
  # @param response_type [Type] The response type.
109
110
  # @param context [OnlinePayments::SDK::CallContext, nil] Optional call context.
110
111
  # @return The response of the POST request as the given response type
@@ -119,7 +120,14 @@ module OnlinePayments::SDK
119
120
  request_headers ||= []
120
121
 
121
122
  body = nil
122
- if request_body
123
+ if request_body.is_a? MultipartFormDataObject
124
+ request_headers.push(RequestHeader.new('Content-Type', request_body.content_type))
125
+ body = request_body
126
+ elsif request_body.is_a? MultipartFormDataRequest
127
+ multipart = request_body.to_multipart_form_data_object
128
+ request_headers.push(RequestHeader.new('Content-Type', multipart.content_type))
129
+ body = multipart
130
+ elsif request_body
123
131
  request_headers.push(RequestHeader.new('Content-Type', 'application/json'))
124
132
  body = @marshaller.marshal(request_body)
125
133
  else
@@ -19,6 +19,7 @@ module OnlinePayments::SDK
19
19
  using RefineHTTPClient
20
20
 
21
21
  CONTENT_TYPE = 'Content-Type'.freeze
22
+ X_REQUEST_ID_HEADER = 'X-Request-Id'.freeze
22
23
  JSON_CONTENT_TYPE = 'application/json'.freeze
23
24
 
24
25
  # @param args [Hash] the parameters to initialize the connection with
@@ -144,12 +145,16 @@ module OnlinePayments::SDK
144
145
  # @param method [String] 'GET', 'DELETE', 'POST' or 'PUT' depending on the HTTP method being used.
145
146
  # @param uri [URI::HTTP] full URI of the location the request is targeted at, including query parameters.
146
147
  # @param request_headers [Array<OnlinePayments::SDK::RequestHeader>] list of headers that should be used as HTTP headers in the request.
147
- # @param body [String] request body.
148
+ # @param body [String, OnlinePayments::SDK::MultipartFormDataObject] request body.
148
149
  # @yield (Integer, Array<OnlinePayments::SDK::ResponseHeader>, IO) The status code, headers and body of the response.
149
150
  # @raise [OnlinePayments::SDK::CommunicationException] when communication with the Online Payments platform was not successful.
150
151
  def request(method, uri, request_headers, body = nil)
151
152
  request_headers = convert_from_headers(request_headers)
152
153
  request_id = SecureRandom.uuid
154
+
155
+ # set X-Request-Id for better traceability
156
+ request_headers[X_REQUEST_ID_HEADER] = request_id
157
+
153
158
  content_type = request_headers[CONTENT_TYPE]
154
159
 
155
160
  info = { headers: request_headers, content_type: content_type }
@@ -163,14 +168,29 @@ module OnlinePayments::SDK
163
168
  response_status_code = nil
164
169
  response_content_type = nil
165
170
  response_body = ''
166
- raw_request(method, uri, request_headers, body) do |status_code, headers, r_content_type, r_body|
167
- response_headers = headers
168
- response_status_code = status_code
169
- response_content_type = r_content_type
170
- response_body = r_body.read.force_encoding('UTF-8')
171
- r_body = StringIO.new(response_body)
172
-
173
- yield status_code, headers, r_body
171
+
172
+ if body.is_a? OnlinePayments::SDK::MultipartFormDataObject
173
+ multipart_request(method, uri, request_headers, body) do |status_code, headers, r_content_type, r_body|
174
+ response_headers = headers
175
+ response_status_code = status_code
176
+ response_content_type = r_content_type
177
+ unless binary_content_type? response_content_type
178
+ response_body = r_body.read.force_encoding('UTF-8')
179
+ r_body = StringIO.new(response_body)
180
+ end
181
+
182
+ yield status_code, headers, r_body
183
+ end
184
+ else
185
+ raw_request(method, uri, request_headers, body) do |status_code, headers, r_content_type, r_body|
186
+ response_headers = headers
187
+ response_status_code = status_code
188
+ response_content_type = r_content_type
189
+ response_body = r_body.read.force_encoding('UTF-8')
190
+ r_body = StringIO.new(response_body)
191
+
192
+ yield status_code, headers, r_body
193
+ end
174
194
  end
175
195
 
176
196
  log_response(request_id, response_status_code, start_time,
@@ -290,6 +310,52 @@ module OnlinePayments::SDK
290
310
  pipe.close
291
311
  end
292
312
  end
313
+
314
+ # Makes a request using the specified method
315
+ #
316
+ # Yields a status code, an array of {OnlinePayments::SDK::ResponseHeader},
317
+ # the content_type and body
318
+ def multipart_request(method, uri, headers, body = nil)
319
+ unless body.is_a? OnlinePayments::SDK::MultipartFormDataObject
320
+ raise ArgumentError, 'body should be a MultipartFormDataObject'
321
+ end
322
+
323
+ if method != 'post' && method != 'put'
324
+ raise ArgumentError, "method #{method} is not supported"
325
+ end
326
+
327
+ connection = @http_client.send method + '_async',
328
+ uri,
329
+ body: multipart_request_body(body),
330
+ header: headers
331
+
332
+ response = connection.pop
333
+ pipe = response.content
334
+ response_headers = convert_to_sdk_response_headers(response.headers)
335
+
336
+ begin
337
+ yield response.status_code, response_headers, response.content_type, pipe
338
+ ensure
339
+ pipe.close
340
+ end
341
+ end
342
+
343
+ # Creates a request body for the multipart request
344
+ def multipart_request_body( body )
345
+ request_body = []
346
+ body.files.each do |k, v|
347
+ request_body.push :content => v.content,
348
+ 'Content-Type' => v.content_type,
349
+ 'Content-Disposition' => "form-data; name=\"#{k}\"; filename=\"#{v.file_name}\"",
350
+ 'Content-Transfer-Encoding' => 'binary'
351
+ end
352
+
353
+ body.values.each do |k, v|
354
+ request_body << { :content => v,
355
+ 'Content-Disposition' => "form-data; name=\"#{k}\"" }
356
+ end
357
+ request_body
358
+ end
293
359
  end
294
360
  end
295
361
  end
@@ -0,0 +1,44 @@
1
+ require 'securerandom'
2
+
3
+ module OnlinePayments::SDK
4
+
5
+ # A representation of a multipart/form-data object
6
+ class MultipartFormDataObject
7
+ def initialize
8
+ @boundary = SecureRandom.uuid
9
+ @content_type = 'multipart/form-data; boundary=' + @boundary
10
+ @values = {}
11
+ @files = {}
12
+ end
13
+
14
+ attr_reader :boundary
15
+ attr_reader :content_type
16
+ attr_reader :values
17
+ attr_reader :files
18
+
19
+ def add_value(parameter_name, value)
20
+ if parameter_name.nil? || parameter_name.strip.empty?
21
+ raise ArgumentError, 'parameter_name is required'
22
+ end
23
+ raise ArgumentError, 'value is required' if value.nil?
24
+ if @values.include?(parameter_name) || @files.include?(parameter_name)
25
+ raise ArgumentError, 'duplicate parameterName: ' + parameter_name
26
+ end
27
+
28
+ @values[parameter_name] = value
29
+ end
30
+
31
+ # Adds a file to the multipart Form Data Object
32
+ def add_file(parameter_name, uploadable_file)
33
+ if parameter_name.nil? || parameter_name.strip.empty?
34
+ raise ArgumentError, 'parameter_name is required'
35
+ end
36
+ raise ArgumentError, 'uploadable_file is required' if uploadable_file.nil?
37
+ if @values.include?(parameter_name) || @files.include?(parameter_name)
38
+ raise ArgumentError, 'duplicate parameterName: ' + parameter_name
39
+ end
40
+
41
+ @files[parameter_name] = uploadable_file
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ module OnlinePayments::SDK
2
+ # A representation of a multipart/form-data request
3
+ class MultipartFormDataRequest
4
+ # @return [OnlinePayments::SDK::MultipartFormDataObject]
5
+ def to_multipart_form_data_object
6
+ raise NotImplementedError
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ module OnlinePayments::SDK
2
+
3
+ # A file that can be uploaded
4
+ #
5
+ # The allowed forms of content are defined by the Connection implementation
6
+ # The default implementation supports file paths and IO objects.
7
+ #
8
+ # @attr_reader [String] file_name The name of the file.
9
+ # @attr_reader [String, IO] content The file's content.
10
+ # @attr_reader [String] content_type The file's content type.
11
+ # @attr_reader [Integer] content_length The file's content length, or -1 if not known.
12
+ class UploadableFile
13
+
14
+ def initialize(file_name, content, content_type, content_length=-1)
15
+ raise ArgumentError.new("file_name is required") if file_name.nil? or !file_name.strip
16
+ raise ArgumentError.new("content is required") if content.nil?
17
+ raise ArgumentError.new("content_type is required") if content_type.nil? or !content_type.strip
18
+
19
+ @file_name = file_name
20
+ @content = content
21
+ @content_type = content_type
22
+ @content_length = [content_length, -1].max
23
+ end
24
+
25
+ attr_reader :file_name
26
+ attr_reader :content
27
+ attr_reader :content_type
28
+ attr_reader :content_length
29
+ end
30
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'onlinepayments-sdk-ruby'
3
- spec.version = '4.16.0'
3
+ spec.version = '4.17.0'
4
4
  spec.authors = ['Worldline Direct support team']
5
5
  spec.email = ['82139942+worldline-direct-support-team@users.noreply.github.com']
6
6
  spec.summary = %q{SDK to communicate with the Online Payments platform using the Online Payments Server API}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onlinepayments-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.16.0
4
+ version: 4.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Worldline Direct support team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-25 00:00:00.000000000 Z
11
+ date: 2023-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -416,6 +416,8 @@ files:
416
416
  - lib/onlinepayments/sdk/merchant/tokens/tokens_client.rb
417
417
  - lib/onlinepayments/sdk/meta_data_provider.rb
418
418
  - lib/onlinepayments/sdk/modules.rb
419
+ - lib/onlinepayments/sdk/multipart_form_data_object.rb
420
+ - lib/onlinepayments/sdk/multipart_form_data_request.rb
419
421
  - lib/onlinepayments/sdk/not_found_exception.rb
420
422
  - lib/onlinepayments/sdk/param_request.rb
421
423
  - lib/onlinepayments/sdk/payment_platform_exception.rb
@@ -426,6 +428,7 @@ files:
426
428
  - lib/onlinepayments/sdk/request_param.rb
427
429
  - lib/onlinepayments/sdk/response_exception.rb
428
430
  - lib/onlinepayments/sdk/response_header.rb
431
+ - lib/onlinepayments/sdk/uploadable_file.rb
429
432
  - lib/onlinepayments/sdk/validation_exception.rb
430
433
  - lib/onlinepayments/sdk/webhooks.rb
431
434
  - lib/onlinepayments/sdk/webhooks/api_version_mismatch_exception.rb