Class: Dagger::Client
- Inherits:
-
Object
- Object
- Dagger::Client
- Defined in:
- lib/dagger.rb
Class Method Summary collapse
- .init(uri, opts) ⇒ Object
- .init_connection(uri, opts = {}) ⇒ Object
- .init_persistent(opts = {}) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #delete(uri, data, options = {}) ⇒ Object
- #get(uri, opts = {}) ⇒ Object
-
#initialize(http, host = nil) ⇒ Client
constructor
A new instance of Client.
- #open(&block) ⇒ Object
- #patch(uri, data, options = {}) ⇒ Object
- #post(uri, data, options = {}) ⇒ Object
- #put(uri, data, options = {}) ⇒ Object
- #request(method, uri, data, opts = {}) ⇒ Object
- #response ⇒ Object
Constructor Details
#initialize(http, host = nil) ⇒ Client
Returns a new instance of Client.
118 119 120 |
# File 'lib/dagger.rb', line 118 def initialize(http, host = nil) @http, @host = http, host end |
Class Method Details
.init(uri, opts) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/dagger.rb', line 106 def self.init(uri, opts) uri = Utils.parse_uri(uri) http = if opts.delete(:persistent) init_persistent(opts) else init_connection(uri, opts) end new(http, uri.scheme_and_host) end |
.init_connection(uri, opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dagger.rb', line 91 def self.init_connection(uri, opts = {}) http = Net::HTTP.new(opts[:ip] || uri.host, uri.port) if uri.port == 443 || uri.scheme == 'https' http.use_ssl = true if http.respond_to?(:use_ssl=) # persistent does it automatically http.verify_mode = opts[:verify_ssl] === false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER end [:keep_alive_timeout, :open_timeout, :read_timeout, :ssl_version, :ciphers].each do |key| http.send("#{key}=", opts[key] || DEFAULTS[key]) if (opts.has_key?(key) || DEFAULTS.has_key?(key)) end http end |
.init_persistent(opts = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/dagger.rb', line 81 def self.init_persistent(opts = {}) # this line below forces one connection manager between multiple threads # @persistent ||= Dagger::ConnectionManager.new(opts) # here we initialize a connection manager for each thread Thread.current[:dagger_persistent] ||= begin Dagger::ConnectionManager.new(opts) end end |
Instance Method Details
#close ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/dagger.rb', line 256 def close if @http.is_a?(Dagger::ConnectionManager) @http.shutdown # calls finish on pool connections else @http.finish if @http.started? end end |
#delete(uri, data, options = {}) ⇒ Object
183 184 185 |
# File 'lib/dagger.rb', line 183 def delete(uri, data, = {}) request(:delete, uri, data, ) end |
#get(uri, opts = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dagger.rb', line 122 def get(uri, opts = {}) uri = Utils.resolve_uri(uri, @host, opts[:query]) if @host != uri.scheme_and_host raise ArgumentError.new("#{uri.scheme_and_host} does not match #{@host}") end opts[:follow] = 10 if opts[:follow] == true headers = opts[:headers] || {} headers['Accept'] = 'application/json' if opts[:json] && headers['Accept'].nil? headers['Content-Type'] = 'application/json' if opts[:json] && opts[:body] if opts[:ip] headers['Host'] = uri.host uri = opts[:ip] end request = Net::HTTP::Get.new(uri, DEFAULT_HEADERS.merge(headers)) request.basic_auth(opts.delete(:username), opts.delete(:password)) if opts[:username] request.body = Utils.encode_body(opts[:body], opts) if opts[:body] if @http.respond_to?(:started?) # regular Net::HTTP @http.start unless @http.started? resp, data = @http.request(request) else # persistent resp, data = @http.send_request(uri, request) end if REDIRECT_CODES.include?(resp.code.to_i) && resp['Location'] && (opts[:follow] && opts[:follow] > 0) opts[:follow] -= 1 debug { "Following redirect to #{resp['Location']}" } return get(resp['Location'], opts) end @response = build_response(resp, data || resp.body) rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EINVAL, Timeout::Error, \ Net::OpenTimeout, Net::ReadTimeout, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, \ SocketError, EOFError, OpenSSL::SSL::SSLError => e if retries = opts[:retries] and retries.to_i > 0 debug { "Got #{e.class}! Retrying in a sec (#{retries} retries left)" } sleep (opts[:retry_wait] || DEFAULT_RETRY_WAIT) get(uri, opts.merge(retries: retries - 1)) else raise end end |
#open(&block) ⇒ Object
246 247 248 249 250 251 252 253 254 |
# File 'lib/dagger.rb', line 246 def open(&block) if @http.is_a?(Dagger::ConnectionManager) instance_eval(&block) else @http.start do instance_eval(&block) end end end |
#patch(uri, data, options = {}) ⇒ Object
179 180 181 |
# File 'lib/dagger.rb', line 179 def patch(uri, data, = {}) request(:patch, uri, data, ) end |
#post(uri, data, options = {}) ⇒ Object
171 172 173 |
# File 'lib/dagger.rb', line 171 def post(uri, data, = {}) request(:post, uri, data, ) end |
#put(uri, data, options = {}) ⇒ Object
175 176 177 |
# File 'lib/dagger.rb', line 175 def put(uri, data, = {}) request(:put, uri, data, ) end |
#request(method, uri, data, opts = {}) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/dagger.rb', line 187 def request(method, uri, data, opts = {}) if method.to_s.downcase == 'get' data ||= opts[:body] return get(uri, opts.merge(body: data)) end uri = Utils.resolve_uri(uri, @host, opts[:query]) if @host != uri.scheme_and_host raise ArgumentError.new("#{uri.scheme_and_host} does not match #{@host}") end headers = DEFAULT_HEADERS.merge(opts[:headers] || {}) body = Utils.encode_body(data, opts) if opts[:username] # opts[:password] is optional str = [opts[:username], opts[:password]].compact.join(':') headers['Authorization'] = 'Basic ' + Base64.encode64(str) end if opts[:json] headers['Content-Type'] = 'application/json' headers['Accept'] = 'application/json' if headers['Accept'].nil? end start = Time.now debug { "Sending #{method} request to #{uri.request_uri} with headers #{headers.inspect} -- #{query}" } if @http.respond_to?(:started?) # regular Net::HTTP args = [method.to_s.downcase, uri.request_uri, body, headers] args.delete_at(2) if args[0] == 'delete' # Net::HTTP's delete does not accept data @http.start unless @http.started? resp, data = @http.send(*args) else # Net::HTTP::Persistent req = Kernel.const_get("Net::HTTP::#{method.capitalize}").new(uri.request_uri, headers) req.body = body resp, data = @http.send_request(uri, req) end debug { "Got response #{resp.code} in #{(Time.now - start).round(2)}s: #{data || resp.body}" } @response = build_response(resp, data || resp.body) rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EINVAL, Timeout::Error, \ Net::OpenTimeout, Net::ReadTimeout, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, \ SocketError, EOFError, OpenSSL::SSL::SSLError => e if method.to_s.downcase != 'get' && retries = opts[:retries] and retries.to_i > 0 debug { "Got #{e.class}! Retrying in a sec (#{retries} retries left)" } sleep (opts[:retry_wait] || DEFAULT_RETRY_WAIT) request(method, uri, data, opts.merge(retries: retries - 1)) else raise end end |
#response ⇒ Object
242 243 244 |
# File 'lib/dagger.rb', line 242 def response @response or raise 'Request not sent!' end |