PageRenderTime 654ms CodeModel.GetById 171ms app.highlight 240ms RepoModel.GetById 142ms app.codeStats 1ms

/lib/httparty.rb

http://github.com/jnunemaker/httparty
Ruby | 668 lines | 267 code | 69 blank | 332 comment | 13 complexity | 8b4c0910e6d86dfaba6c5cb367420be2 MD5 | raw file
  1require 'pathname'
  2require 'net/http'
  3require 'net/https'
  4require 'uri'
  5require 'zlib'
  6require 'multi_xml'
  7require 'mime/types'
  8require 'json'
  9require 'csv'
 10
 11require 'httparty/module_inheritable_attributes'
 12require 'httparty/cookie_hash'
 13require 'httparty/net_digest_auth'
 14require 'httparty/version'
 15require 'httparty/connection_adapter'
 16require 'httparty/logger/logger'
 17require 'httparty/request/body'
 18require 'httparty/response_fragment'
 19require 'httparty/text_encoder'
 20require 'httparty/headers_processor'
 21
 22# @see HTTParty::ClassMethods
 23module HTTParty
 24  def self.included(base)
 25    base.extend ClassMethods
 26    base.send :include, ModuleInheritableAttributes
 27    base.send(:mattr_inheritable, :default_options)
 28    base.send(:mattr_inheritable, :default_cookies)
 29    base.instance_variable_set("@default_options", {})
 30    base.instance_variable_set("@default_cookies", CookieHash.new)
 31  end
 32
 33  # == Common Request Options
 34  # Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
 35  #
 36  # [:+body+:] Body of the request. If passed an object that responds to #to_hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
 37  # [:+http_proxyaddr+:] Address of proxy server to use.
 38  # [:+http_proxyport+:]  Port of proxy server to use.
 39  # [:+http_proxyuser+:] User for proxy server authentication.
 40  # [:+http_proxypass+:] Password for proxy server authentication.
 41  # [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
 42  # [:+query+:] Query string, or an object that responds to #to_hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use an object which responds to #to_hash. See also HTTParty::ClassMethods.default_params.
 43  # [:+timeout+:] Timeout for opening connection and reading data.
 44  # [:+local_host+:] Local address to bind to before connecting.
 45  # [:+local_port+:] Local port to bind to before connecting.
 46  # [:+body_stream+:] Allow streaming to a REST server to specify a body_stream.
 47  # [:+stream_body+:] Allow for streaming large files without loading them into memory.
 48  # [:+multipart+:] Force content-type to be multipart
 49  #
 50  # There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are:
 51  # * :+base_uri+: see HTTParty::ClassMethods.base_uri.
 52  # * :+basic_auth+: see HTTParty::ClassMethods.basic_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
 53  # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
 54  # * :+digest_auth+: see HTTParty::ClassMethods.digest_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
 55  # * :+format+: see HTTParty::ClassMethods.format.
 56  # * :+headers+: see HTTParty::ClassMethods.headers. Must be a an object which responds to #to_hash.
 57  # * :+maintain_method_across_redirects+: see HTTParty::ClassMethods.maintain_method_across_redirects.
 58  # * :+no_follow+: see HTTParty::ClassMethods.no_follow.
 59  # * :+parser+: see HTTParty::ClassMethods.parser.
 60  # * :+uri_adapter+: see HTTParty::ClassMethods.uri_adapter
 61  # * :+connection_adapter+: see HTTParty::ClassMethods.connection_adapter.
 62  # * :+pem+: see HTTParty::ClassMethods.pem.
 63  # * :+query_string_normalizer+: see HTTParty::ClassMethods.query_string_normalizer
 64  # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
 65  # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
 66
 67  module ClassMethods
 68    # Turns on logging
 69    #
 70    #   class Foo
 71    #     include HTTParty
 72    #     logger Logger.new('http_logger'), :info, :apache
 73    #   end
 74    def logger(logger, level = :info, format = :apache)
 75      default_options[:logger]     = logger
 76      default_options[:log_level]  = level
 77      default_options[:log_format] = format
 78    end
 79
 80    # Raises HTTParty::ResponseError if response's code matches this statuses
 81    #
 82    #   class Foo
 83    #     include HTTParty
 84    #     raise_on [404, 500]
 85    #   end
 86    def raise_on(codes = [])
 87      default_options[:raise_on] = *codes
 88    end
 89
 90    # Allows setting http proxy information to be used
 91    #
 92    #   class Foo
 93    #     include HTTParty
 94    #     http_proxy 'http://foo.com', 80, 'user', 'pass'
 95    #   end
 96    def http_proxy(addr = nil, port = nil, user = nil, pass = nil)
 97      default_options[:http_proxyaddr] = addr
 98      default_options[:http_proxyport] = port
 99      default_options[:http_proxyuser] = user
