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.
86 87 88 |
# File 'lib/dagger.rb', line 86 def initialize(http, host = nil) @http, @host = http, host end |
Class Method Details
.init(uri, opts) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dagger.rb', line 64 def self.init(uri, opts) uri = Utils.parse_uri(uri) 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 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]) if opts.has_key?(key) end # new(http, [uri.scheme, uri.host].join('://')) new(http, uri.scheme_and_host) end |
Instance Method Details
#close ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/dagger.rb', line 220 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
147 148 149 |
# File 'lib/dagger.rb', line 147 def delete(uri, data, = {}) request(:delete, uri, data, ) end |
#get(uri, opts = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dagger.rb', line 90 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} doesn't match #{@host}") end opts[:follow] = 10 if opts[:follow] == true headers = opts[:headers] || {} headers['Accept'] = 'application/json' if opts[:json] && headers['Accept'].nil? 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] 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, \ SocketError, EOFError, Net::ReadTimeout, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, 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
210 211 212 213 214 215 216 217 218 |
# File 'lib/dagger.rb', line 210 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
143 144 145 |
# File 'lib/dagger.rb', line 143 def patch(uri, data, = {}) request(:patch, uri, data, ) end |
#post(uri, data, options = {}) ⇒ Object
135 136 137 |
# File 'lib/dagger.rb', line 135 def post(uri, data, = {}) request(:post, uri, data, ) end |
#put(uri, data, options = {}) ⇒ Object
139 140 141 |
# File 'lib/dagger.rb', line 139 def put(uri, data, = {}) request(:put, uri, data, ) end |
#request(method, uri, data, opts = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/dagger.rb', line 151 def request(method, uri, data, opts = {}) if method.to_s.downcase == 'get' query = (opts[:query] || {}).merge(data || {}) return get(uri, opts.merge(query: query)) end uri = Utils.resolve_uri(uri, @host) if @host != uri.scheme_and_host raise ArgumentError.new("#{uri.scheme_and_host} doesn't match #{@host}") end headers = DEFAULT_HEADERS.merge(opts[:headers] || {}) query = if data.is_a?(String) data elsif opts[:json] headers['Content-Type'] = 'application/json' headers['Accept'] = 'application/json' if headers['Accept'].nil? Oj.dump(data, mode: :compat) # compat ensures symbols are converted to strings else # querystring, then Utils.encode(data) end if opts[:username] # opts[:password] is optional str = [opts[:username], opts[:password]].compact.join(':') headers['Authorization'] = 'Basic ' + Base64.encode64(str) end if @http.respond_to?(:started?) # regular Net::HTTP args = [method.to_s.downcase, uri.path, query, 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.path, headers) # req.set_form_data(query) req.body = query resp, data = @http.request(uri, req) 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 method.to_s.downcase != 'get' && retries = opts[:retries] and retries.to_i > 0 debug "[#{DAGGER_NAME}] 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
206 207 208 |
# File 'lib/dagger.rb', line 206 def response @response or raise 'Request not sent!' end |