Parcourir la source

get users assets implementation

kunickyd il y a 3 ans
Parent
commit
d8c510550a
1 fichiers modifiés avec 34 ajouts et 7 suppressions
  1. 34 7
      app.js

+ 34 - 7
app.js

@@ -1,9 +1,16 @@
 const express = require("express");
 const res = require("express/lib/response");
+const http = require("http");
 
 const app = express();
 const port = 3000;
 
+//TODO load these values from setting or env
+
+const irohaApiHost = "localhost";
+const irohaApiPort = 5000;
+const irohaDomain = "test";
+
 app.post("/price", (req, res) => {
     //TODO implement price calculation depending on extent area
     res.send({
@@ -12,13 +19,33 @@ app.post("/price", (req, res) => {
 });
 
 app.get("/users/:userId/assets", (req, res) => {
-    //TODO implement...
-    res.send({
-        assets: [
-            {assetId: "test", balance: 20},
-            {assetId: "doge", balance: 150}
-        ]
-    })
+
+    let output = '';
+
+    let options = {
+        hostname: irohaApiHost,
+        port: irohaApiPort,
+        path: '/accounts/' + req.params.userId + '@' + irohaDomain + '/assets/',
+        method: 'GET',        
+    }
+
+    let irohaReq = http.request(options, irohaRes => {
+
+        irohaRes.on("data", chunk => {
+           output += chunk;           
+        });
+
+        irohaRes.on('end', () => {            
+            res.send(JSON.parse(output));
+        })
+    });
+
+    irohaReq.on("error", error => {
+        console.error(error);
+        res.send(error);
+    });
+
+    irohaReq.end();
 });
 
 app.get("/users/:userId/assets/:assetId", (req, res) => {