Class: Grifter::HTTPService
- Includes:
- JsonHelpers, Log
- Defined in:
- lib/grifter/http_service.rb
Constant Summary collapse
- RequestLogSeperator =
'-'*40
Constants included from Log
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#headers ⇒ Object
allows for doing some fancy stuff in threading.
-
#http ⇒ Object
readonly
allow stubbing http if we are testing.
-
#last_request ⇒ Object
readonly
this is useful for testing apis, and other times you want to interrogate the http details of a response.
-
#last_response ⇒ Object
readonly
this is useful for testing apis, and other times you want to interrogate the http details of a response.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
-
#do_request(method, path, obj = nil, options = {}) ⇒ Object
do_request performs the actual request, and does associated logging options can include: - :timeout, which specifies num secs the request should timeout in (this turns out to be kind of annoying to implement).
- #get(path, options = {}) ⇒ Object
- #head(path, options = {}) ⇒ Object
- #in_parallel(&blk) ⇒ Object
-
#initialize(config) ⇒ HTTPService
constructor
A new instance of HTTPService.
- #make_headers(options) ⇒ Object
-
#make_path(path_suffix, base_uri = nil) ⇒ Object
add base uri to request.
- #options(path, options = {}) ⇒ Object
- #patch(path, obj, options = {}) ⇒ Object
- #post(path, obj, options = {}) ⇒ Object
- #post_form(path, params, options = {}) ⇒ Object
- #put(path, obj, options = {}) ⇒ Object
- #req_args(path, options) ⇒ Object
- #stubs(&blk) ⇒ Object
Methods included from JsonHelpers
Methods included from Log
#add_logger, #level=, #log, #logger, #loggers
Constructor Details
#initialize(config) ⇒ HTTPService
Returns a new instance of HTTPService.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/grifter/http_service.rb', line 14 def initialize config @config = config @name = config[:name] @base_uri = config[:base_uri] @log_headers = config.fetch(:log_headers, true) @log_bodies = config.fetch(:log_bodies, true) logger.debug "Configuring service '#{@name}' with:\n\t#{@config.inspect}" #@conn = Net::HTTP.new(@config[:hostname], @config[:port]) #@conn.use_ssl = @config[:ssl] @conn = Faraday.new @config[:faraday_url] do |conn_builder| #do our own logging #conn_builder.response logger: logger #conn_builder.adapter Faraday.default_adapter # make requests with Net::HTTP conn_builder.adapter :typhoeus conn_builder.ssl[:verify] = false if @config[:ignore_ssl_cert] #this nonsense dont work?! https://github.com/lostisland/faraday_middleware/issues/76 #conn_builder.use :instrumentation end @headers = { 'accept' => 'application/json', 'content-type' => 'application/json', } if @config[:default_headers] logger.debug "Default headers configured: " + @config[:default_headers].inspect @config[:default_headers].each_pair do |k, v| @headers[k.to_s] = v.to_s end end @default_timeout = @config.fetch(:timeout, 10) logger.info "Initialized grifter service '#{@name}'" end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
61 62 63 |
# File 'lib/grifter/http_service.rb', line 61 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
61 62 63 |
# File 'lib/grifter/http_service.rb', line 61 def conn @conn end |
#headers ⇒ Object
allows for doing some fancy stuff in threading
62 63 64 |
# File 'lib/grifter/http_service.rb', line 62 def headers @headers end |
#http ⇒ Object (readonly)
allow stubbing http if we are testing
60 61 62 |
# File 'lib/grifter/http_service.rb', line 60 def http @http end |
#last_request ⇒ Object (readonly)
this is useful for testing apis, and other times you want to interrogate the http details of a response
66 67 68 |
# File 'lib/grifter/http_service.rb', line 66 def last_request @last_request end |
#last_response ⇒ Object (readonly)
this is useful for testing apis, and other times you want to interrogate the http details of a response
66 67 68 |
# File 'lib/grifter/http_service.rb', line 66 def last_response @last_response end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
61 62 63 |
# File 'lib/grifter/http_service.rb', line 61 def name @name end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
183 184 185 |
# File 'lib/grifter/http_service.rb', line 183 def delete path, ={} do_request :delete, path, nil, end |
#do_request(method, path, obj = nil, options = {}) ⇒ Object
do_request performs the actual request, and does associated logging options can include:
-
:timeout, which specifies num secs the request should timeout in (this turns out to be kind of annoying to implement)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 134 135 136 137 138 |
# File 'lib/grifter/http_service.rb', line 74 def do_request method, path, obj=nil, ={} #grifter clients pass in path possibly including query params. #Faraday needs the query and path seperately. parsed = URI.parse make_path(path) #faraday needs the request params as a hash. #this turns out to be non-trivial query_hash = if parsed.query cgi_hash = CGI.parse(parsed.query) #make to account for one param having multiple values cgi_hash.inject({}) { |h,(k,v)| h[k] = v[1] ? v : v.first; h } else nil end req_headers = make_headers() body = if [:form] URI.encode_www_form obj else jsonify(obj) end #log the request logger.debug [ "Doing request: #{@name}: #{method.to_s.upcase} #{path}", @log_headers ? ["Request Headers:", req_headers.map{ |k, v| "#{k}: #{v.inspect}" }] : nil, @log_bodies ? ["Request Body:", body] : nil, ].flatten.compact.join("\n") #doing it this way avoids problem with OPTIONS method: https://github.com/lostisland/faraday/issues/305 response = nil metrics_obj = { method: method, service: @name, path: path } ActiveSupport::Notifications.instrument('request.grifter', metrics_obj) do response = @conn.run_request(method, nil, nil, nil) do |req| req.path = parsed.path req.params = metrics_obj[:params] = query_hash if query_hash req.headers = req_headers req.body = body req.[:timeout] = .fetch(:timeout, @default_timeout) end metrics_obj[:response] = response end logger.info "Request status: (#{response.status}) #{@name}: #{method.to_s.upcase} #{path}" #@last_request = req @last_response = response response_obj = objectify response.body if response.headers['content-type'] =~ /json/ logger.debug [ "Response Details:", @log_headers ? ["Response Headers:", response.headers.map { |k, v| "#{k}: #{v.inspect}" }] : nil, @log_bodies ? [ "Response Body:", jsonify(response_obj)] : nil, '' ].flatten.compact.join("\n") end raise RequestException.new(nil, response) unless response.status >= 200 and response.status < 300 return response_obj end |
#get(path, options = {}) ⇒ Object
171 172 173 |
# File 'lib/grifter/http_service.rb', line 171 def get path, ={} do_request :get, path, nil, end |
#head(path, options = {}) ⇒ Object
175 176 177 |
# File 'lib/grifter/http_service.rb', line 175 def head path, ={} do_request :head, path, nil, end |
#in_parallel(&blk) ⇒ Object
140 141 142 143 |
# File 'lib/grifter/http_service.rb', line 140 def in_parallel &blk @conn.headers = @headers @conn.in_parallel &blk end |
#make_headers(options) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/grifter/http_service.rb', line 155 def make_headers headers = if [:additional_headers] @headers.merge [:additional_headers] elsif [:headers] [:headers] else @headers.clone end headers['content-type'] = 'application/x-www-form-urlencoded' if [:form] headers end |
#make_path(path_suffix, base_uri = nil) ⇒ Object
add base uri to request
146 147 148 149 150 151 152 153 |
# File 'lib/grifter/http_service.rb', line 146 def make_path path_suffix, base_uri=nil base_uri_to_use = base_uri ? base_uri : @base_uri if base_uri_to_use base_uri_to_use + path_suffix else path_suffix end end |
#options(path, options = {}) ⇒ Object
179 180 181 |
# File 'lib/grifter/http_service.rb', line 179 def path, ={} do_request :options, path, nil, end |
#patch(path, obj, options = {}) ⇒ Object
195 196 197 |
# File 'lib/grifter/http_service.rb', line 195 def patch path, obj, ={} do_request :patch, path, obj, end |
#post(path, obj, options = {}) ⇒ Object
187 188 189 |
# File 'lib/grifter/http_service.rb', line 187 def post path, obj, ={} do_request :post, path, obj, end |
#post_form(path, params, options = {}) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/grifter/http_service.rb', line 199 def post_form path, params, ={} do_request :post, path, params, .merge(form: true) #request_obj = Net::HTTP::Post.new(*req_args(path, options)) #request_obj.set_form_data params #do_request request_obj, options end |
#put(path, obj, options = {}) ⇒ Object
191 192 193 |
# File 'lib/grifter/http_service.rb', line 191 def put path, obj, ={} do_request :put, path, obj, end |
#req_args(path, options) ⇒ Object
167 168 169 |
# File 'lib/grifter/http_service.rb', line 167 def req_args path, [make_path(path, [:base_uri]), make_headers()] end |
#stubs(&blk) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/grifter/http_service.rb', line 51 def stubs &blk stubs = Faraday::Adapter::Test::Stubs.new @conn = Faraday.new @config[:faraday_url] do |conn_builder| conn_builder.adapter :test, stubs end stubs end |