Class: HTTPigeon::Request
- Inherits:
-
Object
- Object
- HTTPigeon::Request
- Defined in:
- lib/httpigeon/request.rb
Constant Summary collapse
- REQUEST_ID_HEADER =
'X-Request-Id'.freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#fuse ⇒ Object
readonly
Returns the value of attribute fuse.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .delete(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) ⇒ Object
- .get(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) ⇒ Object
- .post(endpoint, payload, headers = {}, event_type = nil, log_filters = []) ⇒ Object
- .put(endpoint, payload, headers = {}, event_type = nil, log_filters = []) ⇒ Object
Instance Method Summary collapse
-
#initialize(base_url:, options: nil, headers: nil, adapter: nil, logger: nil, event_type: nil, log_filters: nil, fuse_config: nil) ⇒ Request
constructor
A new instance of Request.
- #run(method: :get, path: '/', payload: {}) ⇒ Object
Constructor Details
#initialize(base_url:, options: nil, headers: nil, adapter: nil, logger: nil, event_type: nil, log_filters: nil, fuse_config: nil) ⇒ Request
Returns a new instance of Request.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/httpigeon/request.rb', line 45 def initialize(base_url:, options: nil, headers: nil, adapter: nil, logger: nil, event_type: nil, log_filters: nil, fuse_config: nil) @base_url = URI.parse(base_url) request_headers = default_headers.merge(headers.to_h) fuse_config_opts = { service_id: @base_url.host }.merge(fuse_config.to_h) @fuse = CircuitBreaker::Fuse.(fuse_config_opts) base_connection = Faraday.new(url: @base_url.to_s).tap do |config| config.headers.deep_merge!(request_headers) config..merge!(.to_h) config.response :circuit_breaker, fuse.config if HTTPigeon.mount_circuit_breaker config.response :httpigeon_logger, logger if logger.is_a?(HTTPigeon::Logger) end @connection = if block_given? yield(base_connection) && base_connection else base_connection.tap do |faraday| faraday.headers.deep_merge!(request_headers) faraday..merge!(.to_h) faraday.request :url_encoded faraday.adapter adapter || Faraday.default_adapter faraday.response :httpigeon_logger, default_logger(event_type, log_filters) unless logger.is_a?(HTTPigeon::Logger) end end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
41 42 43 |
# File 'lib/httpigeon/request.rb', line 41 def base_url @base_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
41 42 43 |
# File 'lib/httpigeon/request.rb', line 41 def connection @connection end |
#fuse ⇒ Object (readonly)
Returns the value of attribute fuse.
41 42 43 |
# File 'lib/httpigeon/request.rb', line 41 def fuse @fuse end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
41 42 43 |
# File 'lib/httpigeon/request.rb', line 41 def response @response end |
Class Method Details
.delete(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/httpigeon/request.rb', line 33 def delete(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) request = new(base_url: endpoint, headers: headers, event_type: event_type, log_filters: log_filters) request.run(method: :delete, path: '', payload: query) do |req| yield(req) if block_given? end end |
.get(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/httpigeon/request.rb', line 12 def get(endpoint, query = {}, headers = {}, event_type = nil, log_filters = []) request = new(base_url: endpoint, headers: headers, event_type: event_type, log_filters: log_filters) request.run(method: :get, path: '', payload: query) do |req| yield(req) if block_given? end end |
.post(endpoint, payload, headers = {}, event_type = nil, log_filters = []) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/httpigeon/request.rb', line 19 def post(endpoint, payload, headers = {}, event_type = nil, log_filters = []) request = new(base_url: endpoint, headers: headers, event_type: event_type, log_filters: log_filters) request.run(method: :post, path: '', payload: payload) do |req| yield(req) if block_given? end end |
.put(endpoint, payload, headers = {}, event_type = nil, log_filters = []) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/httpigeon/request.rb', line 26 def put(endpoint, payload, headers = {}, event_type = nil, log_filters = []) request = new(base_url: endpoint, headers: headers, event_type: event_type, log_filters: log_filters) request.run(method: :put, path: '', payload: payload) do |req| yield(req) if block_given? end end |
Instance Method Details
#run(method: :get, path: '/', payload: {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/httpigeon/request.rb', line 72 def run(method: :get, path: '/', payload: {}) unless method.to_sym == :get || method.to_sym == :delete payload = payload.presence&.to_json connection.headers['Content-Type'] = 'application/json' end connection.headers[REQUEST_ID_HEADER] = SecureRandom.uuid if HTTPigeon.auto_generate_request_id raw_response = if HTTPigeon.mount_circuit_breaker fuse.execute(request_id: connection.headers[REQUEST_ID_HEADER]) do connection.send(method, path, payload) do |request| yield(request) if block_given? end end else connection.send(method, path, payload) do |request| yield(request) if block_given? end end @response = HTTPigeon::Response.new(self, raw_response) end |