Last active
December 24, 2022 14:38
-
-
Save JayGoldberg/3415b1885203e61f7181b6e5d868c07b to your computer and use it in GitHub Desktop.
App Script REST style API in doGet() and doPost()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet(e) { | |
// Look up the function for the specified endpoint | |
var handler = getEndpointHandlers[e.parameter.action]; | |
if (handler) { | |
// Call the appropriate function for the endpoint | |
return handler(e); | |
} else { | |
// Return an error if the endpoint is not recognized | |
return ContentService.createTextOutput("Error: Invalid GET endpoint.").setMimeType(ContentService.MimeType.TEXT); | |
} | |
} | |
function doPost(e) { | |
// Look up the function for the specified endpoint | |
var handler = getEndpointHandlers[e.parameter.action]; | |
if (handler) { | |
// Call the appropriate function for the endpoint | |
return handler(e); | |
} else { | |
// Return an error if the endpoint is not recognized | |
return ContentService.createTextOutput("Error: Invalid POST endpoint.").setMimeType(ContentService.MimeType.TEXT); | |
} | |
} | |
// ROUTES: containing functions to handle GET requests to different endpoints | |
var getEndpointHandlers = { | |
'apnSearch': bqApnSearch, | |
'users': getUsers, | |
'schemas': getSchemas, | |
'schema': getSchema, | |
'properties': propSearch | |
}; | |
// ROUTES: containing functions to handle POST requests to different endpoints | |
var postEndpointHandlers = { | |
'bqImportCsv': bqImportCsv, | |
'bqDeleteTable': bqDeleteTable, | |
'properties': propSearch | |
}; | |
// TODO: add examples for parameter handling/parsing parameters passed to handler | |
// https://script.google.com/macros/s/<macro_id>/exec?action=apnSearch/3938844&fields=name,address,phone | |
function getSchemas(e) { | |
var schemas = ['parcels','dsd','ced','listings'] | |
return ContentService.createTextOutput(JSON.stringify(schemas)).setMimeType(ContentService.MimeType.JSON); | |
//return ContentService.createTextOutput(JSON.stringify(schema)).setMimeType(ContentService.MimeType.JSON); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment