mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
adding :block_response parameter
This commit is contained in:
parent
937340bf3d
commit
8a9c6d7383
5 changed files with 30 additions and 5 deletions
12
README.rdoc
12
README.rdoc
|
@ -12,7 +12,7 @@ of specifying actions: get, put, post, delete.
|
||||||
require 'rest_client'
|
require 'rest_client'
|
||||||
|
|
||||||
RestClient.get 'http://example.com/resource'
|
RestClient.get 'http://example.com/resource'
|
||||||
|
|
||||||
RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
|
RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
|
||||||
|
|
||||||
RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json}
|
RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json}
|
||||||
|
@ -137,7 +137,15 @@ If you want to use non-normalized URIs, you can normalize them with the addressa
|
||||||
|
|
||||||
== Lower-level access
|
== Lower-level access
|
||||||
|
|
||||||
For cases not covered by the general API, you can use the RestClient::Resource class which provide a lower-level API, see the class' rdoc for more information.
|
For cases not covered by the general API, you can use the RestClient::Request class which provide a lower-level API.
|
||||||
|
|
||||||
|
You can:
|
||||||
|
|
||||||
|
* specify ssl parameters
|
||||||
|
* override cookies
|
||||||
|
* manually handle the response (so you can operate on the response stream than reading it fully in memory)
|
||||||
|
|
||||||
|
see the class' rdoc for more information.
|
||||||
|
|
||||||
== Shell
|
== Shell
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
- fix shell for 1.9.2
|
- fix shell for 1.9.2
|
||||||
- workaround when some gem monkeypatch net/http (patch provided by Ian Warshak)
|
- workaround when some gem monkeypatch net/http (patch provided by Ian Warshak)
|
||||||
- DELETE requests should process parameters just like GET and HEAD
|
- DELETE requests should process parameters just like GET and HEAD
|
||||||
|
- adding :block_response parameter for manual processing
|
||||||
|
|
||||||
# 1.6.1
|
# 1.6.1
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ module RestClient
|
||||||
# * :headers a hash containing the request headers
|
# * :headers a hash containing the request headers
|
||||||
# * :cookies will replace possible cookies in the :headers
|
# * :cookies will replace possible cookies in the :headers
|
||||||
# * :user and :password for basic auth, will be replaced by a user/password available in the :url
|
# * :user and :password for basic auth, will be replaced by a user/password available in the :url
|
||||||
|
# * :block_response call the provided block with the HTTPResponse as parameter
|
||||||
# * :raw_response return a low-level RawResponse instead of a Response
|
# * :raw_response return a low-level RawResponse instead of a Response
|
||||||
# * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
|
# * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
|
||||||
# * :timeout and :open_timeout
|
# * :timeout and :open_timeout
|
||||||
|
@ -45,6 +46,7 @@ module RestClient
|
||||||
@password = args[:password]
|
@password = args[:password]
|
||||||
@timeout = args[:timeout]
|
@timeout = args[:timeout]
|
||||||
@open_timeout = args[:open_timeout]
|
@open_timeout = args[:open_timeout]
|
||||||
|
@block_response = args[:block_response]
|
||||||
@raw_response = args[:raw_response] || false
|
@raw_response = args[:raw_response] || false
|
||||||
@verify_ssl = args[:verify_ssl] || false
|
@verify_ssl = args[:verify_ssl] || false
|
||||||
@ssl_client_cert = args[:ssl_client_cert] || nil
|
@ssl_client_cert = args[:ssl_client_cert] || nil
|
||||||
|
@ -164,9 +166,13 @@ module RestClient
|
||||||
log_request
|
log_request
|
||||||
|
|
||||||
net.start do |http|
|
net.start do |http|
|
||||||
res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
|
if @block_response
|
||||||
log_response res
|
http.request(req, payload ? payload.to_s : nil, & @block_response)
|
||||||
process_result res, & block
|
else
|
||||||
|
res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
|
||||||
|
log_response res
|
||||||
|
process_result res, & block
|
||||||
|
end
|
||||||
end
|
end
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
raise RestClient::ServerBrokeConnection
|
raise RestClient::ServerBrokeConnection
|
||||||
|
|
|
@ -13,5 +13,15 @@ describe RestClient::Request do
|
||||||
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should == 'foo'
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should == 'foo'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can use a block to process response" do
|
||||||
|
response_value = nil
|
||||||
|
block = Proc.new do |http_response|
|
||||||
|
response_value = http_response.body
|
||||||
|
end
|
||||||
|
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
||||||
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
|
||||||
|
response_value.should == "foo"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
0
test.rb
0
test.rb
Loading…
Add table
Reference in a new issue