Created
July 4, 2020 02:44
-
-
Save simonho288/35cd83f6234a0b32965a88f478db09c7 to your computer and use it in GitHub Desktop.
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 unlockWalletRestApi() { | |
| // Make a POST request with a JSON payload. | |
| var options = { | |
| 'method': 'post', | |
| 'payload': '' | |
| }; | |
| UrlFetchApp.fetch('http://demo.simonho.net:5000/api/unlock_wallet', options); | |
| } | |
| function createAccountRestApi() { | |
| // Make a POST request with a JSON payload. | |
| var options = { | |
| 'method': 'post', | |
| 'contentType': 'application/json', | |
| 'payload': JSON.stringify({}) | |
| //'payload': '', | |
| }; | |
| var response = UrlFetchApp.fetch('http://demo.simonho.net:5000/api/create_account', options); | |
| console.log(response.getContentText()); | |
| return response.getContentText(); | |
| } | |
| function voteCandidateRestApi(account, candidateKey) { | |
| console.log('account: ' + account); | |
| console.log('candidateKey: ' + candidateKey); | |
| // Make a POST request with a JSON payload. | |
| var data = { | |
| account: account, | |
| candidate: candidateKey | |
| }; | |
| var options = { | |
| 'method': 'post', | |
| 'payload': data | |
| }; | |
| UrlFetchApp.fetch('http://demo.simonho.net:5000/api/vote_candidate', options); | |
| } | |
| function candidatesRestApi_() { | |
| var url = 'http://demo.simonho.net:5000/api/candidates'; | |
| console.log('Election backend url: ' + url); | |
| var response = JSON.parse(UrlFetchApp.fetch(url)); | |
| return response; | |
| } | |
| function voteRestApi_() { | |
| // Make a POST request with a JSON payload. | |
| var data = { | |
| 'name': 'Bob Smith', | |
| 'age': 35, | |
| 'pets': ['fido', 'fluffy'] | |
| }; | |
| var options = { | |
| 'method' : 'post', | |
| 'contentType': 'application/json', | |
| // Convert the JavaScript object to a JSON string. | |
| 'payload' : JSON.stringify(data) | |
| }; | |
| UrlFetchApp.fetch('http://demo.simonho.net:5000/api/vote_candidate', options); | |
| } | |
| function getCandidates_() { | |
| var response; | |
| try { | |
| response = candidatesRestApi_(); | |
| } catch (error) { | |
| throw new Error('Unable to call RESTful API from candidatesRestApi_()'); | |
| } | |
| var records = response.map(function (item) { | |
| var record = app.models.ElectionData.newRecord(); | |
| record.Count = item._count; | |
| record.Key = item._key; | |
| record.Name = item._name; | |
| return record; | |
| }); | |
| return records; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment