-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add request and response hooks (#278)
- Loading branch information
1 parent
f6b54ff
commit 2c28ca1
Showing
14 changed files
with
629 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,8 @@ | |
# Internal Utilities | ||
require 'easypost/internal_utilities' | ||
|
||
# Hooks | ||
require 'easypost/hooks' | ||
|
||
module EasyPost | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module EasyPost::Hooks | ||
def self.subscribe(type, name, block) | ||
subscribers[type][name] = block | ||
|
||
name | ||
end | ||
|
||
def self.unsubscribe(type, name) | ||
subscribers[type].delete(name) | ||
end | ||
|
||
def self.unsubscribe_all(type) | ||
subscribers.delete(type) | ||
end | ||
|
||
def self.notify(type, context) | ||
subscribers[type].each_value { |subscriber| subscriber.call(context) } | ||
end | ||
|
||
def self.any_subscribers?(type) | ||
!subscribers[type].empty? | ||
end | ||
|
||
def self.subscribers | ||
@subscribers ||= Hash.new { |hash, key| hash[key] = {} } | ||
end | ||
|
||
private_class_method :subscribers | ||
end | ||
|
||
require_relative 'hooks/request_context' | ||
require_relative 'hooks/response_context' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class EasyPost::Hooks::RequestContext | ||
attr_reader :method, :path, :headers, :request_body, :request_timestamp, :request_uuid | ||
|
||
def initialize(method:, path:, headers:, request_body:, request_timestamp:, request_uuid:) | ||
@method = method | ||
@path = path | ||
@headers = headers | ||
@request_body = request_body | ||
@request_timestamp = request_timestamp | ||
@request_uuid = request_uuid | ||
|
||
freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class EasyPost::Hooks::ResponseContext | ||
attr_reader :http_status, :method, :path, :headers, :response_body, | ||
:request_timestamp, :response_timestamp, :request_uuid, | ||
:client_response_object | ||
|
||
def initialize(http_status:, method:, path:, headers:, response_body:, | ||
request_timestamp:, response_timestamp:, request_uuid:, | ||
client_response_object:) | ||
@http_status = http_status | ||
@method = method | ||
@path = path | ||
@headers = headers | ||
@response_body = response_body | ||
@request_timestamp = request_timestamp | ||
@response_timestamp = response_timestamp | ||
@request_uuid = request_uuid | ||
@client_response_object = client_response_object | ||
|
||
freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
spec/cassettes/client/EasyPost_Client_client_object_hooks_notifies_multiple_subscribers.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.