mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Multipart with Streaming
This commit is contained in:
parent
75cb7f218f
commit
6815ae2496
9 changed files with 310 additions and 7 deletions
|
@ -13,8 +13,23 @@ of specifying actions: get, put, post, delete.
|
|||
RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }
|
||||
|
||||
RestClient.delete 'http://example.com/resource'
|
||||
|
||||
== Multipart
|
||||
|
||||
See RestClient module docs for details.
|
||||
Yeah, that's right! This does multipart sends for you!
|
||||
|
||||
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg")
|
||||
|
||||
This does two things for you:
|
||||
|
||||
* Auto-detects that you have a File value sends it as multipart
|
||||
* Auto-detects the mime of the file and sets it in the HEAD of the payload for each entry
|
||||
|
||||
If you are sending params that do not contain a File object but the payload needs to be multipart then:
|
||||
|
||||
RestClient.post '/data', :foo => 'bar', :multipart => true
|
||||
|
||||
See RestClient module docs for more details.
|
||||
|
||||
== Usage: ActiveResource-Style
|
||||
|
2
Rakefile
2
Rakefile
|
@ -76,7 +76,7 @@ Rake::RDocTask.new do |t|
|
|||
t.title = "rest-client, fetch RESTful resources effortlessly"
|
||||
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
||||
t.options << '--charset' << 'utf-8'
|
||||
t.rdoc_files.include('README')
|
||||
t.rdoc_files.include('README.rdoc')
|
||||
t.rdoc_files.include('lib/*.rb')
|
||||
end
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ require 'net/https'
|
|||
|
||||
require File.dirname(__FILE__) + '/rest_client/resource'
|
||||
require File.dirname(__FILE__) + '/rest_client/request_errors'
|
||||
require File.dirname(__FILE__) + '/rest_client/payload'
|
||||
require File.dirname(__FILE__) + '/rest_client/net_http_ext'
|
||||
|
||||
# This module's static methods are the entry point for using the REST client.
|
||||
#
|
||||
|
@ -61,7 +63,7 @@ module RestClient
|
|||
|
||||
# Internal class used to build and execute the request.
|
||||
class Request
|
||||
attr_reader :method, :url, :payload, :headers, :user, :password
|
||||
attr_reader :method, :url, :headers, :user, :password
|
||||
|
||||
def self.execute(args)
|
||||
new(args).execute
|
||||
|
@ -70,8 +72,8 @@ module RestClient
|
|||
def initialize(args)
|
||||
@method = args[:method] or raise ArgumentError, "must pass :method"
|
||||
@url = args[:url] or raise ArgumentError, "must pass :url"
|
||||
@payload = Payload.generate(args[:payload] || '')
|
||||
@headers = args[:headers] || {}
|
||||
@payload = process_payload(args[:payload])
|
||||
@user = args[:user]
|
||||
@password = args[:password]
|
||||
end
|
||||
|
@ -170,9 +172,13 @@ module RestClient
|
|||
raise RequestFailed, res
|
||||
end
|
||||
end
|
||||
|
||||
def payload
|
||||
@payload
|
||||
end
|
||||
|
||||
def default_headers
|
||||
{ :accept => 'application/xml' }
|
||||
@payload.headers.merge({ :accept => 'application/xml' })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
21
lib/rest_client/net_http_ext.rb
Normal file
21
lib/rest_client/net_http_ext.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
#
|
||||
# Replace the request method in Net::HTTP to sniff the body type
|
||||
# and set the stream if appropriate
|
||||
#
|
||||
# Taken from:
|
||||
# http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/
|
||||
|
||||
module Net
|
||||
class HTTP
|
||||
alias __request__ request
|
||||
|
||||
def request(req, body = nil, &block)
|
||||
if body != nil && body.respond_to?(:read)
|
||||
req.body_stream = body
|
||||
return __request__(req, nil, &block)
|
||||
else
|
||||
return __request__(req, body, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
183
lib/rest_client/payload.rb
Normal file
183
lib/rest_client/payload.rb
Normal file
|
@ -0,0 +1,183 @@
|
|||
require "tempfile"
|
||||
|
||||
module RestClient
|
||||
|
||||
module Payload
|
||||
extend self
|
||||
|
||||
class NotImplemented < RuntimeError; end
|
||||
|
||||
def generate(params)
|
||||
if params.is_a?(String)
|
||||
Base.new(params)
|
||||
elsif params.delete(:multipart) == true ||
|
||||
params.any? { |_,v| v.respond_to?(:path) && v.respond_to?(:read) }
|
||||
Multipart.new(params)
|
||||
else
|
||||
UrlEncoded.new(params)
|
||||
end
|
||||
end
|
||||
|
||||
class Base
|
||||
|
||||
def initialize(params)
|
||||
build_stream(params)
|
||||
end
|
||||
|
||||
def build_stream(params)
|
||||
@stream = StringIO.new(params)
|
||||
@stream.seek(0)
|
||||
end
|
||||
|
||||
def read(bytes=nil)
|
||||
@stream.read(bytes)
|
||||
end
|
||||
alias :to_s :read
|
||||
|
||||
def escape(v)
|
||||
URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
||||
end
|
||||
|
||||
def headers
|
||||
{'Content-Length' => size.to_s}
|
||||
end
|
||||
|
||||
def size
|
||||
@stream.size
|
||||
end
|
||||
alias :length :size
|
||||
|
||||
|
||||
end
|
||||
|
||||
class UrlEncoded < Base
|
||||
|
||||
def build_stream(params)
|
||||
@stream = StringIO.new(params.map do |k,v|
|
||||
"#{escape(k)}=#{escape(v)}"
|
||||
end.join("&"))
|
||||
@stream.seek(0)
|
||||
end
|
||||
|
||||
def headers
|
||||
super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Multipart < Base
|
||||
|
||||
EOL = "\r\n"
|
||||
|
||||
def build_stream(params)
|
||||
b = "--#{boundary}"
|
||||
|
||||
@stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
|
||||
@stream.write(b + EOL)
|
||||
params.each do |k,v|
|
||||
if v.respond_to?(:read) && v.respond_to?(:path)
|
||||
create_file_field(@stream, k,v)
|
||||
else
|
||||
create_regular_field(@stream, k,v)
|
||||
end
|
||||
@stream.write(b + EOL)
|
||||
end
|
||||
@stream.write('--')
|
||||
@stream.seek(0)
|
||||
end
|
||||
|
||||
def create_regular_field(s, k, v)
|
||||
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"")
|
||||
s.write(EOL)
|
||||
s.write(EOL)
|
||||
s.write(v)
|
||||
end
|
||||
|
||||
def create_file_field(s, k, v)
|
||||
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}")
|
||||
s.write("Content-Type: #{mime_for(v.path)}#{EOL}")
|
||||
s.write(EOL)
|
||||
while data = v.read(8124)
|
||||
s.write(data)
|
||||
end
|
||||
end
|
||||
|
||||
def mime_for(path)
|
||||
ext = File.extname(path)[1..-1]
|
||||
MIME_TYPES[ext] || 'text/plain'
|
||||
end
|
||||
|
||||
def boundary
|
||||
@boundary ||= rand(1_000_000).to_s
|
||||
end
|
||||
|
||||
def headers
|
||||
super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# :stopdoc:
|
||||
# From WEBrick.
|
||||
MIME_TYPES = {
|
||||
"ai" => "application/postscript",
|
||||
"asc" => "text/plain",
|
||||
"avi" => "video/x-msvideo",
|
||||
"bin" => "application/octet-stream",
|
||||
"bmp" => "image/bmp",
|
||||
"class" => "application/octet-stream",
|
||||
"cer" => "application/pkix-cert",
|
||||
"crl" => "application/pkix-crl",
|
||||
"crt" => "application/x-x509-ca-cert",
|
||||
#"crl" => "application/x-pkcs7-crl",
|
||||
"css" => "text/css",
|
||||
"dms" => "application/octet-stream",
|
||||
"doc" => "application/msword",
|
||||
"dvi" => "application/x-dvi",
|
||||
"eps" => "application/postscript",
|
||||
"etx" => "text/x-setext",
|
||||
"exe" => "application/octet-stream",
|
||||
"gif" => "image/gif",
|
||||
"htm" => "text/html",
|
||||
"html" => "text/html",
|
||||
"jpe" => "image/jpeg",
|
||||
"jpeg" => "image/jpeg",
|
||||
"jpg" => "image/jpeg",
|
||||
"js" => "text/javascript",
|
||||
"lha" => "application/octet-stream",
|
||||
"lzh" => "application/octet-stream",
|
||||
"mov" => "video/quicktime",
|
||||
"mpe" => "video/mpeg",
|
||||
"mpeg" => "video/mpeg",
|
||||
"mpg" => "video/mpeg",
|
||||
"pbm" => "image/x-portable-bitmap",
|
||||
"pdf" => "application/pdf",
|
||||
"pgm" => "image/x-portable-graymap",
|
||||
"png" => "image/png",
|
||||
"pnm" => "image/x-portable-anymap",
|
||||
"ppm" => "image/x-portable-pixmap",
|
||||
"ppt" => "application/vnd.ms-powerpoint",
|
||||
"ps" => "application/postscript",
|
||||
"qt" => "video/quicktime",
|
||||
"ras" => "image/x-cmu-raster",
|
||||
"rb" => "text/plain",
|
||||
"rd" => "text/plain",
|
||||
"rtf" => "application/rtf",
|
||||
"sgm" => "text/sgml",
|
||||
"sgml" => "text/sgml",
|
||||
"tif" => "image/tiff",
|
||||
"tiff" => "image/tiff",
|
||||
"txt" => "text/plain",
|
||||
"xbm" => "image/x-xbitmap",
|
||||
"xls" => "application/vnd.ms-excel",
|
||||
"xml" => "text/xml",
|
||||
"xpm" => "image/x-xpixmap",
|
||||
"xwd" => "image/x-xwindowdump",
|
||||
"zip" => "application/zip",
|
||||
}
|
||||
# :startdoc:
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|||
s.homepage = "http://rest-client.heroku.com/"
|
||||
s.has_rdoc = true
|
||||
s.platform = Gem::Platform::RUBY
|
||||
s.files = %w(Rakefile README rest-client.gemspec
|
||||
s.files = %w(Rakefile README.rdoc rest-client.gemspec
|
||||
lib/request_errors.rb lib/resource.rb lib/rest_client.rb
|
||||
spec/base.rb spec/request_errors_spec.rb spec/resource_spec.rb spec/rest_client_spec.rb
|
||||
bin/restclient)
|
||||
|
|
BIN
spec/master_shake.jpg
Normal file
BIN
spec/master_shake.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
78
spec/payload_spec.rb
Normal file
78
spec/payload_spec.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
require File.dirname(__FILE__) + "/base"
|
||||
|
||||
context "A regular Payload" do
|
||||
|
||||
specify "should should default content-type to standard enctype" do
|
||||
RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
|
||||
should == 'application/x-www-form-urlencoded'
|
||||
end
|
||||
|
||||
specify "should form properly encoded params" do
|
||||
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
|
||||
should == "foo=bar"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "A multipart Payload" do
|
||||
specify "should should default content-type to standard enctype" do
|
||||
m = RestClient::Payload::Multipart.new({})
|
||||
m.stub!(:boundary).and_return(123)
|
||||
m.headers['Content-Type'].should == 'multipart/form-data; boundary="123"'
|
||||
end
|
||||
|
||||
xspecify "should form properly seperated multipart data" do
|
||||
m = RestClient::Payload::Multipart.new({:foo => "bar"})
|
||||
m.stub!(:boundary).and_return("123")
|
||||
m.to_s.should == <<-EOS
|
||||
--123\r
|
||||
Content-Disposition: multipart/form-data; name="foo"\r
|
||||
\r
|
||||
bar\r
|
||||
--123--\r
|
||||
EOS
|
||||
end
|
||||
|
||||
xspecify "should form properly seperated multipart data" do
|
||||
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
||||
# f = mock("master_shake.jpg")
|
||||
# f.stub!(:path).and_return("master_shake.jpg")
|
||||
# f.stub!(:read).and_return("datadatadata")
|
||||
m = RestClient::Payload::Multipart.new({:foo => f})
|
||||
m.stub!(:boundary).and_return("123")
|
||||
m.to_s.should == <<-EOS
|
||||
--123\r
|
||||
Content-Disposition: multipart/form-data; name="foo"; filename="master_shake.jpg"\r
|
||||
Content-Type: image/jpeg\r
|
||||
\r
|
||||
datadatadata\r
|
||||
--123--\r
|
||||
EOS
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "Payload generation" do
|
||||
|
||||
specify "should recognize standard urlencoded params" do
|
||||
RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
|
||||
end
|
||||
|
||||
specify "should recognize multipart params" do
|
||||
# f = mock("master_shake.jpg")
|
||||
# f.stub!(:path)
|
||||
# f.stub!(:read)
|
||||
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
||||
|
||||
RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart)
|
||||
end
|
||||
|
||||
specify "should be multipart if forced" do
|
||||
RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart)
|
||||
end
|
||||
|
||||
specify "should return data if no of the above" do
|
||||
RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base)
|
||||
end
|
||||
|
||||
end
|
|
@ -99,7 +99,7 @@ describe RestClient do
|
|||
klass = mock("net:http class")
|
||||
@request.should_receive(:net_http_class).with(:put).and_return(klass)
|
||||
klass.should_receive(:new).and_return('result')
|
||||
@request.should_receive(:transmit).with(@uri, 'result', 'payload')
|
||||
@request.should_receive(:transmit).with(@uri, 'result', be_kind_of(RestClient::Payload::Base))
|
||||
@request.execute_inner
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue