Skip to content

Commit 2d9cda5

Browse files
committed
feat(api): modify upload options serialization for backend API compatibility
1 parent 62e54e5 commit 2d9cda5

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

lib/imagekit/models/beta/v2/file_upload_params.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,71 @@ class FileUploadParams < Imagekit::Internal::Type::BaseModel
99
extend Imagekit::Internal::Type::RequestParameters::Converter
1010
include Imagekit::Internal::Type::RequestParameters
1111

12+
# Serialize upload options to handle proper formatting for ImageKit backend API.
13+
# Special cases handled:
14+
# - tags: converted to comma-separated string
15+
# - responseFields: converted to comma-separated string
16+
# - extensions: JSON stringified
17+
# - customMetadata: JSON stringified
18+
# - transformation: JSON stringified
19+
#
20+
# @api private
21+
#
22+
# @param params [Object]
23+
#
24+
# @return [Array(Object, Hash{Symbol=>Object})]
25+
def self.dump_request(params)
26+
state = {can_retry: true}
27+
case (dumped = dump(params, state: state))
28+
in Hash
29+
serialized = serialize_upload_options(dumped)
30+
options = Imagekit::Internal::Util.coerce_hash!(serialized[:request_options]).to_h
31+
request_options = state.fetch(:can_retry) ? options : {**options, max_retries: 0}
32+
[serialized.except(:request_options), request_options]
33+
else
34+
[dumped, nil]
35+
end
36+
end
37+
38+
# @api private
39+
#
40+
# @param upload_options [Hash{Symbol=>Object}]
41+
#
42+
# @return [Hash{Symbol=>Object}]
43+
def self.serialize_upload_options(upload_options)
44+
serialized = {}
45+
46+
upload_options.each do |key, value|
47+
# Skip nil values
48+
if value.nil?
49+
serialized[key] = value
50+
next
51+
end
52+
53+
serialized[key] = case key
54+
when :tags
55+
# Tags should be comma-separated string
56+
value.is_a?(Array) ? value.join(',') : value
57+
when :responseFields
58+
# Response fields should be comma-separated string
59+
value.is_a?(Array) ? value.join(',') : value
60+
when :extensions
61+
# Extensions should be JSON stringified
62+
value.is_a?(Array) ? JSON.generate(value) : value
63+
when :customMetadata
64+
# Custom metadata should be JSON stringified
65+
value.is_a?(Hash) ? JSON.generate(value) : value
66+
when :transformation
67+
# Transformation should be JSON stringified
68+
(value.is_a?(Hash) || value.respond_to?(:to_h)) ? JSON.generate(value) : value
69+
else
70+
value
71+
end
72+
end
73+
74+
serialized
75+
end
76+
1277
# @!attribute file
1378
# The API accepts any of the following:
1479
#

lib/imagekit/models/file_upload_params.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,71 @@ class FileUploadParams < Imagekit::Internal::Type::BaseModel
77
extend Imagekit::Internal::Type::RequestParameters::Converter
88
include Imagekit::Internal::Type::RequestParameters
99

10+
# Serialize upload options to handle proper formatting for ImageKit backend API.
11+
# Special cases handled:
12+
# - tags: converted to comma-separated string
13+
# - responseFields: converted to comma-separated string
14+
# - extensions: JSON stringified
15+
# - customMetadata: JSON stringified
16+
# - transformation: JSON stringified
17+
#
18+
# @api private
19+
#
20+
# @param params [Object]
21+
#
22+
# @return [Array(Object, Hash{Symbol=>Object})]
23+
def self.dump_request(params)
24+
state = {can_retry: true}
25+
case (dumped = dump(params, state: state))
26+
in Hash
27+
serialized = serialize_upload_options(dumped)
28+
options = Imagekit::Internal::Util.coerce_hash!(serialized[:request_options]).to_h
29+
request_options = state.fetch(:can_retry) ? options : {**options, max_retries: 0}
30+
[serialized.except(:request_options), request_options]
31+
else
32+
[dumped, nil]
33+
end
34+
end
35+
36+
# @api private
37+
#
38+
# @param upload_options [Hash{Symbol=>Object}]
39+
#
40+
# @return [Hash{Symbol=>Object}]
41+
def self.serialize_upload_options(upload_options)
42+
serialized = {}
43+
44+
upload_options.each do |key, value|
45+
# Skip nil values
46+
if value.nil?
47+
serialized[key] = value
48+
next
49+
end
50+
51+
serialized[key] = case key
52+
when :tags
53+
# Tags should be comma-separated string
54+
value.is_a?(Array) ? value.join(',') : value
55+
when :responseFields
56+
# Response fields should be comma-separated string
57+
value.is_a?(Array) ? value.join(',') : value
58+
when :extensions
59+
# Extensions should be JSON stringified
60+
value.is_a?(Array) ? JSON.generate(value) : value
61+
when :customMetadata
62+
# Custom metadata should be JSON stringified
63+
value.is_a?(Hash) ? JSON.generate(value) : value
64+
when :transformation
65+
# Transformation should be JSON stringified
66+
(value.is_a?(Hash) || value.respond_to?(:to_h)) ? JSON.generate(value) : value
67+
else
68+
value
69+
end
70+
end
71+
72+
serialized
73+
end
74+
1075
# @!attribute file
1176
# The API accepts any of the following:
1277
#

0 commit comments

Comments
 (0)