Skip to content

Commit

Permalink
update webhook model with signature header
Browse files Browse the repository at this point in the history
add signature header to webhook.rb

ut with factory

lint
  • Loading branch information
chaoyaji-cb committed Aug 29, 2024
1 parent f4494c0 commit dc30954
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
34 changes: 22 additions & 12 deletions lib/coinbase/client/models/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#This is the OpenAPI 3.0 specification for the Coinbase Platform APIs, used in conjunction with the Coinbase Platform SDKs.
The version of the OpenAPI document: 0.0.1-alpha
Contact: [email protected]
Generated by: https://openapi-generator.tech
Generator version: 7.7.0
Expand Down Expand Up @@ -36,6 +36,9 @@ class Webhook
# The date and time the webhook was last updated.
attr_accessor :updated_at

# The header that will contain the signature of the webhook payload.
attr_accessor :signature_header

class EnumAttributeValidator
attr_reader :datatype
attr_reader :allowable_values
Expand Down Expand Up @@ -67,7 +70,8 @@ def self.attribute_map
:'event_filters' => :'event_filters',
:'notification_uri' => :'notification_uri',
:'created_at' => :'created_at',
:'updated_at' => :'updated_at'
:'updated_at' => :'updated_at',
:'signature_header' => :'signature_header'
}
end

Expand All @@ -85,14 +89,15 @@ def self.openapi_types
:'event_filters' => :'Array<WebhookEventFilter>',
:'notification_uri' => :'String',
:'created_at' => :'Time',
:'updated_at' => :'Time'
:'updated_at' => :'Time',
:'signature_header' => :'String'
}
end

# List of attributes with nullable: true
def self.openapi_nullable
Set.new([
])
])
end

# Initializes the object
Expand Down Expand Up @@ -139,6 +144,10 @@ def initialize(attributes = {})
if attributes.key?(:'updated_at')
self.updated_at = attributes[:'updated_at']
end

if attributes.key?(:'signature_header')
self.signature_header = attributes[:'signature_header']
end
end

# Show invalid properties with the reasons. Usually used together with valid?
Expand All @@ -161,13 +170,14 @@ def valid?
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
id == o.id &&
network_id == o.network_id &&
event_type == o.event_type &&
event_filters == o.event_filters &&
notification_uri == o.notification_uri &&
created_at == o.created_at &&
updated_at == o.updated_at
id == o.id &&
network_id == o.network_id &&
event_type == o.event_type &&
event_filters == o.event_filters &&
notification_uri == o.notification_uri &&
created_at == o.created_at &&
updated_at == o.updated_at &&
signature_header == o.signature_header
end

# @see the `==` method
Expand All @@ -179,7 +189,7 @@ def eql?(o)
# Calculates hash code according to all attributes.
# @return [Integer] Hash code
def hash
[id, network_id, event_type, event_filters, notification_uri, created_at, updated_at].hash
[id, network_id, event_type, event_filters, notification_uri, created_at, updated_at, signature_header].hash
end

# Builds the object from hash
Expand Down
23 changes: 17 additions & 6 deletions lib/coinbase/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@ class << self
# @param event_filters [Array<Hash>] Filters applied to the events that determine
# which specific events trigger the webhook. Each filter should be a hash that
# can include keys like `contract_address`, `from_address`, or `to_address`.
# @param signature_header [String] The custom header to be used for x-webhook-signature header on callbacks,
# so developers can verify the requests are coming from Coinbase.
# @return [Coinbase::Webhook] A new instance of Webhook.
#
# @example Create a new webhook
# webhook = Coinbase::Webhook.create(
# network_id: :ethereum_mainnet,
# notification_uri: 'https://example.com/callback',
# event_type: 'transaction',
# event_filters: [{ 'contract_address' => '0x...', 'from_address' => '0x...', 'to_address' => '0x...' }]
# event_filters: [{ 'contract_address' => '0x...', 'from_address' => '0x...', 'to_address' => '0x...' }],
# signature_header: 'example_header'
# )
def create(network_id:, notification_uri:, event_type:, event_filters:)
def create(network_id:, notification_uri:, event_type:, event_filters:, signature_header: '')
model = Coinbase.call_api do
webhooks_api.create_webhook(
create_webhook_request: {
network_id: Coinbase.normalize_network(network_id),
notification_uri: notification_uri,
event_type: event_type,
event_filters: event_filters
event_filters: event_filters,
signature_header: signature_header
}
)
end
Expand Down Expand Up @@ -112,6 +116,14 @@ def event_filters
@model.event_filters
end

