decoupled form and multipart transcoder, moved check to Request::Body

This commit is contained in:
HoneyryderChuck 2025-09-01 23:24:59 +01:00
parent 6f71bd4a5b
commit de153bc995
5 changed files with 33 additions and 20 deletions

View File

@ -127,8 +127,13 @@ module HTTPX
# @type var body: bodyIO
Transcoder::Body.encode(body)
elsif (form = params.delete(:form))
# @type var form: Transcoder::urlencoded_input
Transcoder::Form.encode(form)
if Transcoder::Multipart.multipart?(form)
# @type var form: Transcoder::multipart_input
Transcoder::Multipart.encode(form)
else
# @type var form: Transcoder::urlencoded_input
Transcoder::Form.encode(form)
end
elsif (json = params.delete(:json))
# @type var body: _ToJson
Transcoder::JSON.encode(json)

View File

@ -48,11 +48,7 @@ module HTTPX
end
def encode(form)
if multipart?(form)
Multipart::Encoder.new(form)
else
Encoder.new(form)
end
Encoder.new(form)
end
def decode(response)
@ -67,14 +63,6 @@ module HTTPX
raise Error, "invalid form mime type (#{content_type})"
end
end
def multipart?(data)
data.any? do |_, v|
Multipart::MULTIPART_VALUE_COND.call(v) ||
(v.respond_to?(:to_ary) && v.to_ary.any?(&Multipart::MULTIPART_VALUE_COND)) ||
(v.respond_to?(:to_hash) && v.to_hash.any? { |_, e| Multipart::MULTIPART_VALUE_COND.call(e) })
end
end
end
end
end

View File

@ -13,5 +13,19 @@ module HTTPX::Transcoder
value.key?(:body) &&
(value.key?(:filename) || value.key?(:content_type)))
end
module_function
def multipart?(form_data)
form_data.any? do |_, v|
Multipart::MULTIPART_VALUE_COND.call(v) ||
(v.respond_to?(:to_ary) && v.to_ary.any?(&Multipart::MULTIPART_VALUE_COND)) ||
(v.respond_to?(:to_hash) && v.to_hash.any? { |_, e| Multipart::MULTIPART_VALUE_COND.call(e) })
end
end
def encode(form_data)
Encoder.new(form_data)
end
end
end

View File

@ -3,14 +3,14 @@ module HTTPX::Transcoder
type form_nested_value = form_value | _ToAry[form_value] | _ToHash[string, form_value]
type urlencoded_input = Enumerable[[_ToS, form_nested_value | Multipart::multipart_nested_value]]
type urlencoded_input = Enumerable[[_ToS, form_nested_value]]
module Form
PARAM_DEPTH_LIMIT: Integer
def self?.encode: (urlencoded_input form) -> (Encoder | Multipart::Encoder)
def self?.encode: (urlencoded_input form) -> Encoder
def self?.decode: (HTTPX::Response response) -> _Decoder
def self?.multipart?: (form_nested_value | Multipart::multipart_nested_value data) -> bool
class Encoder
extend Forwardable

View File

@ -16,6 +16,12 @@ module HTTPX
type multipart_nested_value = multipart_value | _ToAry[multipart_value] | _ToHash[string, multipart_value]
type multipart_input = Enumerable[[_ToS, Multipart::multipart_nested_value]]
def self?.encode: (multipart_input form_data) -> Multipart::Encoder
def self?.multipart?: (form_nested_value | multipart_nested_value form_data) -> bool
class Encoder
@boundary: String
@part_index: Integer
@ -36,9 +42,9 @@ module HTTPX
private
def to_parts: (Enumerable[[Symbol | string, multipart_nested_value]] multipart_data) -> Array[_Reader]
def to_parts: (multipart_input multipart_data) -> Array[_Reader]
def initialize: (Enumerable[[Symbol | string, multipart_nested_value]] multipart_data) -> untyped
def initialize: (multipart_input multipart_data) -> untyped
def header_part: (String key, String content_type, String? filename) -> StringIO