@@ -348,27 +348,6 @@ def normalized_headers(*headers)
348348 end
349349 end
350350
351- # @api private
352- class SerializationAdapter
353- # @return [Pathname, IO]
354- attr_reader :inner
355-
356- # @param a [Object]
357- #
358- # @return [String]
359- def to_json ( *a ) = ( inner . is_a? ( IO ) ? inner . read : inner . read ( binmode : true ) ) . to_json ( *a )
360-
361- # @param a [Object]
362- #
363- # @return [String]
364- def to_yaml ( *a ) = ( inner . is_a? ( IO ) ? inner . read : inner . read ( binmode : true ) ) . to_yaml ( *a )
365-
366- # @api private
367- #
368- # @param inner [Pathname, IO]
369- def initialize ( inner ) = @inner = inner
370- end
371-
372351 # @api private
373352 #
374353 # An adapter that satisfies the IO interface required by `::IO.copy_stream`
@@ -480,42 +459,35 @@ class << self
480459 # @api private
481460 #
482461 # @param y [Enumerator::Yielder]
483- # @param boundary [String]
484- # @param key [Symbol, String]
485462 # @param val [Object]
486463 # @param closing [Array<Proc>]
487- private def write_multipart_chunk ( y , boundary :, key :, val :, closing :)
488- val = val . inner if val . is_a? ( Orb ::Internal ::Util ::SerializationAdapter )
464+ # @param content_type [String, nil]
465+ private def write_multipart_content ( y , val :, closing :, content_type : nil )
466+ content_type ||= "application/octet-stream"
489467
490- y << "--#{ boundary } \r \n "
491- y << "Content-Disposition: form-data"
492- unless key . nil?
493- name = ERB ::Util . url_encode ( key . to_s )
494- y << "; name=\" #{ name } \" "
495- end
496- case val
497- in Pathname | IO
498- filename = ERB ::Util . url_encode ( File . basename ( val . to_path ) )
499- y << "; filename=\" #{ filename } \" "
500- else
501- end
502- y << "\r \n "
503468 case val
469+ in Orb ::FilePart
470+ return write_multipart_content (
471+ y ,
472+ val : val . content ,
473+ closing : closing ,
474+ content_type : val . content_type
475+ )
504476 in Pathname
505- y << "Content-Type: application/octet-stream \r \n \r \n "
477+ y << "Content-Type: #{ content_type } \r \n \r \n "
506478 io = val . open ( binmode : true )
507479 closing << io . method ( :close )
508480 IO . copy_stream ( io , y )
509481 in IO
510- y << "Content-Type: application/octet-stream \r \n \r \n "
482+ y << "Content-Type: #{ content_type } \r \n \r \n "
511483 IO . copy_stream ( val , y )
512484 in StringIO
513- y << "Content-Type: application/octet-stream \r \n \r \n "
485+ y << "Content-Type: #{ content_type } \r \n \r \n "
514486 y << val . string
515487 in String
516- y << "Content-Type: application/octet-stream \r \n \r \n "
488+ y << "Content-Type: #{ content_type } \r \n \r \n "
517489 y << val . to_s
518- in _ if primitive? ( val )
490+ in -> { primitive? ( _1 ) }
519491 y << "Content-Type: text/plain\r \n \r \n "
520492 y << val . to_s
521493 else
@@ -525,6 +497,36 @@ class << self
525497 y << "\r \n "
526498 end
527499
500+ # @api private
501+ #
502+ # @param y [Enumerator::Yielder]
503+ # @param boundary [String]
504+ # @param key [Symbol, String]
505+ # @param val [Object]
506+ # @param closing [Array<Proc>]
507+ private def write_multipart_chunk ( y , boundary :, key :, val :, closing :)
508+ y << "--#{ boundary } \r \n "
509+ y << "Content-Disposition: form-data"
510+
511+ unless key . nil?
512+ name = ERB ::Util . url_encode ( key . to_s )
513+ y << "; name=\" #{ name } \" "
514+ end
515+
516+ case val
517+ in Orb ::FilePart unless val . filename . nil?
518+ filename = ERB ::Util . url_encode ( val . filename )
519+ y << "; filename=\" #{ filename } \" "
520+ in Pathname | IO
521+ filename = ERB ::Util . url_encode ( File . basename ( val . to_path ) )
522+ y << "; filename=\" #{ filename } \" "
523+ else
524+ end
525+ y << "\r \n "
526+
527+ write_multipart_content ( y , val : val , closing : closing )
528+ end
529+
528530 # @api private
529531 #
530532 # @param body [Object]
@@ -565,21 +567,21 @@ class << self
565567 # @return [Object]
566568 def encode_content ( headers , body )
567569 content_type = headers [ "content-type" ]
568- body = body . inner if body . is_a? ( Orb ::Internal ::Util ::SerializationAdapter )
569-
570570 case [ content_type , body ]
571571 in [ Orb ::Internal ::Util ::JSON_CONTENT , Hash | Array | -> { primitive? ( _1 ) } ]
572572 [ headers , JSON . fast_generate ( body ) ]
573- in [ Orb ::Internal ::Util ::JSONL_CONTENT , Enumerable ] unless body . is_a? ( StringIO ) || body . is_a? ( IO )
573+ in [ Orb ::Internal ::Util ::JSONL_CONTENT , Enumerable ] unless body . is_a? ( Orb :: Internal :: Type :: FileInput )
574574 [ headers , body . lazy . map { JSON . fast_generate ( _1 ) } ]
575- in [ %r{^multipart/form-data} , Hash | Pathname | StringIO | IO ]
575+ in [ %r{^multipart/form-data} , Hash | Orb :: Internal :: Type :: FileInput ]
576576 boundary , strio = encode_multipart_streaming ( body )
577577 headers = { **headers , "content-type" => "#{ content_type } ; boundary=#{ boundary } " }
578578 [ headers , strio ]
579579 in [ _ , Symbol | Numeric ]
580580 [ headers , body . to_s ]
581581 in [ _ , StringIO ]
582582 [ headers , body . string ]
583+ in [ _ , Orb ::FilePart ]
584+ [ headers , body . content ]
583585 else
584586 [ headers , body ]
585587 end
0 commit comments