speakeasy_client_sdk_ruby 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sdk/apiendpoints.rb +338 -0
  3. data/lib/sdk/apis.rb +264 -0
  4. data/lib/sdk/embeds.rb +128 -0
  5. data/lib/sdk/metadata.rb +144 -0
  6. data/lib/sdk/models/operations/deleteapi.rb +52 -0
  7. data/lib/sdk/models/operations/deleteapiendpoint.rb +55 -0
  8. data/lib/sdk/models/operations/deleteschema.rb +55 -0
  9. data/lib/sdk/models/operations/deleteversionmetadata.rb +58 -0
  10. data/lib/sdk/models/operations/downloadschema.rb +55 -0
  11. data/lib/sdk/models/operations/downloadschemarevision.rb +58 -0
  12. data/lib/sdk/models/operations/findapiendpoint.rb +59 -0
  13. data/lib/sdk/models/operations/generateopenapispec.rb +56 -0
  14. data/lib/sdk/models/operations/generateopenapispecforapiendpoint.rb +59 -0
  15. data/lib/sdk/models/operations/generatepostmancollection.rb +55 -0
  16. data/lib/sdk/models/operations/generatepostmancollectionforapiendpoint.rb +58 -0
  17. data/lib/sdk/models/operations/generaterequestpostmancollection.rb +52 -0
  18. data/lib/sdk/models/operations/getallapiendpoints.rb +52 -0
  19. data/lib/sdk/models/operations/getallapiversions.rb +72 -0
  20. data/lib/sdk/models/operations/getallforversionapiendpoints.rb +55 -0
  21. data/lib/sdk/models/operations/getapiendpoint.rb +59 -0
  22. data/lib/sdk/models/operations/getapis.rb +69 -0
  23. data/lib/sdk/models/operations/getembedaccesstoken.rb +60 -0
  24. data/lib/sdk/models/operations/getplugins.rb +38 -0
  25. data/lib/sdk/models/operations/getrequestfromeventlog.rb +53 -0
  26. data/lib/sdk/models/operations/getschema.rb +56 -0
  27. data/lib/sdk/models/operations/getschemadiff.rb +62 -0
  28. data/lib/sdk/models/operations/getschemarevision.rb +59 -0
  29. data/lib/sdk/models/operations/getschemas.rb +55 -0
  30. data/lib/sdk/models/operations/getvalidembedaccesstokens.rb +38 -0
  31. data/lib/sdk/models/operations/getversionmetadata.rb +55 -0
  32. data/lib/sdk/models/operations/insertversionmetadata.rb +60 -0
  33. data/lib/sdk/models/operations/queryeventlog.rb +53 -0
  34. data/lib/sdk/models/operations/registerschema.rb +86 -0
  35. data/lib/sdk/models/operations/revokeembedaccesstoken.rb +49 -0
  36. data/lib/sdk/models/operations/runplugin.rb +56 -0
  37. data/lib/sdk/models/operations/upsertapi.rb +57 -0
  38. data/lib/sdk/models/operations/upsertapiendpoint.rb +63 -0
  39. data/lib/sdk/models/operations/upsertplugin.rb +39 -0
  40. data/lib/sdk/models/operations/validateapikey.rb +35 -0
  41. data/lib/sdk/models/shared/api.rb +69 -0
  42. data/lib/sdk/models/shared/apiendpoint.rb +84 -0
  43. data/lib/sdk/models/shared/boundedrequest.rb +64 -0
  44. data/lib/sdk/models/shared/embedaccesstokenresponse.rb +25 -0
  45. data/lib/sdk/models/shared/embedtoken.rb +52 -0
  46. data/lib/sdk/models/shared/error.rb +28 -0
  47. data/lib/sdk/models/shared/filter.rb +31 -0
  48. data/lib/sdk/models/shared/filters.rb +34 -0
  49. data/lib/sdk/models/shared/generateopenapispecdiff.rb +28 -0
  50. data/lib/sdk/models/shared/plugin.rb +43 -0
  51. data/lib/sdk/models/shared/requestmetadata.rb +28 -0
  52. data/lib/sdk/models/shared/schema.rb +40 -0
  53. data/lib/sdk/models/shared/schemadiff.rb +48 -0
  54. data/lib/sdk/models/shared/security.rb +25 -0
  55. data/lib/sdk/models/shared/unboundedrequest.rb +37 -0
  56. data/lib/sdk/models/shared/versionmetadata.rb +57 -0
  57. data/lib/sdk/plugins.rb +140 -0
  58. data/lib/sdk/requests.rb +136 -0
  59. data/lib/sdk/schemas.rb +334 -0
  60. data/lib/sdk/sdk.rb +173 -0
  61. data/lib/sdk/utils/metadata_fields.rb +154 -0
  62. data/lib/sdk/utils/retries.rb +130 -0
  63. data/lib/sdk/utils/utils.rb +769 -0
  64. data/lib/speakeasy_client_sdk_ruby.rb +70 -0
  65. metadata +206 -0