100      default_options[:http_proxypass] = pass
101    end
102
103    # Allows setting a base uri to be used for each request.
104    # Will normalize uri to include http, etc.
105    #
106    #   class Foo
107    #     include HTTParty
108    #     base_uri 'twitter.com'
109    #   end
110    def base_uri(uri = nil)
111      return default_options[:base_uri] unless uri
112      default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
113    end
114
115    # Allows setting basic authentication username and password.
116    #
117    #   class Foo
118    #     include HTTParty
119    #     basic_auth 'username', 'password'
120    #   end
121    def basic_auth(u, p)
122      default_options[:basic_auth] = {username: u, password: p}
123    end
124
125    # Allows setting digest authentication username and password.
126    #
127    #   class Foo
128    #     include HTTParty
129    #     digest_auth 'username', 'password'
130    #   end
131    def digest_auth(u, p)
132      default_options[:digest_auth] = {username: u, password: p}
133    end
134
135    # Do not send rails style query strings.
136    # Specifically, don't use bracket notation when sending an array
137    #
138    # For a query:
139    #   get '/', query: {selected_ids: [1,2,3]}
140    #
141    # The default query string looks like this:
142    #   /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
143    #
144    # Call `disable_rails_query_string_format` to transform the query string
145    # into:
146    #   /?selected_ids=1&selected_ids=2&selected_ids=3
147    #
148    # @example
149    #   class Foo
150    #     include HTTParty
151    #     disable_rails_query_string_format
152    #   end
153    def disable_rails_query_string_format
154      query_string_normalizer Request::NON_RAILS_QUERY_STRING_NORMALIZER
155    end
156
157    # Allows setting default parameters to be appended to each request.
158    # Great for api keys and such.
159    #
160    #   class Foo
161    #     include HTTParty
162    #     default_params api_key: 'secret', another: 'foo'
163    #   end
164    def default_params(h = {})
165      raise ArgumentError, 'Default params must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
166      default_options[:default_params] ||= {}
167      default_options[:default_params].merge!(h)
168    end
169
170    # Allows setting a default timeout for all HTTP calls
171    # Timeout is specified in seconds.
172    #
173    #   class Foo
174    #     include HTTParty
175    #     default_timeout 10
176    #   end
177    def default_timeout(value)
178      validate_timeout_argument(__method__, value)
179      default_options[:timeout] = value
180    end
181
182    # Allows setting a default open_timeout for all HTTP calls in seconds
183    #
184    #   class Foo
185    #     include HTTParty
186    #     open_timeout 10
187    #   end
188    def open_timeout(value)
189      validate_timeout_argument(__method__, value)
190      default_options[:open_timeout] = value
191    end
192
193    # Allows setting a default read_timeout for all HTTP calls in seconds
194    #
195    #   class Foo
196    #     include HTTParty
197    #     read_timeout 10
198    #   end
199    def read_timeout(value)
200      validate_timeout_argument(__method__, value)
201      default_options[:read_timeout] = value
202    end
203
204    # Allows setting a default write_timeout for all HTTP calls in seconds
205    # Supported by Ruby > 2.6.0
206    #
207    #   class Foo
208    #     include HTTParty
209    #     write_timeout 10
210    #   end
211    def write_timeout(value)
212      validate_timeout_argument(__method__, value)
213      default_options[:write_timeout] = value
214    end
215
216
217    # Set an output stream for debugging, defaults to $stderr.
218    # The output stream is passed on to Net::HTTP#set_debug_output.
219    #
220    #   class Foo
221    #     include HTTParty
222    #     debug_output $stderr
223    #   end
224    def debug_output(stream = $stderr)
225      default_options[:debug_output] = stream
226    end
227
228    # Allows setting HTTP headers to be used for each request.
229    #
230    #   class Foo
231    #     include HTTParty
232    #     headers 'Accept' => 'text/html'
233    #   end
234    def headers(h = nil)
235      if h
236        raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
237        default_options[:headers] ||= {}
238        default_options[:headers].merge!(h.to_hash)
239      else
240        default_options[:headers] || {}
241      end
242    end
243
244    def cookies(h = {})
245      raise ArgumentError, 'Cookies must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
246      default_cookies.add_cookies(h)
247    end
248
249    # Proceed to the location header when an HTTP response dictates a redirect.
250    # Redirects are always followed by default.
251    #
252    # @example
253    #   class Foo
254    #     include HTTParty
255    #     base_uri 'http://google.com'
256    #     follow_redirects true
257    #   end
258    def follow_redirects(value = true)
259      default_options[:follow_redirects] = value
260    end
261
262    # Allows setting the format with which to parse.
263    # Must be one of the allowed formats ie: json, xml
264    #
265    #   class Foo
266    #     include HTTParty
267    #     format :json
268    #   end
269    def format(f = nil)
270      if f.nil?
271        default_options[:format]
272      else
273        parser(Parser) if parser.nil?
274        default_options[:format] = f
275        validate_format
276      end
277    end
278
279    # Declare whether or not to follow redirects.  When true, an
280    # {HTTParty::RedirectionTooDeep} error will raise upon encountering a
281    # redirect. You can then gain access to the response object via
282    # HTTParty::RedirectionTooDeep#response.
283    #
284    # @see HTTParty::ResponseError#response
285    #
286    # @example
287    #   class Foo
288    #     include HTTParty
289    #     base_uri 'http://google.com'
290    #     no_follow true
291    #   end
292    #
293    #   begin
294    #     Foo.get('/')
295    #   rescue HTTParty::RedirectionTooDeep => e
296    #     puts e.response.body
297    #   end
298    def no_follow(value = false)
299      default_options[:no_follow] = value
300    end
301
302    # Declare that you wish to maintain the chosen HTTP method across redirects.
303    # The default behavior is to follow redirects via the GET method, except
304    # if you are making a HEAD request, in which case the default is to
305    # follow all redirects with HEAD requests.
306    # If you wish to maintain the original method, you can set this option to true.
307    #
308    # @example
309    #   class Foo
310    #     include HTTParty
311    #     base_uri 'http://google.com'
312    #     maintain_method_across_redirects true
313    #   end
314
315    def maintain_method_across_redirects(value = true)
316      default_options[:maintain_method_across_redirects] = value
317    end
318
319    # Declare that you wish to resend the full HTTP request across redirects,
320    # even on redirects that should logically become GET requests.
321    # A 303 redirect in HTTP signifies that the redirected url should normally
322    # retrieved using a GET request, for instance, it is the output of a previous
323    # POST. maintain_method_across_redirects respects this behavior, but you
324    # can force HTTParty to resend_on_redirect even on 303 responses.
325    #
326    # @example
327    #   class Foo
328    #     include HTTParty
329    #     base_uri 'http://google.com'
330    #     resend_on_redirect
331    #   end
332
333    def resend_on_redirect(value = true)
334      default_options[:resend_on_redirect] = value
335    end
336
337    # Allows setting a PEM file to be used
338    #
339    #   class Foo
340    #     include HTTParty
341    #     pem File.read('/home/user/my.pem'), "optional password"
342    #   end
343    def pem(pem_contents, password = nil)
344      default_options[:pem] = pem_contents
345      default_options[:pem_password] = password
346    end
347
348    # Allows setting a PKCS12 file to be used
349    #
350    #   class Foo
351    #     include HTTParty
352    #     pkcs12 File.read('/home/user/my.p12'), "password"
353    #   end
354    def pkcs12(p12_contents, password)
355      default_options[:p12] = p12_contents
356      default_options[:p12_password] = password
357    end
358
359    # Override the way query strings are normalized.
360    # Helpful for overriding the default rails normalization of Array queries.
361    #
362    # For a query:
363    #   get '/', query: {selected_ids: [1,2,3]}
364    #
365    # The default query string normalizer returns:
366    #   /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
367    #
368    # Let's change it to this:
369    #  /?selected_ids=1&selected_ids=2&selected_ids=3
370    #
371    # Pass a Proc to the query normalizer which accepts the yielded query.
372    #
373    # @example Modifying Array query strings
374    #   class ServiceWrapper
375    #     include HTTParty
376    #
377    #     query_string_normalizer proc { |query|
378    #       query.map do |key, value|
379    #         value.map {|v| "#{key}=#{v}"}
380    #       end.join('&')
381    #     }
382    #   end
383    #
384    # @param [Proc] normalizer custom query string normalizer.
385    # @yield [Hash, String] query string
386    # @yieldreturn [Array] an array that will later be joined with '&'
387    def query_string_normalizer(normalizer)
388      default_options[:query_string_normalizer] = normalizer
389    end
390
391    # Allows setting of SSL version to use. This only works in Ruby 1.9+.
392    # You can get a list of valid versions from OpenSSL::SSL::SSLContext::METHODS.
393    #
394    #   class Foo
395    #     include HTTParty
396    #     ssl_version :SSLv3
397    #   end
398    def ssl_version(version)
399      default_options[:ssl_version] = version
400    end
401
402    # Allows setting of SSL ciphers to use.  This only works in Ruby 1.9+.
403    # You can get a list of valid specific ciphers from OpenSSL::Cipher.ciphers.
404    # You also can specify a cipher suite here, listed here at openssl.org:
405    # http://www.openssl.org/docs/apps/ciphers.html#CIPHER_SUITE_NAMES
406    #
407    #   class Foo
408    #     include HTTParty
409    #     ciphers "RC4-SHA"
410    #   end
411    def ciphers(cipher_names)
412      default_options[:ciphers] = cipher_names
413    end
414
415    # Allows setting an OpenSSL certificate authority file.  The file
416    # should contain one or more certificates in PEM format.
417    #
418    # Setting this option enables certificate verification.  All
419    # certificates along a chain must be available in ssl_ca_file or
420    # ssl_ca_path for verification to succeed.
421    #
422    #
423    #   class Foo
424    #     include HTTParty
425    #     ssl_ca_file '/etc/ssl/certs/ca-certificates.crt'
426    #   end
427    def ssl_ca_file(path)
428      default_options[:ssl_ca_file] = path
429    end
430
431    # Allows setting an OpenSSL certificate authority path (directory).
432    #
433    # Setting this option enables certificate verification.  All
434    # certificates along a chain must be available in ssl_ca_file or
435    # ssl_ca_path for verification to succeed.
436    #
437    #   class Foo
438    #     include HTTParty
439    #     ssl_ca_path '/etc/ssl/certs/'
440    #   end
441    def ssl_ca_path(path)
442      default_options[:ssl_ca_path] = path
443    end
444
445    # Allows setting a custom parser for the response.
446    #
447    #   class Foo
448    #     include HTTParty
449    #     parser Proc.new {|data| ...}
450    #   end
451    def parser(custom_parser = nil)
452      if custom_parser.nil?
453        default_options[:parser]
454      else
455        default_options[:parser] = custom_parser
456        validate_format
457      end
458    end
459
460    # Allows setting a custom URI adapter.
461    #
462    #   class Foo
463    #     include HTTParty
464    #     uri_adapter Addressable::URI
465    #   end
466    def uri_adapter(uri_adapter)
467      raise ArgumentError, 'The URI adapter should respond to #parse' unless uri_adapter.respond_to?(:parse)
468      default_options[:uri_adapter] = uri_adapter
469    end
470
471    # Allows setting a custom connection_adapter for the http connections
472    #
473    # @example
474    #   class Foo
475    #     include HTTParty
476    #     connection_adapter Proc.new {|uri, options| ... }
477    #   end
478    #
479    # @example provide optional configuration for your connection_adapter
480    #   class Foo
481    #     include HTTParty
482    #     connection_adapter Proc.new {|uri, options| ... }, {foo: :bar}
483    #   end
484    #
485    # @see HTTParty::ConnectionAdapter
486    def connection_adapter(custom_adapter = nil, options = nil)
487      if custom_adapter.nil?
488        default_options[:connection_adapter]
489      else
490        default_options[:connection_adapter] = custom_adapter
491        default_options[:connection_adapter_options] = options
492      end
493    end
494
495    # Allows making a get request to a url.
496    #
497    #   class Foo
498    #     include HTTParty
499    #   end
500    #
501    #   # Simple get with full url
502    #   Foo.get('http://foo.com/resource.json')
503    #
504    #   # Simple get with full url and query parameters
505    #   # ie: http://foo.com/resource.json?limit=10
506    #   Foo.get('http://foo.com/resource.json', query: {limit: 10})
507    def get(path, options = {}, &block)
508      perform_request Net::HTTP::Get, path, options, &block
509    end
510
511    # Allows making a post request to a url.
512    #
513    #   class Foo
514    #     include HTTParty
515    #   end
516    #
517    #   # Simple post with full url and setting the body
518    #   Foo.post('http://foo.com/resources', body: {bar: 'baz'})
519    #
520    #   # Simple post with full url using :query option,
521    #   # which appends the parameters to the URI.
522    #   Foo.post('http://foo.com/resources', query: {bar: 'baz'})
523    def post(path, options = {}, &block)
524      perform_request Net::HTTP::Post, path, options, &block
525    end
526
527    # Perform a PATCH request to a path
528    def patch(path, options = {}, &block)
529      perform_request Net::HTTP::Patch, path, options, &block
530    end
531
532    # Perform a PUT request to a path
533    def put(path, options = {}, &block)
534      perform_request Net::HTTP::Put, path, options, &block
535    end
536
537    # Perform a DELETE request to a path
538    def delete(path, options = {}, &block)
539      perform_request Net::HTTP::Delete, path, options, &block
540    end
541
542    # Perform a MOVE request to a path
543    def move(path, options = {}, &block)
544      perform_request Net::HTTP::Move, path, options, &block
545    end
546
547    # Perform a COPY request to a path
548    def copy(path, options = {}, &block)
549      perform_request Net::HTTP::Copy, path, options, &block
550    end
551
552    # Perform a HEAD request to a path
553    def head(path, options = {}, &block)
554      ensure_method_maintained_across_redirects options
555      perform_request Net::HTTP::Head, path, options, &block
556    end
557
558    # Perform an OPTIONS request to a path
559    def options(path, options = {}, &block)
560      perform_request Net::HTTP::Options, path, options, &block
561    end
562
563    # Perform a MKCOL request to a path
564    def mkcol(path, options = {}, &block)
565      perform_request Net::HTTP::Mkcol, path, options, &block
566    end
567
568    def lock(path, options = {}, &block)
569      perform_request Net::HTTP::Lock, path, options, &block
570    end
571
572    def unlock(path, options = {}, &block)
573      perform_request Net::HTTP::Unlock, path, options, &block
574    end
575
576    attr_reader :default_options
577
578    private
579
580    def validate_timeout_argument(timeout_type, value)
581      raise ArgumentError, "#{ timeout_type } must be an integer or float" unless value && (value.is_a?(Integer) || value.is_a?(Float))
582    end
583
584    def ensure_method_maintained_across_redirects(options)
585      unless options.key?(:maintain_method_across_redirects)
586        options[:maintain_method_across_redirects] = true
587      end
588    end
589
590    def perform_request(http_method, path, options, &block) #:nodoc:
591      options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
592      HeadersProcessor.new(headers, options).call
593      process_cookies(options)
594      Request.new(http_method, path, options).perform(&block)
595    end
596
597    def process_cookies(options) #:nodoc:
598      return unless options[:cookies] || default_cookies.any?
599      options[:headers] ||= headers.dup
600      options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
601    end
602
603    def validate_format
604      if format && parser.respond_to?(:supports_format?) && !parser.supports_format?(format)
605        supported_format_names = parser.supported_formats.map(&:to_s).sort.join(', ')
606        raise UnsupportedFormat, "'#{format.inspect}' Must be one of: #{supported_format_names}"
607      end
608    end
609  end
610
611  def self.normalize_base_uri(url) #:nodoc:
612    normalized_url = url.dup
613    use_ssl = (normalized_url =~ /^https/) || (normalized_url =~ /:443\b/)
614    ends_with_slash = normalized_url =~ /\/$/
615
616    normalized_url.chop! if ends_with_slash
617    normalized_url.gsub!(/^https?:\/\//i, '')
618
619    "http#{'s' if use_ssl}://#{normalized_url}"
620  end
621
622  class Basement #:nodoc:
623    include HTTParty
624  end
625
626  def self.get(*args, &block)
627    Basement.get(*args, &block)
628  end
629
630  def self.post(*args, &block)
631    Basement.post(*args, &block)
632  end
633
634  def self.patch(*args, &block)
635    Basement.patch(*args, &block)
636  end
637
638  def self.put(*args, &block)
639    Basement.put(*args, &block)
640  end
641
642  def self.delete(*args, &block)
643    Basement.delete(*args, &block)
644  end
645
646  def self.move(*args, &block)
647    Basement.move(*args, &block)
648  end
649
650  def self.copy(*args, &block)
651    Basement.copy(*args, &block)
652  end
653
654  def self.head(*args, &block)
655    Basement.head(*args, &block)
656  end
657
658  def self.options(*args, &block)
659    Basement.options(*args, &block)
660  end
661end
662
663require 'httparty/hash_conversions'
664require 'httparty/utils'
665require 'httparty/exceptions'
666require 'httparty/parser'
667require 'httparty/request'
668require 'httparty/response'