Skip to content

Commit

Permalink
Merge pull request #58 from ostdotcom/webhooks
Browse files Browse the repository at this point in the history
Merge wekhooks to develop
  • Loading branch information
kedarchandrayan authored Jun 18, 2019
2 parents 618165b + 9ffc378 commit 935ea24
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[OST JAVA SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-java/tree/v2.1.0)
[OST Ruby SDK v2.2.0](https://github.com/ostdotcom/ost-sdk-ruby/tree/v2.2.0)
---

* Added webhooks module to call webhook management OST APIs.
* Support for verify webhook signature.

[OST Ruby SDK v2.1.0](https://github.com/ostdotcom/ost-sdk-ruby/tree/v2.1.0)
---

* Added base tokens module to V2 API's
Expand Down
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ response = devices_service.get_list(get_params)
```



### Device Managers Module


Expand Down Expand Up @@ -181,7 +180,6 @@ response = device_managers_service.get(get_params)
```



### Sessions Module

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

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


#### Rules Module

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


#### Price Points Module

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


#### Transactions Module

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



### Chains Module

To get information about the auxiliary chain on which the token economy is running, use services
Expand Down Expand Up @@ -441,4 +441,79 @@ Get Base Token Detail:
```ruby
get_params = {}
response = base_tokens_service.get(get_params)
```


### Webhooks Module

To manage webhooks on the OST Platform Interface, use services provided by the Webhooks module. You can
use this service to create new webhooks and manage existing webhooks.

```ruby
webhooks_service = ost_sdk.services.webhooks
```

Create Webhook:

```ruby
create_params = {}
create_params[:topics] = ['transactions/initiate', 'transactions/success']
create_params[:url] = 'https://testingWebhooks.com'
# create_params[:status] = 'inactive'
response = webhooks_service.create(create_params)
```

Update Webhook:

```ruby
update_params = {}
update_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
update_params[:topics] = ['transactions/initiate', 'transactions/success', 'transactions/failure']
update_params[:status] = 'inactive'
response = webhooks_service.update(update_params)
```

Get Webhook:

```ruby
get_params = {}
get_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
response = webhooks_service.get(get_params)
```

Get Webhook List:

```ruby
get_params = {}
# get_params[:limit] = 1
# get_params[:pagination_identifier] = 'eyJwYWdlIjoyLCJsaW1pdCI6MX0='
response = webhooks_service.get_list(get_params)
```

Delete Webhook:

```ruby
delete_params = {}
delete_params[:webhook_id] = 'b036aff5-75a3-466d-a20c-a956b198fd14'
response = webhooks_service.delete(delete_params)
```

Verify webhook request signature:

```ruby
signature_params = {}
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"}]}}}
signature_params[:stringified_data] = webhook_event_data.to_json

# Get webhoook version from webhook events data.
signature_params[:version] = "v2"

# Get ost-timestamp from the response received in event.
signature_params[:request_timestamp] = '1559902637'

# Get signature from the response received in event.
signature_params[:signature] = '2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824'

signature_params[:webhook_secret] = 'mySecret'
response = webhooks_service.verify_signature(signature_params)
```
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.2.0
1 change: 1 addition & 0 deletions lib/ost-sdk-ruby/saas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative 'saas/transactions'
require_relative 'saas/users'
require_relative 'saas/base_tokens'
require_relative 'saas/webhooks'

module OSTSdk

Expand Down
12 changes: 12 additions & 0 deletions lib/ost-sdk-ruby/saas/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def get_user_id!(params)
get_value_for_key!(params, "user_id")
end

# Get webhook_id key from params hash and delete it
#
# Arguments:
# params: (Hash)
#
# Returns:
# user_id: (String)
#
def get_webhook_id!(params)
get_value_for_key!(params, "webhook_id")
end

# Get chain_id key from params hash and delete it
#
# Arguments:
Expand Down
3 changes: 2 additions & 1 deletion lib/ost-sdk-ruby/saas/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Saas
class Manifest

attr_reader :balance, :chains, :device_managers, :devices, :price_points, :recovery_owners, :rules,
:sessions, :tokens, :transactions, :users, :base_tokens
:sessions, :tokens, :transactions, :users, :base_tokens, :webhooks

# Initialize
#
Expand All @@ -31,6 +31,7 @@ def initialize(params)
@transactions = OSTSdk::Saas::Transactions.new(params)
@users = OSTSdk::Saas::Users.new(params)
@base_tokens = OSTSdk::Saas::BaseTokens.new(params)
@webhooks = OSTSdk::Saas::Webhooks.new(params)

end

Expand Down
90 changes: 90 additions & 0 deletions lib/ost-sdk-ruby/saas/webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module OSTSdk

module Saas

class Webhooks < OSTSdk::Saas::Base

# Initialize
#
# Arguments:
# api_base_url: (String)
# api_key: (String)
# api_secret: (String)
# api_spec: (Boolean)
# config: (Hash)
#
def initialize(params)
super
@url_prefix = '/webhooks'
end

# Create a Webhook
#
# Returns:
# response: (Hash)
#
def create(params = {})
http_helper.send_post_request("#{@url_prefix}/", params)
end

# Update a Webhook
#
# Returns:
# response: (Hash)
#
def update(params = {})
http_helper.send_post_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
end

# Get webhook details
#
# Returns:
# response: (Hash)
#
def get(params = {})
http_helper.send_get_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
end

# List Webhooks
#
# Returns:
# response: (Hash)
#
def get_list(params = {})
http_helper.send_get_request("#{@url_prefix}/", params)
end

# Delete a Webhook
#
# Returns:
# response: (Hash)
#
def delete(params = {})
http_helper.send_delete_request("#{@url_prefix}/#{get_webhook_id!(params)}", params)
end

# Verify webhook request signature
#
# Returns:
# response: (Boolean)
#
def verify_signature(params = {})
version = params[:version]
webhook_secret = params[:webhook_secret]
stringified_data = params[:stringified_data]
request_timestamp = params[:request_timestamp]
signature = params[:signature]

signature_params = "#{request_timestamp}.#{version}.#{stringified_data}"
digest = OpenSSL::Digest.new('sha256')
signature_to_be_verified = OpenSSL::HMAC.hexdigest(digest, webhook_secret, signature_params)

signature == signature_to_be_verified

end

end

end

end
72 changes: 72 additions & 0 deletions lib/ost-sdk-ruby/test/webhooks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require_relative "../../../lib/ost-sdk-ruby/util"
require_relative "../../../lib/ost-sdk-ruby/saas"
require "test/unit"
require_relative "../../../lib/config"

class UsersTest < Test::Unit::TestCase

def webhooks_service
@webhooks_service ||= Config::OST_SDK.services.webhooks
end

def test_webhooks_create
result = webhooks_service.create({
topics: ['transactions/initiate', 'transactions/success'],
url: 'https://testingWebhooks.com'
})
puts "result=>#{result}" unless result["success"]
webhook_id = result["data"]["webhook"]["id"]
webhooks_get(webhook_id)
webhooks_get_all
webhooks_update(webhook_id)
webhooks_delete(webhook_id)
assert_equal(result["success"], true)
end

def webhooks_get(webhook_id)
result = webhooks_service.get({
webhook_id: webhook_id
})
puts "result=>#{result}" unless result["success"]
assert_equal(result["success"], true)
end

def webhooks_get_all
result = webhooks_service.get_list()
puts "result=>#{result}" unless result["success"]
assert_equal(result["success"], true)
end

def webhooks_update(webhook_id)
result = webhooks_service.update({
webhook_id: webhook_id,
topics: ['transactions/initiate', 'transactions/success', 'transactions/failure'],
status: 'inactive'
})
puts "result=>#{result}" unless result["success"]
assert_equal(result["success"], true)
end

def webhooks_delete(webhook_id)
result = webhooks_service.delete({
webhook_id: webhook_id,
})
puts "result=>#{result}" unless result["success"]
assert_equal(result["success"], true)
end

def test_webhooks_verify_signature
data = {}
data[:hello] = 'hello'
result = webhooks_service.verify_signature({
version: "2",
webhook_secret: "mySecret",
request_timestamp: "1559902637",
signature: "2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824",
stringified_data: data.to_json
})
puts "result=>#{result}" unless result
assert_equal(result, true)
end

end
27 changes: 27 additions & 0 deletions lib/ost-sdk-ruby/util/http_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ def send_get_request(endpoint, request_params)
end
end

# Send DELETE requests
#
# Arguments:
# end_point: (String)
# request_params: (Hash)
#
# Returns:
# response: (Hash)
#
def send_delete_request(endpoint, request_params)
perform_and_handle_exceptions('u_hh_3', 'DELETE request Failed') do
escaped_query_string = get_query_string(endpoint, request_params)
raw_url = get_api_url(endpoint) + "?#{escaped_query_string}"
uri = URI(raw_url)
http = setup_request(uri)
if @api_spec
return {request_uri: uri.to_s.split("?")[0], request_type: 'DELETE', request_params: escaped_query_string}
else
result = {}
Timeout.timeout(@timeout) do
result = http.delete(uri)
end
return format_response(result)
end
end
end

# Generate a signature for test case. It only creates a signature for a given Hash
#
# Arguments:
Expand Down
2 changes: 1 addition & 1 deletion lib/ost-sdk-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module OSTSdk

VERSION = "2.1.0"
VERSION = "2.2.0"

end

0 comments on commit 935ea24

Please sign in to comment.