]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/lib/puppet/feature/faraday/request.rb
Revert "add stackforge/keystone to 3rdparty"
[dsa-puppet.git] / 3rdparty / modules / aviator / lib / puppet / feature / faraday / request.rb
1 module Faraday
2   # Used to setup urls, params, headers, and the request body in a sane manner.
3   #
4   #   @connection.post do |req|
5   #     req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
6   #     req.headers['b'] = '2' # Header
7   #     req.params['c']  = '3' # GET Param
8   #     req['b']         = '2' # also Header
9   #     req.body = 'abc'
10   #   end
11   #
12   class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
13     extend MiddlewareRegistry
14
15     register_middleware File.expand_path('../request', __FILE__),
16       :url_encoded => [:UrlEncoded, 'url_encoded'],
17       :multipart => [:Multipart, 'multipart'],
18       :retry => [:Retry, 'retry'],
19       :authorization => [:Authorization, 'authorization'],
20       :basic_auth => [:BasicAuthentication, 'basic_authentication'],
21       :token_auth => [:TokenAuthentication, 'token_authentication'],
22       :instrumentation => [:Instrumentation, 'instrumentation']
23
24     def self.create(request_method)
25       new(request_method).tap do |request|
26         yield(request) if block_given?
27       end
28     end
29
30     # Public: Replace params, preserving the existing hash type
31     def params=(hash)
32       if params
33         params.replace hash
34       else
35         super
36       end
37     end
38
39     # Public: Replace request headers, preserving the existing hash type
40     def headers=(hash)
41       if headers
42         headers.replace hash
43       else
44         super
45       end
46     end
47
48     def url(path, params = nil)
49       if path.respond_to? :query
50         if query = path.query
51           path = path.dup
52           path.query = nil
53         end
54       else
55         path, query = path.split('?', 2)
56       end
57       self.path = path
58       self.params.merge_query query, options.params_encoder
59       self.params.update(params) if params
60     end
61
62     def [](key)
63       headers[key]
64     end
65
66     def []=(key, value)
67       headers[key] = value
68     end
69
70     # ENV Keys
71     # :method - a symbolized request method (:get, :post)
72     # :body   - the request body that will eventually be converted to a string.
73     # :url    - URI instance for the current request.
74     # :status           - HTTP response status code
75     # :request_headers  - hash of HTTP Headers to be sent to the server
76     # :response_headers - Hash of HTTP headers from the server
77     # :parallel_manager - sent if the connection is in parallel mode
78     # :request - Hash of options for configuring the request.
79     #   :timeout      - open/read timeout Integer in seconds
80     #   :open_timeout - read timeout Integer in seconds
81     #   :proxy        - Hash of proxy options
82     #     :uri        - Proxy Server URI
83     #     :user       - Proxy server username
84     #     :password   - Proxy server password
85     # :ssl - Hash of options for configuring SSL requests.
86     def to_env(connection)
87       Env.new(method, body, connection.build_exclusive_url(path, params),
88         options, headers, connection.ssl, connection.parallel_manager)
89     end
90   end
91 end
92