Created
November 26, 2018 15:49
-
-
Save bentstamnes/8aa8f657cf3378ebc033d3c91da132e7 to your computer and use it in GitHub Desktop.
Test
This file contains 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 HTTPCall(HTTPRequest); // Makes async HTTP request and returns the response in the following update frame. | |
class HTTPRequest { | |
constructor(url, onComplete) { | |
this.url = url; // URL to get data from | |
this.onComplete = onComplete; // The function that will be called the next update cycle after the request has completed. | |
this.method = "GET"; // "GET", "POST", "PUT", "DELETE" | |
this.payload = ""; // Payload when using POST or PUT | |
this.timeout = 5; // Number of seconds to wait for a response. default = 5 seconds | |
this.headers = {}; // Key pair values for the header (these get turned into a string) | |
} | |
} | |
class HTTPResponse { | |
constructor() { | |
this.responseText; // Response as string. | |
this.responseJSON; // Parse response as parsed JSON. Returns "{ error: "Unable to parse JSON" }" if unable to parse the JSON | |
this.statusCode; // Integer number of the status code. Use -1 if unable to make connection or other error. | |
this.headers; // a dict of the returned headers | |
} | |
} | |
// ------- Example --------- | |
req = new HTTPRequest("https://example.com/endpoint", weatherResponse); | |
HTTPCall(req); | |
function weatherResponse(response) { | |
if (response.statusCode == 200) { | |
console.log(response.responseText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment