Skip to content

Commit 935ea24

Browse files
Merge pull request #58 from ostdotcom/webhooks
Merge wekhooks to develop
2 parents 618165b + 9ffc378 commit 935ea24

File tree

10 files changed

+291
-7
lines changed

10 files changed

+291
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
[OST JAVA SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-java/tree/v2.1.0)
1+
[OST Ruby SDK v2.2.0](https://github.com/ostdotcom/ost-sdk-ruby/tree/v2.2.0)
2+
---
3+
4+
* Added webhooks module to call webhook management OST APIs.
5+
* Support for verify webhook signature.
6+
7+
[OST Ruby SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-ruby/tree/v2.1.0)
28
---
39

410
* Added base tokens module to V2 API's

README.md

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ response = devices_service.get_list(get_params)
149149
```
150150

151151

152-
153152
### Device Managers Module
154153

155154

@@ -181,7 +180,6 @@ response = device_managers_service.get(get_params)
181180
```
182181

183182

184-
185183
### Sessions Module
186184

187185
In order to create a more seamless user experience, so that users don't have to
@@ -224,6 +222,7 @@ response = sessions_service.get_list(get_params)
224222

225223
For executing transactions, you need to understand the 4 modules described below.
226224

225+
227226
#### Rules Module
228227

229228
When executing a token transfer, a user's TokenHolder contract
@@ -247,6 +246,7 @@ get_params = {}
247246
response = rules_service.get_list(get_params)
248247
```
249248

249+
250250
#### Price Points Module
251251

252252
To know the value tokens (such as OST, USDC) price point in pay currency and when it was last updated,
@@ -264,6 +264,7 @@ get_params[:chain_id] = 2000
264264
response = price_points_service.get(get_params)
265265
```
266266

267+
267268
#### Transactions Module
268269

269270
After reviewing the rules information received using services in the Rules
@@ -407,7 +408,6 @@ response = tokens_service.get(get_params)
407408
```
408409

409410

410-
411411
### Chains Module
412412

413413
To get information about the auxiliary chain on which the token economy is running, use services
@@ -441,4 +441,79 @@ Get Base Token Detail:
441441
```ruby
442442
get_params = {}
443443
response = base_tokens_service.get(get_params)
444+
```
445+
446+
447+
### Webhooks Module
448+
449+
To manage webhooks on the OST Platform Interface, use services provided by the Webhooks module. You can
450+
use this service to create new webhooks and manage existing webhooks.
451+
452+
```ruby
453+
webhooks_service = ost_sdk.services.webhooks
454+
```
455+
456+
Create Webhook:
457+
458+
```ruby
459+
create_params = {}
460+
create_params[:topics] = ['transactions/initiate', 'transactions/success']
461+
create_params[:url] = 'https://testingWebhooks.com'
462+
# create_params[:status] = 'inactive'
463+
response = webhooks_service.create(create_params)
464+
```
465+
466+
Update Webhook:
467+
468+
```ruby
469+
update_params = {}
470+
update_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
471+
update_params[:topics] = ['transactions/initiate', 'transactions/success', 'transactions/failure']
472+
update_params[:status] = 'inactive'
473+
response = webhooks_service.update(update_params)
474+
```
475+
476+
Get Webhook:
477+
478+
```ruby
479+
get_params = {}
480+
get_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
481+
response = webhooks_service.get(get_params)
482+
```
483+
484+
Get Webhook List:
485+
486+
```ruby
487+
get_params = {}
488+
# get_params[:limit] = 1
489+
# get_params[:pagination_identifier] = 'eyJwYWdlIjoyLCJsaW1pdCI6MX0='
490+
response = webhooks_service.get_list(get_params)
491+
```
492+
493+
Delete Webhook:
494+
495+
```ruby
496+
delete_params = {}
497+
delete_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
498+
response = webhooks_service.delete(delete_params)
499+
```
500+
501+
Verify webhook request signature:
502+
503+
```ruby
504+
signature_params = {}
505+
webhook_event_data = {"id":"54e3cd1c-afd7-4dcf-9c78-137c56a53582","topic":"transactions/success","created_at":1560838772,"webhook_id":"0823a4ea-5d87-44cf-8ca8-1e5a31bf8e46","version":"v2","data":{"result_type":"transaction","transaction":{"id":"ddebe817-b94f-4b51-9227-f543fae4715a","transaction_hash":"0x7ee737db22b58dc4da3f4ea4830ca709b388d84f31e77106cb79ee09fc6448f9","from":"0x69a581096dbddf6d1e0fff7ebc1254bb7a2647c6","to":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","nonce":3,"value":"0","gas_price":"1000000000","gas_used":120558,"transaction_fee":"120558000000000","block_confirmation":24,"status":"SUCCESS","updated_timestamp":1560838699,"block_timestamp":1560838698,"block_number":1554246,"rule_name":"Pricer","meta_property":{},"transfers":[{"from":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","from_user_id":"acfdea7d-278e-4ffc-aacb-4a21398a280c","to":"0x0a754aaab96d634337aac6556312de396a0ca46a","to_user_id":"7bc8e0bd-6761-4604-8f8e-e33f86f81309","amount":"112325386","kind":"transfer"}]}}}
506+
signature_params[:stringified_data] = webhook_event_data.to_json
507+
508+
# Get webhoook version from webhook events data.
509+
signature_params[:version] = "v2"
510+
511+
# Get ost-timestamp from the response received in event.
512+
signature_params[:request_timestamp] = '1559902637'
513+
514+
# Get signature from the response received in event.
515+
signature_params[:signature] = '2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824'
516+
517+
signature_params[:webhook_secret] = 'mySecret'
518+
response = webhooks_service.verify_signature(signature_params)
444519
```

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

lib/ost-sdk-ruby/saas.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require_relative 'saas/transactions'
1414
require_relative 'saas/users'
1515
require_relative 'saas/base_tokens'
16+
require_relative 'saas/webhooks'
1617

1718
module OSTSdk
1819

lib/ost-sdk-ruby/saas/base.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ def get_user_id!(params)
5454
get_value_for_key!(params, "user_id")
5555
end
5656

57+
# Get webhook_id key from params hash and delete it
58+
#
59+
# Arguments:
60+
# params: (Hash)
61+
#
62+
# Returns:
63+
# user_id: (String)
64+
#
65+
def get_webhook_id!(params)
66+
get_value_for_key!(params, "webhook_id")
67+
end
68+
5769
# Get chain_id key from params hash and delete it
5870
#
5971
# Arguments:

lib/ost-sdk-ruby/saas/manifest.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Saas
55
class Manifest
66

77
attr_reader :balance, :chains, :device_managers, :devices, :price_points, :recovery_owners, :rules,
8-
:sessions, :tokens, :transactions, :users, :base_tokens
8+
:sessions, :tokens, :transactions, :users, :base_tokens, :webhooks
99

1010
# Initialize
1111
#
@@ -31,6 +31,7 @@ def initialize(params)
3131
@transactions = OSTSdk::Saas::Transactions.new(params)
3232
@users = OSTSdk::Saas::Users.new(params)
3333
@base_tokens = OSTSdk::Saas::BaseTokens.new(params)
34+
@webhooks = OSTSdk::Saas::Webhooks.new(params)
3435

3536
end
3637

lib/ost-sdk-ruby/saas/webhooks.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module OSTSdk
2+
3+
module Saas
4+
5+
class Webhooks < OSTSdk::Saas::Base
6+
7+
# Initialize
8+
#
9+
# Arguments:
10+
# api_base_url: (String)
11+
# api_key: (String)
12+
# api_secret: (String)
13+
# api_spec: (Boolean)
14+
# config: (Hash)
15+
#
16+
def initialize(params)
17+
super
18+
@url_prefix = '/webhooks'
19+
end
20+
21+
# Create a Webhook
22+
#
23+
# Returns:
24+
# response: (Hash)
25+
#
26+
def create(params = {})
27+
http_helper.send_post_request("#{@url_prefix}/", params)
28+
end
29+
30+
# Update a Webhook
31+
#
32+
# Returns:
33+
# response: (Hash)
34+
#
35+
def update(params = {})
36+
http_helper.send_post_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
37+
end
38+
39+
# Get webhook details
40+
#
41+
# Returns:
42+
# response: (Hash)
43+
#
44+
def get(params = {})
45+
http_helper.send_get_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
46+
end
47+
48+
# List Webhooks
49+
#
50+
# Returns:
51+
# response: (Hash)
52+
#
53+
def get_list(params = {})
54+
http_helper.send_get_request("#{@url_prefix}/", params)
55+
end
56+
57+
# Delete a Webhook
58+
#
59+
# Returns:
60+
# response: (Hash)
61+
#
62+
def delete(params = {})
63+
http_helper.send_delete_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
64+
end
65+
66+
# Verify webhook request signature
67+
#
68+
# Returns:
69+
# response: (Boolean)
70+
#
71+
def verify_signature(params = {})
72+
version = params[:version]
73+
webhook_secret = params[:webhook_secret]
74+
stringified_data = params[:stringified_data]
75+
request_timestamp = params[:request_timestamp]
76+
signature = params[:signature]
77+
78+
signature_params = "#{request_timestamp}.#{version}.#{stringified_data}"
79+
digest = OpenSSL::Digest.new('sha256')
80+
signature_to_be_verified = OpenSSL::HMAC.hexdigest(digest, webhook_secret, signature_params)
81+
82+
signature == signature_to_be_verified
83+
84+
end
85+
86+
end
87+
88+
end
89+
90+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require_relative "../../../lib/ost-sdk-ruby/util"
2+
require_relative "../../../lib/ost-sdk-ruby/saas"
3+
require "test/unit"
4+
require_relative "../../../lib/config"
5+
6+
class UsersTest < Test::Unit::TestCase
7+
8+
def webhooks_service
9+
@webhooks_service ||= Config::OST_SDK.services.webhooks
10+
end
11+
12+
def test_webhooks_create
13+
result = webhooks_service.create({
14+
topics: ['transactions/initiate', 'transactions/success'],
15+
url: 'https://testingWebhooks.com'
16+
})
17+
puts "result=>#{result}" unless result["success"]
18+
webhook_id = result["data"]["webhook"]["id"]
19+
webhooks_get(webhook_id)
20+
webhooks_get_all
21+
webhooks_update(webhook_id)
22+
webhooks_delete(webhook_id)
23+
assert_equal(result["success"], true)
24+
end
25+
26+
def webhooks_get(webhook_id)
27+
result = webhooks_service.get({
28+
webhook_id: webhook_id
29+
})
30+
puts "result=>#{result}" unless result["success"]
31+
assert_equal(result["success"], true)
32+
end
33+
34+
def webhooks_get_all
35+
result = webhooks_service.get_list()
36+
puts "result=>#{result}" unless result["success"]
37+
assert_equal(result["success"], true)
38+
end
39+
40+
def webhooks_update(webhook_id)
41+
result = webhooks_service.update({
42+
webhook_id: webhook_id,
43+
topics: ['transactions/initiate', 'transactions/success', 'transactions/failure'],
44+
status: 'inactive'
45+
})
46+
puts "result=>#{result}" unless result["success"]
47+
assert_equal(result["success"], true)
48+
end
49+
50+
def webhooks_delete(webhook_id)
51+
result = webhooks_service.delete({
52+
webhook_id: webhook_id,
53+
})
54+
puts "result=>#{result}" unless result["success"]
55+
assert_equal(result["success"], true)
56+
end
57+
58+
def test_webhooks_verify_signature
59+
data = {}
60+
data[:hello] = 'hello'
61+
result = webhooks_service.verify_signature({
62+
version: "2",
63+
webhook_secret: "mySecret",
64+
request_timestamp: "1559902637",
65+
signature: "2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824",
66+
stringified_data: data.to_json
67+
})
68+
puts "result=>#{result}" unless result
69+
assert_equal(result, true)
70+
end
71+
72+
end

lib/ost-sdk-ruby/util/http_helper.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ def send_get_request(endpoint, request_params)
8181
end
8282
end
8383

84+
# Send DELETE requests
85+
#
86+
# Arguments:
87+
# end_point: (String)
88+
# request_params: (Hash)
89+
#
90+
# Returns:
91+
# response: (Hash)
92+
#
93+
def send_delete_request(endpoint, request_params)
94+
perform_and_handle_exceptions('u_hh_3', 'DELETE request Failed') do
95+
escaped_query_string = get_query_string(endpoint, request_params)
96+
raw_url = get_api_url(endpoint) + "?#{escaped_query_string}"
97+
uri = URI(raw_url)
98+
http = setup_request(uri)
99+
if @api_spec
100+
return {request_uri: uri.to_s.split("?")[0], request_type: 'DELETE', request_params: escaped_query_string}
101+
else
102+
result = {}
103+
Timeout.timeout(@timeout) do
104+
result = http.delete(uri)
105+
end
106+
return format_response(result)
107+
end
108+
end
109+
end
110+
84111
# Generate a signature for test case. It only creates a signature for a given Hash
85112
#
86113
# Arguments:

lib/ost-sdk-ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module OSTSdk
22

3-
VERSION = "2.1.0"
3+
VERSION = "2.2.0"
44

55
end

0 commit comments

Comments
 (0)