update main/mixer | create api common interface

This commit is contained in:
Adrien Marquès 2018-10-22 17:41:49 +02:00
parent da6edab899
commit 19d7743b1d
3 changed files with 78 additions and 4 deletions

28
src/api/interface.ts Normal file
View File

@ -0,0 +1,28 @@
export default interface Client {
/** Processes an API call to the given resource and returns a Promise with the
* error description OR the result data as JSON
*
* @param method the HTTP METHOD to use
* @param path The API path
* @param params The API-specific options
*/
call(method: HTTP_METHOD, path: String, params?: Parameters) : Promise<object|Error>;
}
// Defines HTTP methods subset
export enum HTTP_METHOD {
GET,
POST,
PUT,
DELETE
}
export class Parameters{
get: object;
post: object;
constructor(){ this.get = {}; this.post = {}; }
}

View File

@ -1,6 +1,7 @@
import { createServer } from 'http'
import { Socket } from 'net';
import Mixer from './mixer'
import CarrefourItems from './api/items';
const mixer: Mixer = new Mixer();
@ -10,9 +11,10 @@ const mixer: Mixer = new Mixer();
const server = createServer(mixer.http_handler);
// 2. bind 404 error
server.on('clientError', (err: Error, sock: Socket) => {
server.on('error', (err: Error, sock: Socket) => {
sock.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
// 3. listen on given port
server.listen(8000);
console.log('+ Listen :8080')

View File

@ -1,24 +1,68 @@
import { IncomingMessage, ServerResponse, Server } from "http";
import CarrefourItems from "./api/items";
import { parse as parseURL, UrlWithParsedQuery } from "url";
import { ParsedUrlQuery } from "querystring";
export default class Mixer {
static valid_urls = ["/", "/oauth"];
carrefour: CarrefourItems = new CarrefourItems();
constructor(){
console.log("mixer created");
console.log("Mixer Initialised");
}
http_handler(req: IncomingMessage, res: ServerResponse){
let urlObj : UrlWithParsedQuery = parseURL(req.url,true)
// reject invalid requests
if( req.method != 'POST' ){
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({reason: 'invalid HTTP method; only POST allowed'}))
}else if( req.url != "/" ){
return;
}else if( Mixer.valid_urls.indexOf(urlObj.pathname) < 0 ){
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({reason: 'invalid URI; only / allowed'}))
res.end(JSON.stringify({reason: `invalid URI; only [${Mixer.valid_urls}] allowed`}))
return;
}
// if query
if( urlObj.pathname == "/" ){
let err:Error = Mixer.query(req, res);
if( err != null ){
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({reason: `query error: ${err.toString()}`}))
}
// if oauth
} else {
let err:Error = Mixer.oauth(req, res);
if( err != null ){
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({reason: `query error: ${err.toString()}`}))
}
}
res.end();
}
static query(req: IncomingMessage, res: ServerResponse) : Error{
let query: ParsedUrlQuery = parseURL(req.url,true).query
console.log('query is:', query['q'])
return null;
}
static oauth(req: IncomingMessage, res: ServerResponse) : Error{
console.log(`oauth(${req},${res})`);
let query: ParsedUrlQuery = parseURL(req.url,true).query
console.log('query is:', query);
return new Error('OAuth error');
}
}