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;
/** 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<object>[];
/* Returns a promise resolving on the output data of the element <i> and rejecting the error if any */
get(i: number) : Promise<object>;
}
}

View File

@ -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';

View File

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

View File

@ -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) => {

View File

@ -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);