# Returns the signature header for the webhook. It is used as the value of callback header
# with key 'x-webhook-signature'.
#
# @return [String] The signature header value.
def signature_header
@model.signature_header
end

# Updates the webhook with a new notification URI.
#
# @param notification_uri [String] The new URI for webhook notifications.
Expand All @@ -124,9 +136,7 @@ def update(notification_uri:)
webhooks_api.update_webhook(
id,
update_webhook_request: {
network_id: network_id,
notification_uri: notification_uri,
event_type: event_type,
event_filters: event_filters.map(&:to_hash)
}
)
Expand Down Expand Up @@ -162,7 +172,8 @@ def to_s
network_id: @model.network_id,
event_type: @model.event_type,
notification_uri: @model.notification_uri,
event_filters: @model.event_filters.map(&:to_hash).to_json
event_filters: @model.event_filters.map(&:to_hash).to_json,
signature_header: @model.signature_header
)
end

Expand Down
24 changes: 24 additions & 0 deletions spec/factories/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

FactoryBot.define do
factory :webhook_model, class: 'Coinbase::Client::Webhook' do
id { 'webhook_id' }
network_id { :base_sepolia }
event_type { 'erc20_transfer' }
event_filters { [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }] }
notification_uri { 'https://example.com/notify' }
signature_header { 'example_header' }

trait :updated_uri do
notification_uri { build(:notification_uri) }
end
end

factory :webhook, class: 'Coinbase::Webhook' do
transient do
model { build(:webhook_model) }
end

initialize_with { new(model) }
end
end
22 changes: 2 additions & 20 deletions spec/unit/coinbase/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
describe Coinbase::Webhook do
let(:api_client) { instance_double(Coinbase::Client::ApiClient) }
let(:webhooks_api) { instance_double(Coinbase::Client::WebhooksApi) }
let(:webhook_model) do
Coinbase::Client::Webhook.new(
id: 'webhook_id',
network_id: :base_sepolia,
event_type: 'erc20_transfer',
event_filters: [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }],
notification_uri: 'https://example.com/notify'
)
end
let(:webhook_model) { build(:webhook_model) }

before do
allow(Coinbase.configuration).to receive(:api_client).and_return(api_client)
Expand Down Expand Up @@ -93,15 +85,7 @@
subject(:updated_webhook) { webhook.update(notification_uri: new_notification_uri) }

let(:new_notification_uri) { 'https://newurl.com/notify' }
let(:updated_webhook_model) do
Coinbase::Client::Webhook.new(
id: 'webhook_id',
network_id: :base_sepolia,
event_type: 'erc20_transfer',
event_filters: [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }],
notification_uri: new_notification_uri
)
end
let(:updated_webhook_model) { build(:webhook_model, :updated_uri, notification_uri: new_notification_uri) }
let(:webhook) { described_class.new(webhook_model) }

before do
Expand All @@ -118,9 +102,7 @@
expect(webhooks_api).to have_received(:update_webhook).with(
'webhook_id',
update_webhook_request: {
network_id: :base_sepolia,
notification_uri: new_notification_uri,
event_type: 'erc20_transfer',
event_filters: [{ 'contract_address' => '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }]
}
)
Expand Down

0 comments on commit dc30954

Please sign in to comment.