2015-06-27 15:03:50 +02:00
//api.js
2015-08-16 13:42:14 +02:00
/ * *
* Module for handling API requests .
* API is able to be called through / api / : apirequest ? = arguments
* @ module api
2015-08-16 13:48:08 +02:00
* @ author Bram van der Veen < 96 aa48 @ gmail . com >
2015-08-16 13:42:14 +02:00
* /
2015-08-16 01:37:27 +02:00
//Importing self-written modules.
2016-06-18 15:06:51 +02:00
const lookup = require ( './lookup' ) ;
const schedule = require ( './schedule' ) ;
2015-06-27 15:03:50 +02:00
2015-08-16 01:37:27 +02:00
/ * *
* Takes the information from the request
* and tries to return information .
* @ param { Object } req - Request object supplied by Express .
* @ param { Object } res - Response object supplied by Express .
* @ param { Function } next - Next function supplied by Express .
* @ param { String } api - Type of API request supplied by the user .
* /
2015-06-27 15:03:50 +02:00
function parse ( req , res , next , api ) {
req . api = true ;
if ( api == 'search' ) {
if ( ! req . query . name ) error ( 'You didn\'t send the needed queries: name' , res ) ;
else {
lookup . api ( req , function ( lookup ) {
if ( lookup . error ) error ( lookup . error , res ) ;
2015-08-15 21:07:22 +02:00
else sendResponse ( lookup . data , res ) ;
2015-06-27 15:03:50 +02:00
} ) ;
}
}
else if ( api == 'schedule' ) {
if ( ! req . query . name ) error ( 'You didn\'t send the needed queries : name' , res ) ;
else {
lookup . api ( req , function ( lookup ) {
if ( lookup . error ) error ( lookup . error , res ) ;
else {
if ( lookup . data . length > 1 || lookup . data . length == 0 ) error ( 'The request that you did had multiple responses or none, make sure that your query returns one.' , res , lookup . data )
else {
2015-08-15 21:07:22 +02:00
schedule . api ( lookup , function ( scheduleData ) {
sendResponse ( scheduleData , res , true ) ;
2015-06-27 15:03:50 +02:00
} ) ;
}
}
} )
}
}
}
2015-08-16 01:37:27 +02:00
/ * *
* Returns an error to the user when the request
* was unable to complete .
* @ param { String } str - The error message to display .
* @ param { Object } res - Reponse object supplied by Express .
* @ param { Function } next - Next function supplied by Express .
* @ return { null }
* /
2015-06-27 15:03:50 +02:00
function error ( str , res , data ) {
res . set ( 'Content-Type' , 'application/json' ) ;
2016-06-18 15:06:51 +02:00
res . status ( 400 ) . end ( JSON . stringify ( { 'error' : str , 'data' : data } , null , 2 ) ) ;
2015-06-27 15:03:50 +02:00
return ;
}
2015-08-16 01:37:27 +02:00
/ * *
* Sends a response to the user that did an API request .
* @ param { Object } data - Data object with requested data .
* @ param { Object } res - Response object supplied by Express .
* @ param { Bool } disablePretty - Boolean to disable pretty printing of the response .
* @ return { null }
* /
2015-08-15 21:07:22 +02:00
function sendResponse ( data , res , disablePretty ) {
2015-06-27 15:03:50 +02:00
res . set ( 'Content-Type' , 'application/json' ) ;
2016-06-18 15:06:51 +02:00
res . status ( 200 ) . end ( JSON . stringify ( { 'data' : data } , null , disablePretty ? 0 : 2 ) ) ;
2015-06-27 15:03:50 +02:00
return ;
}
2015-08-16 01:37:27 +02:00
//Exporting the parse function as a module.
2015-06-27 15:03:50 +02:00
module . exports = parse ;