できません。エラー... メソッドをmonkey_patchingしPatron::Session.request
、処理の直前にリクエストを譲ることでできます。ただし、これは「libcurl」リクエストではないことに注意してください。これは C コードにのみ存在し、Patron::Request
インスタンスであるためです。
また、メソッド全体を書き直さなければならないため、モンキーパッチはいつでも壊れる可能性があることに注意してください!
yield
libcurl へのリクエストを処理する直前に を追加するので、ブロックで取得する機会があります。
req = nil
response = s.delete(uri) do |r|
req = r
end
# now req should be your request instance.
パッチのヒントは次のとおりです。
class Patron::Session
# You have to patch all the standard methods to accept a block
def get(url, headers = {}, &block)
request(:get, url, headers, &block)
end
# do the same for get_file, head, delete, put, put_file, post, post_file and post_multipart
def request(action, url, headers, options = {}, &block)
# If the Expect header isn't set uploads are really slow
headers['Expect'] ||= ''
req = Request.new
req.action = action
req.headers = self.headers.merge headers
req.timeout = options.fetch :timeout, self.timeout
req.connect_timeout = options.fetch :connect_timeout, self.connect_timeout
req.max_redirects = options.fetch :max_redirects, self.max_redirects
req.username = options.fetch :username, self.username
req.password = options.fetch :password, self.password
req.proxy = options.fetch :proxy, self.proxy
req.proxy_type = options.fetch :proxy_type, self.proxy_type
req.auth_type = options.fetch :auth_type, self.auth_type
req.insecure = options.fetch :insecure, self.insecure
req.ignore_content_length = options.fetch :ignore_content_length, self.ignore_content_length
req.buffer_size = options.fetch :buffer_size, self.buffer_size
req.multipart = options[:multipart]
req.upload_data = options[:data]
req.file_name = options[:file]
base_url = self.base_url.to_s
url = url.to_s
raise ArgumentError, "Empty URL" if base_url.empty? && url.empty?
uri = URI.join(base_url, url)
query = uri.query.to_s.split('&')
query += options[:query].is_a?(Hash) ? Util.build_query_pairs_from_hash(options[:query]) : options[:query].to_s.split('&')
uri.query = query.join('&')
uri.query = nil if uri.query.empty?
url = uri.to_s
req.url = url
yield req if block_given? # added line
handle_request(req)
end
end