From a43611f664730e74d19547896e2fcd9705c820d3 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 12 Nov 2018 12:50:12 +0100 Subject: [PATCH] POST to GET only | accept: application/json but HTML by default --- .gitignore | 3 ++- cnf/api_keys.json | 6 +++++ src/mixer.ts | 67 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 cnf/api_keys.json diff --git a/.gitignore b/.gitignore index a0c842c..b52ee89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /node_modules /package-lock.json /dist -*.js \ No newline at end of file +*.js +types/request\.d\.ts diff --git a/cnf/api_keys.json b/cnf/api_keys.json new file mode 100644 index 0000000..1a1cdcd --- /dev/null +++ b/cnf/api_keys.json @@ -0,0 +1,6 @@ +{ + "carrefour": { + "app_id": "1e70cf82-6062-4067-8b6c-e5f34c0643b7", + "app_secret": "eO8cF7qL5hX2wG0bT5hY6aV2kM7oE8aT2hT4iE8iP5aK3aT4pH" + } +} \ No newline at end of file diff --git a/src/mixer.ts b/src/mixer.ts index e03a58f..fc9e83b 100644 --- a/src/mixer.ts +++ b/src/mixer.ts @@ -27,18 +27,28 @@ export default class Mixer { async http_handler(req: IncomingMessage, res: ServerResponse){ - let urlObj : UrlWithParsedQuery = parseURL(req.url,true) - printf('\n -- new client --\n') + let urlObj : UrlWithParsedQuery = parseURL(req.url, true) // reject invalid requests - if( req.method != 'POST' ) return Mixer.http_error(res, 'only POST method allowed'); + if( req.method != 'GET' ) return Mixer.http_error(res, 'only GET method allowed'); else if( Mixer.valid_urls.indexOf(urlObj.pathname) < 0 ) return Mixer.http_error(res, `only URI [${Mixer.valid_urls}] allowed`); + printf('\n -- new request --\n') try{ let query_result = await this.query(req, res) - res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify( this.format(query_result) )); + + // if accept is set to HTML -> html format + if( req.headers['accept'] && req.headers['accept'].includes("json") ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify( this.format(query_result) )); + + // fallback to JSON by default + } else{ + res.writeHead(200, { 'Content-Type': 'text/html' }) + let query = parseURL(req.url,true).query['q'] + res.end(this.toHTML(query, this.format(query_result)) ); + } }catch(err){ @@ -47,6 +57,8 @@ export default class Mixer { } + printf(' -- response sent --\n') + } @@ -95,4 +107,49 @@ export default class Mixer { } } + + toHTML(request:any, input:any) : string { + let html = ` + + results of '${request}' + + + + + + + + + + + + + ` + + for( let i = 0 ; i < input.results.length ; i++ ){ + html += ` + + + + ` + } + + return html + } + } \ No newline at end of file
namegtindescriptionquantityimages
${input.results[i].name}${input.results[i].gtin}${input.results[i].description}${input.results[i].quantity.value} ${input.results[i].quantity.unit}` + + if( input.results[i]['images'] ){ + + let keys = Object.keys(input.results[i].images) + for( let k of keys ) + if( input.results[i].images[k] != null ) + html += `` + + } + + html += `