OSDN Git Service

feat query SDK
authorxuhongxin <xuhongxin@luojilab.com>
Sun, 4 Nov 2018 06:12:36 +0000 (14:12 +0800)
committerxuhongxin <xuhongxin@luojilab.com>
Sun, 4 Nov 2018 06:12:36 +0000 (14:12 +0800)
package.json
src/http.js
src/index.js
src/sdk/query.js [new file with mode: 0644]

index b2ef3ba..01426f5 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "bytom-js-sdk",
-  "version": "1.3.6",
+  "version": "1.3.7",
   "description": "Bytom JS SDK",
   "main": "src/index.js",
   "keywords": [
index 1884145..2cac9eb 100644 (file)
@@ -4,10 +4,10 @@ const basePath = 'api/v1/btm/';
 
 export function serverHttp(host) {
     this.host = host;
-    this.request = function(path, body, net) {
+    this.request = function(path, body, net, method) {
         var config = {
             url: `${this.host[net]}${basePath}${path}`,
-            method: 'POST',
+            method: method ? method : 'POST' ,
             headers: {
                 Accept: 'application/json',
             },
index 1e2cae6..21f081f 100644 (file)
@@ -5,6 +5,7 @@ import keysSDK from './sdk/keys.js';
 import accountsSDK from './sdk/accounts.js';
 import transactionSDK from './sdk/transaction.js';
 import walletSDK from './sdk/wallet.js';
+import querySDK from './sdk/query.js';
 import {serverHttp, http} from './http.js';
 
 function Bytom(serverHost, wasmPath, baseURL, token) {
@@ -27,6 +28,7 @@ function Bytom(serverHost, wasmPath, baseURL, token) {
     this.sdk.accounts = new accountsSDK(this);
     this.sdk.transaction = new transactionSDK(this);
     this.sdk.wallet = new walletSDK(this);
+    this.sdk.query = new querySDK(this);
 }
 
 
diff --git a/src/sdk/query.js b/src/sdk/query.js
new file mode 100644 (file)
index 0000000..d274215
--- /dev/null
@@ -0,0 +1,39 @@
+import { handleAxiosError } from '../utils/http';
+
+function querySDK(bytom) {
+    this.bytom =bytom; 
+    this.http = bytom.serverHttp;
+}
+/**
+ * Query the price of an asset on a given blockchain. asset_id is a hexdecimal string.
+ *
+ * @param {String} asset_id
+ */
+querySDK.prototype.asset = function(asset_id) {
+    let net = this.bytom.net;
+    let retPromise = new Promise((resolve, reject) => {
+        this.http.request('q/asset?asset_id=' + asset_id, null, net, 'GET').then(resp => {
+            resolve(resp.data);
+        }).catch(err => {
+            reject(handleAxiosError(err));
+        });
+    });
+    return retPromise;
+};
+
+/**
+ * Query the current height of a blockchain.
+ */
+querySDK.prototype.getblockcount = function() {
+    let net = this.bytom.net;
+    let retPromise = new Promise((resolve, reject) => {
+        this.http.request('q/getblockcount', null, net, 'GET').then(resp => {
+            resolve(resp.data);
+        }).catch(err => {
+            reject(handleAxiosError(err));
+        });
+    });
+    return retPromise;
+};
+
+export default querySDK;
\ No newline at end of file