refactor according to the report

This commit is contained in:
Adrien Marquès 2018-11-30 19:11:38 +01:00
parent a4c3c46f04
commit 84979e63c2
5 changed files with 36 additions and 27 deletions

View File

@ -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; feed_query(query: object|string) : boolean;
/** Processes an API call to the given resource and returns a Promise resolving with the output object /* Returns a promise resolving on the output data and rejecting the error if any */
* or rejecting the Error
*
* @param query represents the input data composing the request
*/
get_all() : Promise<object>[]; get_all() : Promise<object>[];
/* Returns a promise resolving on the output data of the element <i> and rejecting the error if any */
get(i: number) : Promise<object>; get(i: number) : Promise<object>;
} }

View File

@ -1,10 +1,10 @@
import Client from "./interface"; import Resource from "./interface";
import Key from "./keys"; import Key from "./keys";
import { request } from 'https'; import { request } from 'https';
import { printf, pad, reprintf } from "../std/printf"; import { printf, pad, reprintf } from "../std/printf";
import { extract } from "../std/extract"; 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 host: string = 'api.fr.carrefour.io';
static uri: string = '/v1/openapi/items'; static uri: string = '/v1/openapi/items';

View File

@ -1,11 +1,11 @@
import Client from "./interface"; import Resource from "./interface";
import Key from "./keys"; import Key from "./keys";
import { request } from 'https'; import { request } from 'https';
import { printf, pad, reprintf } from "../std/printf"; import { printf, pad, reprintf } from "../std/printf";
import { extract } from "../std/extract"; import { extract } from "../std/extract";
import { join } from "path"; import { join } from "path";
export default class OpenFoodFacts implements Client { export default class OpenFoodFacts implements Resource {
static host: string = 'world.openfoodfacts.org'; static host: string = 'world.openfoodfacts.org';
static uri: string = '/api/v0/product/'; static uri: string = '/api/v0/product/';

View File

@ -1,13 +1,18 @@
import { createServer } from 'http' import { createServer } from 'http'
import { Socket } from 'net'; import { Socket } from 'net';
import Mixer from './mixer' import Multiplexer from './multiplexer'
import { printf } from './std/printf'; 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 // 1. build server
const server = createServer(mixer.http_handler.bind(mixer)); const server = createServer(multiplexer.http_handler.bind(multiplexer));
// 2. bind 404 error // 2. bind 404 error
server.on('clientError', (err: Error, sock: Socket) => { server.on('clientError', (err: Error, sock: Socket) => {

View File

@ -2,18 +2,18 @@ import { IncomingMessage, ServerResponse, Server } from "http";
import CarrefourItems from "./api/items"; import CarrefourItems from "./api/items";
import { parse as parseURL, UrlWithParsedQuery } from "url"; import { parse as parseURL, UrlWithParsedQuery } from "url";
import { ParsedUrlQuery } from "querystring"; import { ParsedUrlQuery } from "querystring";
import Client from "./api/interface"; import Resource from "./api/interface";
import { printf, pad } from "./std/printf"; import { printf, pad } from "./std/printf";
import OpenFoodFacts from "./api/off"; import OpenFoodFacts from "./api/off";
export default class Mixer { export default class Multiplexer {
static valid_urls = ["/"]; static valid_urls = ["/"];
stack: Resource[];
chain: Client[] = [new CarrefourItems(), new OpenFoodFacts()]; constructor(...res : Resource[]){
printf('+ multiplexer')
constructor(){ this.stack = res
printf('+ mixer')
pad('loaded\n'); pad('loaded\n');
} }
@ -30,8 +30,8 @@ export default class Mixer {
let urlObj : UrlWithParsedQuery = parseURL(req.url, true) let urlObj : UrlWithParsedQuery = parseURL(req.url, true)
// reject invalid requests // reject invalid requests
if( req.method != 'GET' ) return Mixer.http_error(res, 'only GET method allowed'); if( req.method != 'GET' ) return Multiplexer.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`); 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') printf('\n -- new request --\n')
try{ try{
@ -67,16 +67,16 @@ export default class Mixer {
let query: ParsedUrlQuery = parseURL(req.url,true).query let query: ParsedUrlQuery = parseURL(req.url,true).query
let out: any[] = []; 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{ try{
if( !this.chain[i].feed_query(input) ) if( !resource.feed_query(input) )
throw new Error(`invalid query for api ${i}`) 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){ }catch(e){
printf(' ! failure \t\t\t(%s)\n', e.message); printf(' ! failure \t\t\t(%s)\n', e.message);