Class: Seahorse::Client::Request
- Inherits:
-
Object
- Object
- Seahorse::Client::Request
- Includes:
- HandlerBuilder
- Defined in:
- lib/seahorse/client/request.rb
Instance Attribute Summary collapse
- #context ⇒ RequestContext readonly
- #handlers ⇒ HandlerList readonly
Instance Method Summary collapse
-
#initialize(handlers, context) ⇒ Request
constructor
A new instance of Request.
-
#send_request(options = {}, &block) ⇒ Response
Sends the request, returning a Response object.
Methods included from HandlerBuilder
#handle, #handle_request, #handle_response, #handler_for, #new_handler
Constructor Details
#initialize(handlers, context) ⇒ Request
Returns a new instance of Request.
11 12 13 14 |
# File 'lib/seahorse/client/request.rb', line 11 def initialize(handlers, context) @handlers = handlers @context = context end |
Instance Attribute Details
#context ⇒ RequestContext (readonly)
20 21 22 |
# File 'lib/seahorse/client/request.rb', line 20 def context @context end |
#handlers ⇒ HandlerList (readonly)
17 18 19 |
# File 'lib/seahorse/client/request.rb', line 17 def handlers @handlers end |
Instance Method Details
#send_request(options = {}, &block) ⇒ Response
Sends the request, returning a Seahorse::Client::Response object.
response = request.send_request
# Streaming Responses
By default, HTTP responses are buffered into memory. This can be bad if you are downloading large responses, e.g. large files. You can avoid this by streaming the response to a block or some other target.
## Streaming to a File
You can stream the raw HTTP response body to a File, or any IO-like object, by passing the ‘:target` option.
# create a new file at the given path
request.send_request(target: '/path/to/target/file')
# or provide an IO object to write to
File.open('photo.jpg', 'wb') do |file|
request.send_request(target: file)
end
**Please Note**: The target IO object may receive ‘#truncate(0)` if the request generates a networking error and bytes have already been written to the target.
## Block Streaming
Pass a block to ‘#send_request` and the response will be yielded in chunks to the given block.
# stream the response data
request.send_request do |chunk|
file.write(chunk)
end
**Please Note**: When streaming to a block, it is not possible to retry failed requests.
70 71 72 73 |
# File 'lib/seahorse/client/request.rb', line 70 def send_request( = {}, &block) @context[:response_target] = [:target] || block @handlers.to_stack.call(@context) end |