Skip to content

Commit

Permalink
fix: Execute block within scope (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
2k-joker authored Jul 11, 2024
1 parent 9a8b048 commit a9a43c3
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/httpigeon/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def delete(endpoint, query = {}, headers = {}, event_type = nil, log_filters = [
end
end

attr_reader :connection, :response, :parsed_response, :base_url, :fuse
attr_reader :connection, :response, :base_url, :fuse

delegate :status, :body, to: :response, prefix: true
delegate :status, :body, :parsed_response, to: :response, prefix: true

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)
Expand Down Expand Up @@ -77,7 +77,17 @@ def run(method: :get, path: '/', payload: {})

connection.headers[REQUEST_ID_HEADER] = SecureRandom.uuid if HTTPigeon.auto_generate_request_id

raw_response = HTTPigeon.mount_circuit_breaker ? run_with_fuse(method, path, payload) : simple_run(method, path, payload)
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
Expand All @@ -93,19 +103,5 @@ def default_logger(event_type, log_filters)
def default_headers
{ 'Accept' => 'application/json' }
end

def simple_run(method, path, payload)
connection.send(method, path, payload) do |request|
yield(request) if block_given?
end
end

def run_with_fuse(method, path, payload)
fuse.execute(request_id: connection.headers[REQUEST_ID_HEADER]) do
simple_run(method, path, payload) do |request|
yield(request) if block_given?
end
end
end
end
end

0 comments on commit a9a43c3

Please sign in to comment.