Skip to content

Commit

Permalink
Add RPC example
Browse files Browse the repository at this point in the history
  • Loading branch information
richardolsson committed Jan 10, 2024
1 parent 7fcabce commit 1821db7
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions classes/a502/examples/rest-vs-rpc/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ function setName(personId, name) {
return person;
}

//*
// RPC version
export async function handle(method, path, queryString, headers, body) {
console.log('');
console.log('= REQUEST =====================');
console.log('method: ', method);
console.log('path: ', path);
console.log('query: ', queryString);
console.log('headers: ', headers);
console.log('body: ', body);

if (method == 'POST' && path == '/rpc') {
if (body.func == 'getPeople') {
return {
status: 200,
body: JSON.stringify({
returnValue: getPeople(),
}),
};
} else if (body.func == 'getPerson') {
const personId = body.parameters[0];
return {
status: 200,
body: JSON.stringify({
returnValue: getPerson(personId),
}),
}
} else if (body.func == 'setName') {
const personId = body.parameters[0];
const newName = body.parameters[1];
return {
status: 200,
body: JSON.stringify({
returnValue: setName(personId, newName),
})
}
}
}
}

/*/
// REST version
export async function handle(method, path, queryString, headers, body) {
console.log('');
console.log('= REQUEST =====================');
Expand Down Expand Up @@ -58,3 +100,4 @@ export async function handle(method, path, queryString, headers, body) {
}
}
}
//*/

0 comments on commit 1821db7

Please sign in to comment.