2014-06-26 17:33:32 -04:00
|
|
|
require 'net/http'
|
|
|
|
require 'openssl'
|
2014-06-26 17:34:39 -04:00
|
|
|
require 'stringio'
|
|
|
|
require 'uri'
|
|
|
|
require 'zlib'
|
2009-06-24 15:11:02 -04:00
|
|
|
|
2013-08-06 23:17:08 -04:00
|
|
|
require File.dirname(__FILE__) + '/restclient/version'
|
2014-04-03 15:49:07 -04:00
|
|
|
require File.dirname(__FILE__) + '/restclient/platform'
|
2010-01-18 16:15:15 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/exceptions'
|
2015-01-28 05:45:02 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/utils'
|
2009-01-24 17:49:01 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/request'
|
2010-01-25 16:04:59 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/abstract_response'
|
2009-01-24 17:49:01 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/response'
|
2009-03-16 18:28:20 -04:00
|
|
|
require File.dirname(__FILE__) + '/restclient/raw_response'
|
2009-01-24 17:49:01 -05:00
|
|
|
require File.dirname(__FILE__) + '/restclient/resource'
|
2009-08-12 11:00:44 -04:00
|
|
|
require File.dirname(__FILE__) + '/restclient/payload'
|
2014-03-30 20:41:51 -04:00
|
|
|
require File.dirname(__FILE__) + '/restclient/windows'
|
2009-01-24 17:49:01 -05:00
|
|
|
|
|
|
|
# This module's static methods are the entry point for using the REST client.
|
|
|
|
#
|
|
|
|
# # GET
|
|
|
|
# xml = RestClient.get 'http://example.com/resource'
|
|
|
|
# jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
|
|
|
|
#
|
|
|
|
# # authentication and SSL
|
|
|
|
# RestClient.get 'https://user:password@example.com/private/resource'
|
|
|
|
#
|
|
|
|
# # POST or PUT with a hash sends parameters as a urlencoded form body
|
|
|
|
# RestClient.post 'http://example.com/resource', :param1 => 'one'
|
|
|
|
#
|
|
|
|
# # nest hash parameters
|
|
|
|
# RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
|
|
|
|
#
|
|
|
|
# # POST and PUT with raw payloads
|
|
|
|
# RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
|
|
|
|
# RestClient.post 'http://example.com/resource.xml', xml_doc
|
|
|
|
# RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
|
|
|
|
#
|
|
|
|
# # DELETE
|
|
|
|
# RestClient.delete 'http://example.com/resource'
|
|
|
|
#
|
2009-02-10 16:55:54 -05:00
|
|
|
# # retreive the response http code and headers
|
2009-01-24 20:03:33 -05:00
|
|
|
# res = RestClient.get 'http://example.com/some.jpg'
|
2009-02-10 16:55:54 -05:00
|
|
|
# res.code # => 200
|
2009-01-24 20:03:33 -05:00
|
|
|
# res.headers[:content_type] # => 'image/jpg'
|
|
|
|
#
|
2009-02-10 16:55:54 -05:00
|
|
|
# # HEAD
|
|
|
|
# RestClient.head('http://example.com').headers
|
|
|
|
#
|
2009-01-24 17:49:01 -05:00
|
|
|
# To use with a proxy, just set RestClient.proxy to the proper http proxy:
|
|
|
|
#
|
|
|
|
# RestClient.proxy = "http://proxy.example.com/"
|
|
|
|
#
|
|
|
|
# Or inherit the proxy from the environment:
|
|
|
|
#
|
|
|
|
# RestClient.proxy = ENV['http_proxy']
|
|
|
|
#
|
|
|
|
# For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
|
|
|
|
#
|
|
|
|
# >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
|
|
|
|
# => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
|
|
|
|
#
|
|
|
|
module RestClient
|
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.get(url, headers={}, &block)
|
|
|
|
Request.execute(:method => :get, :url => url, :headers => headers, &block)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:49:01 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.post(url, payload, headers={}, &block)
|
|
|
|
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:49:01 -05:00
|
|
|
|
2011-04-29 18:17:47 -04:00
|
|
|
def self.patch(url, payload, headers={}, &block)
|
|
|
|
Request.execute(:method => :patch, :url => url, :payload => payload, :headers => headers, &block)
|
|
|
|
end
|
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.put(url, payload, headers={}, &block)
|
|
|
|
Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers, &block)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:49:01 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.delete(url, headers={}, &block)
|
|
|
|
Request.execute(:method => :delete, :url => url, :headers => headers, &block)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-02-10 16:55:54 -05:00
|
|
|
|
2010-01-22 15:04:49 -05:00
|
|
|
def self.head(url, headers={}, &block)
|
|
|
|
Request.execute(:method => :head, :url => url, :headers => headers, &block)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-01-24 17:49:01 -05:00
|
|
|
|
2010-07-02 17:21:32 -04:00
|
|
|
def self.options(url, headers={}, &block)
|
|
|
|
Request.execute(:method => :options, :url => url, :headers => headers, &block)
|
|
|
|
end
|
|
|
|
|
2015-04-15 21:17:34 -04:00
|
|
|
# A global proxy URL to use for all requests. This can be overridden on a
|
|
|
|
# per-request basis by passing `:proxy` to RestClient::Request.
|
|
|
|
def self.proxy
|
|
|
|
@proxy
|
|
|
|
end
|
|
|
|
def self.proxy=(value)
|
|
|
|
@proxy = value
|
|
|
|
@proxy_set = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return whether RestClient.proxy was set explicitly. We use this to
|
|
|
|
# differentiate between no value being set and a value explicitly set to nil.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
def self.proxy_set?
|
|
|
|
!!@proxy_set
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
# Setup the log for RestClient calls.
|
|
|
|
# Value should be a logger but can can be stdout, stderr, or a filename.
|
2009-12-29 12:27:39 -05:00
|
|
|
# You can also configure logging by the environment variable RESTCLIENT_LOG.
|
2010-01-05 15:03:27 -05:00
|
|
|
def self.log= log
|
|
|
|
@@log = create_log log
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2009-08-12 22:29:45 -04:00
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
# Create a log that respond to << like a logger
|
|
|
|
# param can be 'stdout', 'stderr', a string (then we will log to that file) or a logger (then we return it)
|
|
|
|
def self.create_log param
|
|
|
|
if param
|
|
|
|
if param.is_a? String
|
|
|
|
if param == 'stdout'
|
|
|
|
stdout_logger = Class.new do
|
|
|
|
def << obj
|
|
|
|
STDOUT.puts obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
stdout_logger.new
|
|
|
|
elsif param == 'stderr'
|
|
|
|
stderr_logger = Class.new do
|
|
|
|
def << obj
|
|
|
|
STDERR.puts obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
stderr_logger.new
|
|
|
|
else
|
|
|
|
file_logger = Class.new do
|
|
|
|
attr_writer :target_file
|
2010-01-31 07:31:30 -05:00
|
|
|
|
2010-01-05 15:03:27 -05:00
|
|
|
def << obj
|
|
|
|
File.open(@target_file, 'a') { |f| f.puts obj }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
logger = file_logger.new
|
|
|
|
logger.target_file = param
|
|
|
|
logger
|
|
|
|
end
|
|
|
|
else
|
|
|
|
param
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@@env_log = create_log ENV['RESTCLIENT_LOG']
|
|
|
|
|
|
|
|
@@log = nil
|
|
|
|
|
|
|
|
def self.log # :nodoc:
|
|
|
|
@@env_log || @@log
|
|
|
|
end
|
|
|
|
|
2010-01-31 07:31:30 -05:00
|
|
|
@@before_execution_procs = []
|
|
|
|
|
2010-02-16 13:03:44 -05:00
|
|
|
# Add a Proc to be called before each request in executed.
|
|
|
|
# The proc parameters will be the http request and the request params.
|
2010-01-31 07:31:30 -05:00
|
|
|
def self.add_before_execution_proc &proc
|
2015-06-10 02:53:09 -04:00
|
|
|
raise ArgumentError.new('block is required') unless proc
|
2010-01-31 07:31:30 -05:00
|
|
|
@@before_execution_procs << proc
|
|
|
|
end
|
2011-04-29 18:17:47 -04:00
|
|
|
|
2011-05-06 11:26:05 -04:00
|
|
|
# Reset the procs to be called before each request is executed.
|
|
|
|
def self.reset_before_execution_procs
|
|
|
|
@@before_execution_procs = []
|
|
|
|
end
|
2010-01-31 07:31:30 -05:00
|
|
|
|
|
|
|
def self.before_execution_procs # :nodoc:
|
|
|
|
@@before_execution_procs
|
|
|
|
end
|
|
|
|
|
2009-01-24 17:49:01 -05:00
|
|
|
end
|