From 84979e63c2d6913f3213fce813f34d0129ad987d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 30 Nov 2018 19:11:38 +0100 Subject: [PATCH] refactor according to the report --- src/api/interface.ts | 18 +++++++++++------- src/api/items.ts | 4 ++-- src/api/off.ts | 4 ++-- src/main.ts | 11 ++++++++--- src/{mixer.ts => multiplexer.ts} | 26 +++++++++++++------------- 5 files changed, 36 insertions(+), 27 deletions(-) rename src/{mixer.ts => multiplexer.ts} (83%) diff --git a/src/api/interface.ts b/src/api/interface.ts index 1e26b1f..605be70 100644 --- a/src/api/interface.ts +++ b/src/api/interface.ts @@ -1,14 +1,18 @@ -export default interface Client { +export default interface Resource { + /** Define the Resource query and returns whether it is valid + * + * @param query the query composing the request + * @returns valid the validity of the received (true = valid) + */ feed_query(query: object|string) : boolean; - /** Processes an API call to the given resource and returns a Promise resolving with the output object - * or rejecting the Error - * - * @param query represents the input data composing the request - */ + /* Returns a promise resolving on the output data and rejecting the error if any */ get_all() : Promise[]; + /* Returns a promise resolving on the output data of the element and rejecting the error if any */ get(i: number) : Promise; -} \ No newline at end of file +} + + diff --git a/src/api/items.ts b/src/api/items.ts index ca89acf..ad573d4 100644 --- a/src/api/items.ts +++ b/src/api/items.ts @@ -1,10 +1,10 @@ -import Client from "./interface"; +import Resource from "./interface"; import Key from "./keys"; import { request } from 'https'; import { printf, pad, reprintf } from "../std/printf"; import { extract } from "../std/extract"; -export default class CarrefourItems implements Client { +export default class CarrefourItems implements Resource { static host: string = 'api.fr.carrefour.io'; static uri: string = '/v1/openapi/items'; diff --git a/src/api/off.ts b/src/api/off.ts index e940b76..9baa2bd 100644 --- a/src/api/off.ts +++ b/src/api/off.ts @@ -1,11 +1,11 @@ -import Client from "./interface"; +import Resource from "./interface"; import Key from "./keys"; import { request } from 'https'; import { printf, pad, reprintf } from "../std/printf"; import { extract } from "../std/extract"; import { join } from "path"; -export default class OpenFoodFacts implements Client { +export default class OpenFoodFacts implements Resource { static host: string = 'world.openfoodfacts.org'; static uri: string = '/api/v0/product/'; diff --git a/src/main.ts b/src/main.ts index a3aec76..63a752d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,18 @@ import { createServer } from 'http' import { Socket } from 'net'; -import Mixer from './mixer' +import Multiplexer from './multiplexer' import { printf } from './std/printf'; +import CarrefourItems from './api/items'; +import OpenFoodFacts from './api/off'; -const mixer: Mixer = new Mixer(); +const multiplexer: Multiplexer = new Multiplexer( + new CarrefourItems(), + new OpenFoodFacts() +); // 1. build server -const server = createServer(mixer.http_handler.bind(mixer)); +const server = createServer(multiplexer.http_handler.bind(multiplexer)); // 2. bind 404 error server.on('clientError', (err: Error, sock: Socket) => { diff --git a/src/mixer.ts b/src/multiplexer.ts similarity index 83% rename from src/mixer.ts rename to src/multiplexer.ts index fc9e83b..52effd0 100644 --- a/src/mixer.ts +++ b/src/multiplexer.ts @@ -2,18 +2,18 @@ import { IncomingMessage, ServerResponse, Server } from "http"; import CarrefourItems from "./api/items"; import { parse as parseURL, UrlWithParsedQuery } from "url"; import { ParsedUrlQuery } from "querystring"; -import Client from "./api/interface"; +import Resource from "./api/interface"; import { printf, pad } from "./std/printf"; import OpenFoodFacts from "./api/off"; -export default class Mixer { +export default class Multiplexer { static valid_urls = ["/"]; + stack: Resource[]; - chain: Client[] = [new CarrefourItems(), new OpenFoodFacts()]; - - constructor(){ - printf('+ mixer') + constructor(...res : Resource[]){ + printf('+ multiplexer') + this.stack = res pad('loaded\n'); } @@ -30,8 +30,8 @@ export default class Mixer { let urlObj : UrlWithParsedQuery = parseURL(req.url, true) // reject invalid requests - 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`); + if( req.method != 'GET' ) return Multiplexer.http_error(res, 'only GET method allowed'); + else if( Multiplexer.valid_urls.indexOf(urlObj.pathname) < 0 ) return Multiplexer.http_error(res, `only URI [${Multiplexer.valid_urls}] allowed`); printf('\n -- new request --\n') try{ @@ -67,16 +67,16 @@ export default class Mixer { let query: ParsedUrlQuery = parseURL(req.url,true).query let out: any[] = []; - for( let i = 0 ; i < this.chain.length ; i++ ){ + for( let resource of this.stack ){ - let input = (i<1) ? query : out[i-1]; + let input = out.length<1 ? query : out[out.length-1]; try{ - if( !this.chain[i].feed_query(input) ) - throw new Error(`invalid query for api ${i}`) + if( !resource.feed_query(input) ) + throw new Error(`invalid query for resource ${out.length}`) - out[i] = await Promise.all( this.chain[i].get_all() ); + out.push( await Promise.all(resource.get_all()) ); }catch(e){ printf(' ! failure \t\t\t(%s)\n', e.message);