mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-11-22 14:23:57 +00:00
BETTERZON-116: Adding API endpoint for searching a new product (#62)
This commit is contained in:
parent
13f1257740
commit
1e6d99a713
|
@ -106,3 +106,26 @@ productsRouter.get('/vendor/:id', async (req: Request, res: Response) => {
|
||||||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST products/
|
||||||
|
productsRouter.post('/', async (req: Request, res: Response) => {
|
||||||
|
const asin: string = req.body.asin;
|
||||||
|
|
||||||
|
if (!asin) {
|
||||||
|
res.status(400).send('Missing parameters.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result: boolean = await ProductService.addNewProduct(asin);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
res.sendStatus(201);
|
||||||
|
} else {
|
||||||
|
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error handling a request: ' + e.message);
|
||||||
|
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -17,6 +17,7 @@ const pool = mariadb.createPool({
|
||||||
|
|
||||||
import {Product} from './product.interface';
|
import {Product} from './product.interface';
|
||||||
import {Products} from './products.interface';
|
import {Products} from './products.interface';
|
||||||
|
import * as http from 'http';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,3 +198,32 @@ export const findByVendor = async (id: number): Promise<Products> => {
|
||||||
|
|
||||||
return prodRows;
|
return prodRows;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes a callout to a crawler instance to search for the requested product
|
||||||
|
* @param asin The amazon asin of the product to look for
|
||||||
|
*/
|
||||||
|
export const addNewProduct = async (asin: string): Promise<boolean> => {
|
||||||
|
try {
|
||||||
|
let options = {
|
||||||
|
host: 'crawl.p4ddy.com',
|
||||||
|
path: '/searchNew',
|
||||||
|
port: '443',
|
||||||
|
method: 'POST'
|
||||||
|
};
|
||||||
|
|
||||||
|
let req = http.request(options, res => {
|
||||||
|
return res.statusCode === 202;
|
||||||
|
});
|
||||||
|
req.write(JSON.stringify({
|
||||||
|
asin: asin,
|
||||||
|
key: process.env.CRAWLER_ACCESS_KEY
|
||||||
|
}));
|
||||||
|
req.end();
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user