Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit d3def88

Browse files
committed
Add support for custom HTTP predelivery callbacks
This approach will allow us to define last minute callbacks before performing the HTTP delivery to webhook consumers. Now in the webhook application that relies on github-services, we can do something like this: ```ruby service = Service::Web.new if webhook.needs_signed_header? service.before_delivery do |url, payload, headers, params| signature = public_key.sign(message: payload) headers['GITHUB-PUBLIC-KEY-SIGNATURE'] = signature end end ```
1 parent e03332d commit d3def88

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/service.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ def inherited(svc)
485485
attr_reader :remote_calls
486486

487487
attr_accessor :needs_public_key_signature, :public_key
488+
attr_reader :pre_delivery_callbacks
488489

489490
def initialize(event = :push, data = {}, payload = nil)
490491
helper_name = "#{event.to_s.classify}Helpers"
@@ -502,6 +503,7 @@ def initialize(event = :push, data = {}, payload = nil)
502503
@http = @secrets = @email_config = nil
503504
@http_calls = []
504505
@remote_calls = []
506+
@pre_delivery_callbacks = []
505507
end
506508

507509
# Boolean fields as either nil, "0", or "1".
@@ -601,7 +603,7 @@ def http_get(url = nil, params = nil, headers = nil)
601603
#
602604
# Yields a Faraday::Request instance.
603605
# Returns a Faraday::Response instance.
604-
def http_post(url = nil, body = nil, headers = nil, params = nil)
606+
def http_post(url = nil, body = nil, headers = {}, params = {})
605607
block = Proc.new if block_given?
606608
http_method :post, url, body, headers, params, &block
607609
end
@@ -633,18 +635,22 @@ def http_post(url = nil, body = nil, headers = nil, params = nil)
633635
#
634636
# Yields a Faraday::Request instance.
635637
# Returns a Faraday::Response instance.
636-
def http_method(method, url = nil, body = nil, headers = nil, params = nil)
638+
def http_method(method, url = nil, body = nil, headers = {}, params = {})
637639
block = Proc.new if block_given?
638640

639641
url = url.strip if url
640642
raise_config_error("Invalid scheme") unless permitted_transport?(url)
641643

644+
if pre_delivery_callbacks.any?
645+
pre_delivery_callbacks.each { |c| c.call(url, body, headers, params) }
646+
end
647+
642648
check_ssl do
643649
http.send(method) do |req|
644650
req.url(url) if url
645-
req.headers.update(headers) if headers
651+
req.headers.update(headers) if headers.present?
646652
req.body = body if body
647-
req.params = params if params
653+
req.params = params if params.present?
648654
block.call req if block
649655
end
650656
end
@@ -854,6 +860,10 @@ def reportable_http_env(env, time)
854860
}
855861
end
856862

863+
def before_delivery(&block)
864+
@pre_delivery_callbacks << block
865+
end
866+
857867
# Raised when an unexpected error occurs during service hook execution.
858868
class Error < StandardError
859869
attr_reader :original_exception

0 commit comments

Comments
 (0)