Skip to content

Commit

Permalink
Merge pull request #44 from ostdotcom/webhooks
Browse files Browse the repository at this point in the history
Merging webhooks endpoint support to develop
  • Loading branch information
kedarchandrayan authored Jun 18, 2019
2 parents c988bd1 + 30d21fd commit 80382e1
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 25 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 Javascript SDK v2.2.0](https://github.com/ostdotcom/ost-sdk-java/tree/v2.2.0)
---

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

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

* Added base tokens module to V2 API's
Expand Down
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,68 @@ Get Token Detail:
```node.js
baseTokensService.get({}).then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });
```

### 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.

```node.js
webhooksService = ostObj.services.webhooks;
```

Create Webhook:

```node.js
topicParams = ['transactions/initiate','transactions/success'];
webhooksService.create({topics: topicParams , url:"https://www.testingWebhooks.com", status:"active"}).then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });
```

Update Webhook:

```node.js
topicParams = ['transactions/initiate','transactions/success','transactions/failure'];
webhooksService.update({webhook_id: 'a743ab9a-2555-409f-aae4-f30c84071c56', topics:topicParams, status:"active"}).then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });

```

Get Webhook:

```node.js
webhooksService.get({webhook_id:'842276e3-f520-4a70-94dc-ad409b70c481'}).then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });
```

Get Webhook List:

```node.js
webhooksService.getList({
//limit:1,
//pagination_identifier:"eyJwYWdlIjoyLCJsaW1pdCI6MX0="})
.then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });
```
Delete Webhook:
```node.js
webhooksService.deleteWebhook({webhook_id:'a743ab9a-2555-409f-aae4-f30c84071c56'}).then(function(res) { console.log(JSON.stringify(res)); }).catch(function(err) { console.log(JSON.stringify(err)); });
```
Verify webhook request signature:
```node.js
webhookEventData = JSON.stringify({"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"}]}}});

// Get webhoook version from webhook events data.
version = "v2";

// Get ost-timestamp from the response received in event.
requestTimestamp = '1559902637';

// Get signature from the response received in event.
signature = '2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824';

stringifiedData = webhookEventData;
webhookSecret = 'mySecret';
let resp = webhooksService.verifySignature(version, stringifiedData,requestTimestamp, signature, webhookSecret);
console.log(resp);
```
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
31 changes: 22 additions & 9 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ RequestKlass.prototype = {
return oThis._send('POST', resource, queryParams);
},

/**
* Send delete request
*
* @param {string} resource - API Resource
* @param {object} queryParams - resource query parameters
*
* @public
*/
deleteRequest: function (resource, queryParams) {
const oThis = this;
return oThis._send('DELETE', resource, queryParams);
},

/**
* Get formatted query params
*
Expand Down Expand Up @@ -153,7 +166,6 @@ RequestKlass.prototype = {
* @param {string} requestType - API request type
* @param {string} resource - API Resource
* @param {object} queryParams - resource query parameters
*
* @private
*/
_send: function (requestType, resource, queryParams) {
Expand All @@ -176,6 +188,10 @@ RequestKlass.prototype = {
options.path = options.path + "?" + requestData;
}

if (requestType === 'DELETE' && validate.isPresent(requestData)) {
options.path = options.path + "?" + requestData;
}

if (DEBUG) {
console.log("------------------------------");
console.log("request OPTIONS \n", JSON.stringify(options));
Expand Down Expand Up @@ -231,20 +247,20 @@ RequestKlass.prototype = {

});

//write data to server
// Write data to server
if (requestType === 'POST' && validate.isPresent(requestData)) {
request.write(requestData);
}

request.end();
});
},

/**
* Parse response
* Parse response.
*
* @param {string} responseData - Response data
* @param {object} response - Response object
*
* @private
*/
_parseResponse: function (responseData, response) {
Expand Down Expand Up @@ -291,24 +307,21 @@ RequestKlass.prototype = {
},

/**
* Sign query params for testing
* Sign query params for testing.
*
* @param {string} resource - url endpoint
* @param {object} queryParams - query params
* @param {object} _apiCredentials - credentail
*
*
* @private
*/
signQueryParamsTest: function (resource, queryParams, _apiCredentials) {
return signQueryParams(resource, queryParams, _apiCredentials)
},

/**
* format query params
* Format query params.
*
* @param {object} queryParams - query params
*
* @private
*/
formatQueryParams: function (queryParams) {
Expand Down
28 changes: 20 additions & 8 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"use strict";

/**
* Validate parameters
* Module to validate parameters.
*
* @module lib/validate
*/
const rootPrefix = ".."
;

/**
* Validate parameters constructor
Expand All @@ -30,10 +27,9 @@ ValidateKlass.prototype = {
},

/**
* Check if parameter is valid
* Check if parameter is valid.
*
* @param {string} param - parameter value
*
* @public
*/
isValid: function (param) {
Expand All @@ -43,7 +39,7 @@ ValidateKlass.prototype = {
},

/**
* Get id from params
* Get id from params.
*
* @param {object} params
*/
Expand All @@ -60,7 +56,7 @@ ValidateKlass.prototype = {


/**
* Get user id from params
* Get user id from params.
*
* @param {object} params
*/
Expand All @@ -75,6 +71,22 @@ ValidateKlass.prototype = {
}
},


/**
* Get webhook id from params.
*
* @param {object} params
*/
getWebhookId: function (params) {
const oThis = this;
if (oThis.isValid(params.webhook_id)) {
let webhookId = params.webhook_id;
return webhookId
} else {
throw new Error('webhook_id missing or invalid in request params');
}
},

/**
* Get chain id from params
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ostdotcom/ost-sdk-js",
"version": "2.1.0",
"version": "2.2.0",
"description": "OST Platform SDK for JavaScript.",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion services/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const rootPrefix = ".."
, rulesKlass = require(rootPrefix + '/services/rules')
, transactionsKlass = require(rootPrefix + '/services/transactions')
, baseTokensKlass = require(rootPrefix + '/services/base_tokens')
, webhooksKlass = require(rootPrefix + '/services/webhooks')
;

// hide request object
Expand Down Expand Up @@ -49,6 +50,7 @@ const manifest = function (params) {
oThis.rules = new rulesKlass(_requestObj);
oThis.transactions = new transactionsKlass(_requestObj);
oThis.base_tokens = new baseTokensKlass(_requestObj);
oThis.webhooks = new webhooksKlass(_requestObj);

return oThis;
};
Expand Down Expand Up @@ -77,7 +79,9 @@ manifest.prototype = {

transactions: null,

base_tokens: null
base_tokens: null,

webhooks: null
};

module.exports = manifest;
Loading

0 comments on commit 80382e1

Please sign in to comment.