Bladeren bron

get single asset info implementation

kunickyd 3 jaren geleden
bovenliggende
commit
cb203cd6ed
2 gewijzigde bestanden met toevoegingen van 37 en 37 verwijderingen
  1. 34 36
      app.js
  2. 3 1
      package.json

+ 34 - 36
app.js

@@ -1,13 +1,12 @@
-const express = require("express");
-const res = require("express/lib/response");
-const http = require("http");
+import fetch from "node-fetch"
+import express from "express"
 
 const app = express();
 const port = 3000;
 
 //TODO load these values from setting or env
 
-const irohaApiHost = "localhost";
+const irohaApiHost = "http://localhost";
 const irohaApiPort = 5000;
 const irohaDomain = "test";
 
@@ -18,42 +17,36 @@ app.post("/price", (req, res) => {
     });
 });
 
-app.get("/users/:userId/assets", (req, res) => {
-
-    let output = '';
-
-    let options = {
-        hostname: irohaApiHost,
-        port: irohaApiPort,
-        path: '/accounts/' + req.params.userId + '@' + irohaDomain + '/assets/',
-        method: 'GET',        
+app.get("/users/:userId/assets", async (req, res) => {
+    
+    try{        
+        res.send(await getUsersAssets(req.params.userId));
     }
-
-    let irohaReq = http.request(options, irohaRes => {
-
-        irohaRes.on("data", chunk => {
-           output += chunk;           
-        });
-
-        irohaRes.on('end', () => {            
-            res.send(JSON.parse(output));
-        })
-    });
-
-    irohaReq.on("error", error => {
+    catch(error){
         console.error(error);
+        res.status(500);
         res.send(error);
-    });
-
-    irohaReq.end();
+    }
 });
 
-app.get("/users/:userId/assets/:assetId", (req, res) => {
-    //TODO implement...
-    res.send({
-        assetId: req.params.assetId,
-        balance: 150
-        });
+app.get("/users/:userId/assets/:assetId", async (req, res) => {
+
+    try{
+        let response = await getUsersAssets(req.params.userId);
+        
+        let i = 0;
+        for(i; i < response.assets.length; i++){            
+            if(response.assets[i].assetId.startsWith(req.params.assetId)){
+                res.send(response.assets[i]);
+                return;
+            }
+        }
+    }
+    catch(error){
+        console.error(error);
+        res.status(500);
+        res.send(error);
+    }
 });
 
 app.post("/buy", (req, res) => {
@@ -63,4 +56,9 @@ app.post("/buy", (req, res) => {
 
 app.listen(port, () => {
     console.log(`Listening at http://localhost:${port}`)
-  })
+});
+
+async function getUsersAssets(userId){    
+    let response = await fetch(irohaApiHost + ':' + irohaApiPort + '/accounts/' + userId + '@' + irohaDomain + '/assets/');
+    return await response.json();
+}

+ 3 - 1
package.json

@@ -3,6 +3,7 @@
   "version": "1.0.0",
   "description": "",
   "main": "app.js",
+  "type": "module",
   "scripts": {
     "dev": "nodemon app.js",
     "start": "node app.js"
@@ -10,7 +11,8 @@
   "author": "Daniel Kunický",
   "license": "ISC",
   "dependencies": {
-    "express": "^4.17.2"
+    "express": "^4.17.2",
+    "node-fetch": "^3.1.0"
   },
   "devDependencies": {
     "nodemon": "^2.0.15"