|
@@ -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 app = express();
|
|
|
const port = 3000;
|
|
const port = 3000;
|
|
|
|
|
|
|
|
//TODO load these values from setting or env
|
|
//TODO load these values from setting or env
|
|
|
|
|
|
|
|
-const irohaApiHost = "localhost";
|
|
|
|
|
|
|
+const irohaApiHost = "http://localhost";
|
|
|
const irohaApiPort = 5000;
|
|
const irohaApiPort = 5000;
|
|
|
const irohaDomain = "test";
|
|
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);
|
|
console.error(error);
|
|
|
|
|
+ res.status(500);
|
|
|
res.send(error);
|
|
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) => {
|
|
app.post("/buy", (req, res) => {
|
|
@@ -63,4 +56,9 @@ app.post("/buy", (req, res) => {
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
app.listen(port, () => {
|
|
|
console.log(`Listening at http://localhost:${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();
|
|
|
|
|
+}
|