BETTERZON-116: Adding API endpoint for searching a new product (#62)

This commit is contained in:
Patrick 2021-05-20 20:59:50 +02:00 committed by GitHub
parent 13f1257740
commit 1e6d99a713
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -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.'}));
}
});
// 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.'}));
}
});

View File

@ -17,6 +17,7 @@ const pool = mariadb.createPool({
import {Product} from './product.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;
};
/**
* 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;
};