Class: Dagger::Client
- Inherits:
-
Object
- Object
- Dagger::Client
- Defined in:
- lib/dagger.rb
Class Method Summary collapse
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.
110 111 112 |
# File 'lib/dagger.rb', line 110 def initialize(http, host = nil) @http, @host = http, host end |
Class Method Details
.init(uri, opts) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/dagger.rb', line 103 def self.init(uri, opts) uri = Utils.parse_uri(uri) http = init_connection(uri, opts) new(http, uri.scheme_and_host) end |
.init_connection(uri, opts = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dagger.rb', line 83 def self.init_connection(uri, opts = {}) http = if opts.delete(:persistent) pool_size = opts[:pool_size] || Net::HTTP::Persistent::DEFAULT_POOL_SIZE Net::HTTP::Persistent.new(name: DAGGER_NAME, pool_size: pool_size) else Net::HTTP.new(opts[:ip] || uri.host, uri.port) end 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 [: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 |
Instance Method Details
#close ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/dagger.rb', line 250 def close if @http.is_a?(Net::HTTP::Persistent) @http.shutdown # calls finish on pool connections else @http.finish if @http.started? end end |
#delete(uri, data, options = {}) ⇒ Object
177 178 179 |
# File 'lib/dagger.rb', line 177 def delete(uri, data, = {}) request(:delete, uri, data, ) end |
#get(uri, opts = {}) ⇒ Object
114 115 116 117 118 119 120 121 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 |
# File 'lib/dagger.rb', line 114 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] && opts[:body].size > 0 if opts[:ip] headers['Host'] = uri.host uri = opts[:ip] end debug { "Sending GET request to #{uri.request_uri} with headers #{headers.inspect} -- #{opts[:body]}" } 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] && opts[:body].size > 0 if @http.respond_to?(:started?) # regular Net::HTTP @http.start unless @http.started? resp, data = @http.request(request) else # persistent resp, data = @http.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
240 241 242 243 244 245 246 247 248 |
# File 'lib/dagger.rb', line 240 def open(&block) if @http.is_a?(Net::HTTP::Persistent) instance_eval(&block) else @http.start do instance_eval(&block) end end end |
#patch(uri, data, options = {}) ⇒ Object
173 174 175 |
# File 'lib/dagger.rb', line 173 def patch(uri, data, = {}) request(:patch, uri, data, ) end |
#post(uri, data, options = {}) ⇒ Object
165 166 167 |
# File 'lib/dagger.rb', line 165 def post(uri, data, = {}) request(:post, uri, data, ) end |
#put(uri, data, options = {}) ⇒ Object
169 170 171 |
# File 'lib/dagger.rb', line 169 def put(uri, data, = {}) request(:put, uri, data, ) end |
#request(method, uri, data, opts = {}) ⇒ Object
181 182 183 184 185 186 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 |
# File 'lib/dagger.rb', line 181 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.strict_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} -- #{data}" } 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.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
236 237 238 |
# File 'lib/dagger.rb', line 236 def response @response or raise 'Request not sent!' end |