Skip to content

Commit

Permalink
Support using http_set_header and http_remove_header as blocks
Browse files Browse the repository at this point in the history
Example:

```ruby
http_set_header "Authorization", "Bearer 12345.."
http_remove_header "Authorization" do
  http_get "/api/users", status: 403
end

http_get "/api/users", status: 200
```

Signed-off-by: Aravinda VK <[email protected]>
  • Loading branch information
aravindavk committed Jan 28, 2024
1 parent 160499f commit de81e2e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/kadalu/binnacle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def start(task_file, opts)
if stdout_line.start_with?('{')
begin
data = JSON.parse(stdout_line, { symbolize_names: true })

# Without ok field this may not be a summary line
next unless data.key?(:ok)

metrics[:tasks] << data
Messages.task_summary(data)
if data[:ok]
Expand Down
46 changes: 45 additions & 1 deletion lib/kadalu/binnacle/plugins/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,61 @@ module Binnacle

# Add a header to the request
#
# Using as Block
#
# ```
# http_base_url "http://localhost:5001"
# http_add_header "Authorization", "Bearer 12345" do
# http_get "/api/folders"
# end
# ```
#
# or without block
#
# ```
# http_base_url "http://localhost:5001"
# http_add_header "Authorization", "Bearer 12345"
# http_get "/api/folders"
# ```
register_plugin 'http_add_header' do |name, value, &block|
Store.hash_add(:request_headers, name, value, &block)
Store.hash_set(:request_headers, name, value, &block)
end

# Set the header to the request
#
# Using as Block
#
# ```
# http_base_url "http://localhost:5001"
# http_set_header "Authorization", "Bearer 12345" do
# http_get "/api/folders"
# end
# ```
#
# or without block
#
# ```
# http_base_url "http://localhost:5001"
# http_set_header "Authorization", "Bearer 12345"
# http_get "/api/folders"
# ```
register_plugin 'http_set_header' do |name, value, &block|
Store.hash_set(:request_headers, name, value, &block)
end

# Remove header from the request
#
# Using as a Block
#
# ```
# http_base_url "http://localhost:5001"
# http_remove_header "Authorization" do
# http_get "/api/folders", status: 403
# end
# ```
#
# or without block
#
# ```
# http_base_url "http://localhost:5001"
# http_remove_header "Authorization"
Expand Down
16 changes: 10 additions & 6 deletions lib/kadalu/binnacle/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ def self.set(name, value, &block)
nil
end

def self.hash_add(hash_name, name, value, &block)
def self.hash_set(hash_name, name, value, &block)
@data[hash_name] = {} unless @data.key?(hash_name)

if block
prev_value = get(hash_name)
prev_value = @data[hash_name][name]
@data[hash_name][name] = value
block.call
@data[hash_name] = prev_value
if prev_value
@data[hash_name][name] = prev_value
else
@data[hash_name].delete(name)
end
else
@data[hash_name][name] = value
end
Expand All @@ -56,10 +60,10 @@ def self.hash_remove(hash_name, name, &block)
@data[hash_name] = {} unless @data.key?(hash_name)

if block
prev_value = get(hash_name)
@data[hash_name].delete(name) if @data[hash_name].key?(name)
prev_value = @data[hash_name][name]
@data[hash_name].delete(name) if prev_value
block.call
@data[hash_name] = prev_value
@data[hash_name][name] = prev_value if prev_value
elsif @data[hash_name].key?(name)
@data[hash_name].delete(name)
end
Expand Down

0 comments on commit de81e2e

Please sign in to comment.