@@ -0,0 +1,769 @@
1
+ # Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
2
+
3
+ # typed: true
4
+ # frozen_string_literal: true
5
+
6
+ require 'date'
7
+ require 'sorbet-runtime'
8
+ require 'base64'
9
+ require_relative './metadata_fields'
10
+
11
+ module SpeakeasyClientSDK
12
+ module Utils
13
+ extend T::Sig
14
+
15
+ class FieldAugmented
16
+ include MetadataFields
17
+ end
18
+
19
+ sig { params(val: Object, primitives: T::Boolean).returns(Object) }
20
+ def self.val_to_string(val, primitives: true)
21
+ if val.is_a? T::Enum
22
+ val.serialize
23
+ elsif val.is_a? DateTime
24
+ val.strftime('%Y-%m-%dT%H:%M:%S.%NZ')
25
+ elsif primitives
26
+ val.to_s
27
+ else
28
+ val
29
+ end
30
+ end
31
+
32
+ sig { params(headers_params: FieldAugmented).returns(T::Hash[Symbol, String]) }
33
+ def self.get_headers(headers_params)
34
+ return {} if headers_params.nil?
35
+
36
+ headers = {}
37
+ param_fields = headers_params.fields
38
+ param_fields.each do |f|
39
+ metadata = f.metadata[:header]
40
+ next if metadata.nil?
41
+
42
+ value = _serialize_header(metadata.fetch(:explode, false), headers_params.send(f.name))
43
+ headers[metadata.fetch(:field_name, f.name)] = value if !value.empty?
44
+ end
45
+ headers
46
+ end
47
+
48
+ sig { params(explode: T::Boolean, obj: Object).returns(String) }
49
+ def self._serialize_header(explode, obj)
50
+ return '' if obj.nil?
51
+
52
+ if obj.respond_to? :fields
53
+ items = []
54
+ obj_fields = obj.fields
55
+ obj_fields.each do |obj_field|
56
+ obj_param_metadata = obj_field.metadata[:header]
57
+ next if obj_param_metadata.nil?
58
+
59
+ obj_field_name = obj_param_metadata.fetch(:field_name, obj_field.name)
60
+ next if obj_field_name == ''
61
+
62
+ val = obj.send(obj_field.name)
63
+ next if val.nil?
64
+
65
+ if explode
66
+ items.append("#{obj_field_name}=#{val_to_string(val)}")
67
+ else
68
+ items.append(obj_field_name)
69
+ items.append(val_to_string(val))
70
+ end
71
+ end
72
+
73
+ return items.join(',') if !items.empty?
74
+ elsif obj.is_a? Hash
75
+ items = []
76
+ obj.each do |key, value|
77
+ next if value.nil?
78
+
79
+ if explode
80
+ items.append("#{key}=#{val_to_string(value)}")
81
+ else
82
+ items.append(key)
83
+ items.append(val_to_string(value))
84
+ end
85
+ end
86
+ return items.join(',') if !items.empty?
87
+
88
+ elsif obj.is_a? Array
89
+ items = obj.filter { |v| !v.nil? }.map { |v| val_to_string(v) }.join(',')
90
+ else
91
+ val_to_string(obj)
92
+ end
93
+ end
94
+
95
+ sig do
96
+ params(field_name: String, explode: T::Boolean, obj: Object, delimiter: String,
97
+ get_field_name_lambda: T.proc.params(obj_field: MetadataFields::Field).returns(String))
98
+ .returns(T::Hash[Symbol, T::Array[String]])
99
+ end
100
+ def self._populate_form(field_name, explode, obj, delimiter, &get_field_name_lambda)
101
+ params = {}
102
+
103
+ return params if obj.nil?
104
+
105
+ if obj.respond_to? :fields
106
+ items = []
107
+ obj_fields = obj.fields
108
+ obj_fields.each do |obj_field|
109
+ obj_field_name = get_field_name_lambda.call(obj_field)
110
+ next if obj_field_name == ''
111
+
112
+ val = obj.send(obj_field.name.to_sym)
113
+ next if val.nil?
114
+
115
+ if explode
116
+ params[obj_field_name] = [val_to_string(val)]
117
+ else
118
+ items.append("#{obj_field_name}#{delimiter}#{val_to_string(val)}")
119
+ end
120
+ end
121
+
122
+ params[field_name] = [items.join(delimiter)] if !items.empty?
123
+ elsif obj.is_a? Hash
124
+ items = []
125
+ obj.each do |key, value|
126
+ next if value.nil?
127
+
128
+ if explode
129
+ params[key] = val_to_string(value)
130
+ else
131
+ items.append("#{key}#{delimiter}#{val_to_string(value)}")
132
+ end
133
+ end
134
+ params[field_name] = [items.join(delimiter)] if !items.empty?
135
+
136
+ elsif obj.is_a? Array
137
+ items = []
138
+ obj.each do |value|
139
+ next if value.nil?
140
+
141
+ if explode
142
+ params[field_name] = [] if !params.key? field_name
143
+
144
+ params[field_name].append(val_to_string(value))
145
+ else
146
+ items.append(val_to_string(value))
147
+ end
148
+ end
149
+ params[field_name] = items.map(&:to_s).join(delimiter) if !items.empty?
150
+
151
+ else
152
+ params[field_name] = val_to_string(obj)
153
+ end
154
+ params
155
+ end
156
+
157
+ sig do
158
+ params(metadata: T::Hash[Symbol, String], field_name: String, obj: Object)
159
+ .returns(T::Hash[Symbol, T::Array[String]])
160
+ end
161
+ def self._get_deep_object_query_params(metadata, field_name, obj)
162
+ params = {}
163
+
164
+ return params if obj.nil?
165
+
166
+ if obj.respond_to? :fields
167
+ obj_fields = obj.fields
168
+ obj_fields.each do |obj_field|
169
+ obj_param_metadata = obj_field.metadata[:query_param]
170
+ next if obj_param_metadata.nil?
171
+
172
+ val = obj.send(obj_field.name)
173
+ next if val.nil?
174
+
175
+ key = "#{metadata.fetch(:field_name, field_name)}[#{obj_param_metadata.fetch(:field_name, obj_field.name)}]"
176
+ if val.is_a? Array
177
+ val.each do |v|
178
+ next if v.nil?
179
+
180
+ params[key] = [] if !params.include? key
181
+
182
+ params[key] << val_to_string(v)
183
+ end
184
+ else
185
+ params[key] = [val_to_string(val)]
186
+ end
187
+ end
188
+ elsif obj.is_a? Hash
189
+ obj.each do |key, value|
190
+ next if value.nil?
191
+
192
+ param_key = "#{metadata.fetch(:field_name, field_name)}[#{key}]"
193
+ if value.is_a? Array
194
+ value.each do |val|
195
+ next if val.nil?
196
+
197
+ params[param_key] = [] if !params.include? param_key
198
+
199
+ params[param_key].append(val_to_string(val))
200
+ end
201
+ else
202
+ params[param_key] = [val_to_string(value)]
203
+ end
204
+ end
205
+ end
206
+ params
207
+ end
208
+
209
+ sig do
210
+ params(metadata: T::Hash[Symbol, String], field_name: String, obj: Object)
211
+ .returns(T::Hash[Symbol, T::Array[String]])
212
+ end
213
+ def self._get_serialized_params(metadata, field_name, obj)
214
+ params = {}
215
+
216
+ serialization = metadata.fetch(:serialization, '')
217
+ params[metadata.fetch(:field_name, field_name)] = obj.marshal_json if serialization == 'json'
218
+
219
+ params
220
+ end
221
+
222
+ sig do
223
+ params(metadata: T::Hash[Symbol, String], field_name: String, obj: Object, delimiter: String)
224
+ .returns(T::Hash[Symbol, T::Array[String]])
225
+ end
226
+ def self._get_delimited_query_params(metadata, field_name, obj, delimiter)
227
+ get_query_param_field_name = lambda do |obj_field|
228
+ obj_param_metadata = obj_field.metadata[:query_param]
229
+
230
+ return '' if obj_param_metadata.nil?
231
+
232
+ return obj_param_metadata.fetch(:field_name, obj_field.name)
233
+ end
234
+
235
+ _populate_form(field_name, metadata.fetch(:explode, true), obj, delimiter, &get_query_param_field_name)
236
+ end
237
+
238
+ sig { params(clazz: Class, query_params: FieldAugmented, gbls: T::Hash[Symbol, T::Hash[Symbol, T::Hash[Symbol, Object]]]).returns(T::Hash[Symbol, T::Array[String]]) }
239
+ def self.get_query_params(clazz, query_params, gbls)
240
+ params = {}
241
+ param_fields = clazz.fields
242
+ param_fields.each do |f|
243
+ request_metadata = f.metadata[:request]
244
+ next if !request_metadata.nil?
245
+
246
+ metadata = f.metadata[:query_param]
247
+ next if metadata.nil?
248
+
249
+ param_name = f.name
250
+ value = query_params&.send(param_name.to_sym)
251
+ value = _populate_from_globals(param_name, value, 'queryParam', gbls)
252
+
253
+ f_name = metadata[:field_name]
254
+ serialization = metadata.fetch(:serialization, '')
255
+ if serialization != ''
256
+ params = params.merge _get_serialized_params(
257
+ metadata, f_name, value
258
+ )
259
+ else
260
+ style = metadata.fetch(:style, 'form')
261
+ case style
262
+ when 'deepObject'
263
+ params = params.merge _get_deep_object_query_params(
264
+ metadata, f_name, value
265
+ )
266
+ when 'form'
267
+ params = params.merge _get_delimited_query_params(
268
+ metadata, f_name, value, ','
269
+ )
270
+ when 'pipeDelimited'
271
+ params = params.merge _get_delimited_query_params(
272
+ metadata, f_name, value, '|'
273
+ )
274
+ else
275
+ raise StandardError, 'not yet implemented'
276
+ end
277
+ end
278
+ end
279
+ params
280
+ end
281
+
282
+ sig { params(clazz: Class, server_url: String, path: String, path_params: FieldAugmented, gbls: T::Hash[Symbol, T::Hash[Symbol, T::Hash[Symbol, Object]]]).returns(String) }
283
+ def self.generate_url(clazz, server_url, path, path_params, gbls)
284
+ clazz.fields.each do |f|
285
+ param_metadata = f.metadata[:path_param]
286
+ next if param_metadata.nil?
287
+
288
+ if param_metadata.fetch(:style, 'simple') == 'simple'
289
+ param = path_params.send(f.name) if !path_params.nil?
290
+ param = _populate_from_globals(f.name, param, 'pathParam', gbls)
291
+ end
292
+
293
+ f_name = param_metadata.fetch(:field_name, f.name)
294
+ serialization = param_metadata.fetch(:serialization, '')
295
+ if serialization != ''
296
+ serialized_params = _get_serialized_params(param_metadata, f_name, param)
297
+ serialized_params.each do |k, v|
298
+ path = path.sub("{#{k}}", v)
299
+ end
300
+ else
301
+ if param.is_a? Array
302
+ pp_vals = []
303
+ param.each do |pp_val|
304
+ pp_vals.append(pp_val.to_s)
305
+ end
306
+ path = path.sub("{#{param_metadata.fetch(:field_name, f.name)}}", pp_vals.join(','))
307
+ elsif param.is_a? Hash
308
+ pp_vals = []
309
+ param.each do |pp_key, pp_val|
310
+ value = val_to_string(pp_val)
311
+
312
+ if param_metadata.fetch(:explode, false)
313
+ pp_vals.append("#{pp_key}=#{value}")
314
+ else
315
+ pp_vals.append("#{pp_key},#{value}")
316
+ end
317
+ end
318
+ path = path.sub("{#{param_metadata.fetch(:field_name, f.name)}}", pp_vals.join(','))
319
+ elsif !(param.is_a?(String) || param.is_a?(Integer) ||
320
+ param.is_a?(Float) || param.is_a?(Complex) || param.is_a?(TrueClass) ||
321
+ param.is_a?(FalseClass))
322
+ pp_vals = []
323
+ attrs = param.fields.filter { |field| field.name && param.respond_to?(field.name.to_sym) }.map(&:name)
324
+ attrs.each do |attr|
325
+ field = param.field(attr)
326
+
327
+ param_value_metadata = field.metadata[:path_param]
328
+
329
+ next if param_value_metadata.nil?
330
+
331
+ parm_name = param_value_metadata.fetch(:field_name, f.name)
332
+
333
+ param_field_val = param.send(attr)
334
+
335
+ if param_field_val.is_a? T::Enum
336
+ param_field_val = param_field_val.serialize
337
+ elsif param_field_val.is_a? DateTime
338
+ param_field_val = param_field_val.strftime('%Y-%m-%dT%H:%M:%S.%NZ')
339
+ end
340
+ if !field.nil? && T::Utils::Nilable.is_union_with_nilclass(field.type) && param_field_val.nil?
341
+ next
342
+ elsif param_metadata.fetch(:explode, false)
343
+ pp_vals.append("#{parm_name}=#{param_field_val}")
344
+ else
345
+ pp_vals.append("#{parm_name},#{param_field_val}")
346
+ end
347
+ end
348
+ path = path.sub("{#{param_metadata.fetch(:field_name, f.name)}}", pp_vals.join(','))
349
+ else
350
+ path = path.sub("{#{param_metadata.fetch(:field_name, f.name)}}", param.to_s)
351
+ end
352
+ end
353
+ end
354
+
355
+ server_url.delete_suffix('/') + path
356
+ end
357
+
358
+ sig { params(content_type: String, pattern: String).returns(T::Boolean) }
359
+ def self.match_content_type(content_type, pattern)
360
+ return true if content_type == pattern || pattern == '*' || pattern == '*/*'
361
+
362
+ pieces = content_type.split(';')
363
+ pieces.each do |piece|
364
+ return true if pattern == piece.strip
365
+ end
366
+
367
+ false
368
+ end
369
+
370
+ sig { params(req: Faraday::Request, security: Object).void }
371
+ def self.configure_request_security(req, security)
372
+ sec_fields = security.fields
373
+ sec_fields.each do |sec_field|
374
+ value = security.send(sec_field.name)
375
+ next if value.nil?
376
+
377
+ metadata = sec_field.metadata[:security]
378
+ next if metadata.nil?
379
+
380
+ _parse_security_option(req, value) if metadata[:option]
381
+
382
+ if metadata[:scheme]
383
+ # Special case for basic auth which could be a flattened struct
384
+ if metadata[:sub_type] == 'basic' && !value.respond_to?(:fields)
385
+ _parse_security_scheme(req, metadata, security)
386
+ else
387
+ _parse_security_scheme(req, metadata, value)
388
+ end
389
+ end
390
+ end
391
+ end
392
+
393
+ sig { params(req: Faraday::Request, option: Object).void }
394
+ def self._parse_security_option(req, option)
395
+ opt_fields = option.fields
396
+ opt_fields.each do |opt_field|
397
+ metadata = opt_field.metadata[:security]
398
+ next if metadata.nil? || !metadata.include?(:scheme)
399
+
400
+ _parse_security_scheme(req, metadata, option.send(opt_field.name))
401
+ end
402
+ end
403
+
404
+ sig { params(req: Faraday::Request, scheme_metadata: T::Hash[Symbol, String], scheme: Object).void }
405
+ def self._parse_security_scheme(req, scheme_metadata, scheme)
406
+ scheme_type = scheme_metadata[:type]
407
+ sub_type = scheme_metadata[:sub_type]
408
+
409
+ if scheme.respond_to? :fields
410
+ if scheme_type == 'http' && sub_type == 'basic'
411
+ _parse_basic_auth_scheme(req, scheme)
412
+ return
413
+ end
414
+
415
+ scheme_fields = scheme.fields
416
+ scheme_fields.each do |field|
417
+ metadata = field.metadata[:security]
418
+ next if metadata.nil? || metadata[:field_name].nil?
419
+
420
+ value = scheme.send(field.name)
421
+ _parse_security_scheme_value(req, scheme_metadata, metadata, value)
422
+ end
423
+ else
424
+ _parse_security_scheme_value(req, scheme_metadata, scheme_metadata, scheme)
425
+ end
426
+ end
427
+
428
+ sig do
429
+ params(req: Faraday::Request, scheme_metadata: T::Hash[Symbol, String],
430
+ security_metadata: T::Hash[Symbol, String], value: Object).void
431
+ end
432
+ def self._parse_security_scheme_value(req, scheme_metadata, security_metadata, value)
433
+ scheme_type = scheme_metadata[:type]
434
+ sub_type = scheme_metadata[:sub_type]
435
+
436
+ header_name = security_metadata[:field_name]
437
+
438
+ case scheme_type
439
+ when 'apiKey'
440
+ case sub_type
441
+ when 'header'
442
+ req.headers[header_name] = value
443
+ when 'query'
444
+ req.params[header_name] = value
445
+ when 'cookie'
446
+ req.cookies[header_name] = value
447
+ else
448
+ raise StandardError, 'not supported'
449
+ end
450
+ when 'openIdConnect'
451
+ req.headers[header_name] = value
452
+ when 'oauth2'
453
+ req.headers[header_name] = value
454
+ when 'http'
455
+ if sub_type == 'bearer'
456
+ req.headers[header_name] = value.downcase.start_with?('bearer ') ? value : "Bearer #{value}"
457
+ else
458
+ raise StandardError, 'not supported'
459
+ end
460
+ else
461
+ raise StandardError, 'not supported'
462
+ end
463
+ end
464
+
465
+ sig { params(req: Faraday::Request, scheme: FieldAugmented).void }
466
+ def self._parse_basic_auth_scheme(req, scheme)
467
+ username, password = ''
468
+
469
+ scheme_fields = scheme.fields
470
+ scheme_fields.each do |scheme_field|
471
+ metadata = scheme_field.metadata[:security]
472
+ next if metadata.nil? || !metadata.include?(:field_name)
473
+
474
+ field_name = metadata[:field_name]
475
+ value = scheme.send(scheme_field.name)
476
+
477
+ username = value if field_name == 'username'
478
+ password = value if field_name == 'password'
479
+ end
480
+
481
+ data = "#{username}:#{password}".encode
482
+ req.headers['Authorization'] = "Basic #{Base64.encode64(data)}"
483
+ end
484
+
485
+ sig { params(optional: T::Boolean).returns(T.proc.params(s: String).returns(DateTime)) }
486
+ def self.datetime_from_iso_format(optional)
487
+ lambda do |s|
488
+ return nil if optional && s.nil?
489
+
490
+ return DateTime.strptime(s, '%Y-%m-%dT%H:%M:%S.%NZ')
491
+ end
492
+ end
493
+
494
+ sig { params(optional: T::Boolean).returns(T.proc.params(s: String).returns(Date)) }
495
+ def self.date_from_iso_format(optional)
496
+ lambda do |s|
497
+ return nil if optional && s.nil?
498
+
499
+ return Date.iso8601(s)
500
+ end
501
+ end
502
+
503
+ sig do
504
+ params(enum_type: T.class_of(T::Enum), optional: T::Boolean)
505
+ .returns(T.proc.params(s: String).returns(T::Enum))
506
+ end
507
+ def self.enum_from_string(enum_type, optional)
508
+ lambda do |s|
509
+ return nil if optional && s.nil?
510
+
511
+ return enum_type.deserialize(s)
512
+ end
513
+ end
514
+
515
+ sig { params(name: String).returns(T.proc.returns(String)) }
516
+ def self.field_name(name)
517
+ proc { |_, field_name = name| field_name }
518
+ end
519
+
520
+ SERIALIZATION_METHOD_TO_CONTENT_TYPE = {
521
+ 'json': 'application/json',
522
+ 'form': 'application/x-www-form-urlencoded',
523
+ 'multipart': 'multipart/form-data',
524
+ 'raw': 'application/octet-stream',
525
+ 'string': 'text/plain'
526
+ }.freeze
527
+
528
+ sig do
529
+ params(request: Object, request_field_name: Symbol, serialization_method: Symbol)
530
+ .returns([String, Object, Object])
531
+ end
532
+ def self.serialize_request_body(request, request_field_name, serialization_method)
533
+ return ['', nil, nil] if request.nil?
534
+
535
+ return serialize_content_type(request_field_name, SERIALIZATION_METHOD_TO_CONTENT_TYPE[serialization_method], request) if !request.respond_to?(:fields) || !request.respond_to?(request_field_name)
536
+
537
+ request_val = request.send(request_field_name)
538
+
539
+ request_fields = request.fields
540
+ request_metadata = nil
541
+ request_fields.each do |f|
542
+ if f.name == request_field_name
543
+ request_metadata = f.metadata[:request]
544
+ break
545
+ end
546
+ end
547
+ raise StandardError, 'invalid request type' if request_metadata.nil?
548
+
549
+ serialize_content_type(
550
+ :request, request_metadata.fetch(:media_type, 'application/octet-stream'), request_val
551
+ )
552
+ end
553
+
554
+ sig do
555
+ params(field_name: Symbol, media_type: String, request: Object)
556
+ .returns([String, Object, T.nilable(T::Array[T::Array[Object]])])
557
+ end
558
+ def self.serialize_content_type(field_name, media_type, request)
559
+ return media_type, marshal_json_complex(request), nil if media_type.match('(application|text)\/.*?\+*json.*')
560
+ return serialize_multipart_form(media_type, request) if media_type.match('multipart\/.*')
561
+ return media_type, serialize_form_data(field_name, request), nil if media_type.match('application\/x-www-form-urlencoded.*')
562
+ return media_type, request, nil if request.is_a?(String) || request.is_a?(Array)
563
+
564
+ raise StandardError, "invalid request body type #{type(request)} for mediaType {metadata['media_type']}"
565
+ end
566
+
567
+ sig { params(field: MetadataFields::Field, data_class: FieldAugmented).returns(Object) }
568
+ def self.parse_field(field, data_class)
569
+ field_metadata = field.metadata[:metadata_string]
570
+ return nil if field_metadata.nil?
571
+
572
+ field_value = data_class.send(field.name)
573
+ return nil if field_value.nil?
574
+
575
+ field_value
576
+ end
577
+
578
+ sig { params(media_type: String, request: FieldAugmented).returns([String, Object, T::Array[T::Array[Object]]]) }
579
+ def self.serialize_multipart_form(media_type, request)
580
+ form = []
581
+ request_fields = request.fields
582
+ request_fields.each do |field|
583
+ val = request.send(field.name)
584
+ next if val.nil?
585
+
586
+ field_metadata = field.metadata[:multipart_form]
587
+ next if field_metadata.nil?
588
+
589
+ if field_metadata[:file] == true
590
+ file_fields = val.fields
591
+
592
+ file_name = ''
593
+ field_name = ''
594
+ content = nil
595
+
596
+ file_fields.each do |file_field|
597
+ file_metadata = file_field.metadata[:multipart_form]
598
+ next if file_metadata.nil?
599
+
600
+ if file_metadata[:content] == true
601
+ content = val.send(file_field.name)
602
+ else
603
+ field_name = file_metadata.fetch(:field_name, file_field.name)
604
+ file_name = val.send(file_field.name)
605
+ end
606
+ end
607
+ raise StandardError, 'invalid multipart/form-data file' if field_name == '' || file_name == '' || content == nil?
608
+
609
+ form.append([field_name, [file_name, content]])
610
+ elsif field_metadata[:json] == true
611
+ to_append = [
612
+ field_metadata.fetch(:field_name, field.name), [
613
+ nil, marshal_json_complex(val), 'application/json'
614
+ ]
615
+ ]
616
+ form.append(to_append)
617
+ else
618
+ field_name = field_metadata.fetch(
619
+ :field_name, field.name
620
+ )
621
+ if val.is_a? Array
622
+ val.each do |value|
623
+ next if value.nil?
624
+
625
+ form.append(
626
+ ["#{field_name}[]", [nil, val_to_string(value)]]
627
+ )
628
+ end
629
+ else
630
+ form.append([field_name, [nil, val_to_string(val)]])
631
+ end
632
+ end
633
+ end
634
+ [media_type, nil, form]
635
+ end
636
+
637
+ sig do
638
+ params(form: T::Array[T::Array[T.any(String, T::Array[T.nilable(String)])]])
639
+ .returns(T::Hash[Symbol, Object])
640
+ end
641
+ def self.encode_form(form)
642
+ payload = {}
643
+ form.each do |field_name, field|
644
+ if field.length == 2
645
+ if field[0].nil?
646
+ payload[field_name] = field[1]
647
+ else
648
+ payload[field_name] = Faraday::Multipart::FilePart.new(field[0], '', field[1])
649
+ end
650
+ elsif field.length == 3
651
+ payload[field_name] = Faraday::Multipart::ParamPart.new(field[1].to_json, field[2])
652
+ end
653
+ end
654
+ payload
655
+ end
656
+
657
+ sig do
658
+ params(field_name: Symbol, data: T.any(FieldAugmented, T::Hash[Symbol, String]))
659
+ .returns(T::Hash[Symbol, Object])
660
+ end
661
+ def self.serialize_form_data(field_name, data)
662
+ get_form_field_name = lambda do |obj_field|
663
+ obj_param_metadata = obj_field.metadata[:form]
664
+
665
+ return '' if obj_param_metadata.nil?
666
+
667
+ return obj_param_metadata.fetch(:field_name, obj_field.name)
668
+ end
669
+
670
+ form = {}
671
+ if data.respond_to? :fields
672
+ data.fields.each do |field|
673
+ val = data.send(field.name)
674
+ next if val.nil?
675
+
676
+ metadata = field.metadata[:form]
677
+
678
+ next if metadata.nil?
679
+
680
+ field_name = metadata.fetch(:field_name, field.name)
681
+
682
+ if metadata[:json]
683
+ form[field_name] = marshal_json_complex(val)
684
+ else
685
+ if metadata.fetch(:style, 'form') == 'form'
686
+ form = form.merge(
687
+ _populate_form(
688
+ field_name, metadata.fetch(:explode, true), val, ',', &get_form_field_name
689
+ )
690
+ )
691
+ else
692
+ raise StandardError, "Invalid form style for field #{field.name}"
693
+ end
694
+ end
695
+ end
696
+ elsif data.is_a? Hash
697
+ data.each do |key, value|
698
+ form[key] = [val_to_string(value)]
699
+ end
700
+ else
701
+ raise StandardError, "Invalid request body type for field #{field_name}"
702
+ end
703
+
704
+ form
705
+ end
706
+
707
+ sig { params(url_with_params: String, params: T::Hash[Symbol, String]).returns(String) }
708
+ def self.template_url(url_with_params, params)
709
+ params.each do |key, value|
710
+ url_with_params = url_with_params.gsub("{#{key}}", value)
711
+ end
712
+
713
+ url_with_params
714
+ end
715
+
716
+ sig { params(param_name: Symbol, value: Object, param_type: String, gbls: T::Hash[Symbol, T::Hash[Symbol, T::Hash[Symbol, Object]]]).returns(Object) }
717
+ def self._populate_from_globals(param_name, value, param_type, gbls)
718
+ if value.nil? && !gbls.nil?
719
+ global_value = gbls.dig(:parameters, param_type.to_sym, param_name.to_sym)
720
+ value = global_value if !global_value.nil?
721
+ end
722
+ value
723
+ end
724
+
725
+ sig { params(complex: Object).returns(Object) }
726
+ def self.marshal_json_complex(complex)
727
+ if complex.is_a? Array
728
+ complex.map { |v| Utils.marshal_json_complex(v) }.to_json
729
+ elsif complex.is_a? Hash
730
+ complex.transform_values { |v| Utils.marshal_json_complex(v) }.to_json
731
+ elsif complex.respond_to? :marshal_json
732
+ complex.marshal_json
733
+ else
734
+ complex.to_json
735
+ end
736
+ end
737
+
738
+ sig { params(data: String, type: Object).returns(Object) }
739
+ def self.unmarshal_complex(data, type)
740
+ begin
741
+ value = unmarshal_json(JSON.parse(data), type)
742
+ rescue TypeError, JSON::ParserError
743
+ value = unmarshal_json(data, type)
744
+ end
745
+ value
746
+ end
747
+
748
+ sig { params(data: Object, type: Object).returns(Object) }
749
+ def self.unmarshal_json(data, type)
750
+ if type.respond_to? :unmarshal_json
751
+ type.unmarshal_json(data)
752
+ elsif type.to_s.start_with? 'T::Array'
753
+ if type.type.respond_to? :raw_type
754
+ data.map { |v| Utils.unmarshal_complex(v, type.type.raw_type) }
755
+ else
756
+ data.map { |v| Utils.unmarshal_complex(v, type.type) }
757
+ end
758
+ elsif type.to_s.start_with? 'T::Hash'
759
+ if type.type.types[1].respond_to? :raw_type
760
+ data.transform_values { |v| Utils.unmarshal_complex(v, type.type.types[1].raw_type) }
761
+ else
762
+ data.transform_values { |v| Utils.unmarshal_complex(v, type.type.types[1]) }
763
+ end
764
+ else
765
+ data
766
+ end
767
+ end
768
+ end
769
+ end