-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removed traces of the iDEAL API - Added keywords - Added the signal API - Moved the JSONRequest class to a separate directory - Added some tests for both signal and address
- Loading branch information
Showing
5 changed files
with
181 additions
and
99 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const https = require("https"); | ||
|
||
class JSONRequest { | ||
constructor(options) { | ||
this._options = options; | ||
} | ||
|
||
_requestOptions(method, path, headers, encoding) { | ||
if(!headers) { | ||
headers = { | ||
"Content-Type": "application/json" | ||
}; | ||
} | ||
|
||
if(!encoding) { | ||
encoding = "utf8"; | ||
} | ||
|
||
return { | ||
method, headers, encoding, | ||
hostname: "api.postcode.nl", | ||
path: "/rest" + path, | ||
auth: this._options.key+":"+this._options.secret | ||
} | ||
} | ||
|
||
_parseJsonResponse(response) { | ||
return new Promise((resolve, reject) => { | ||
const strings = []; | ||
|
||
response.on("data", (chunk) => { | ||
strings.push(chunk); | ||
}); | ||
|
||
response.on("end", () => { | ||
try { | ||
const string = strings.join(""); | ||
if(response.statusCode === 200) { | ||
resolve(JSON.parse(string)); | ||
}else if(response.statusCode === 401) { | ||
reject(Object.assign(new Error("Invalid credentials for call"), { | ||
statusCode: response.statusCode, | ||
headers: response.headers | ||
})); | ||
}else{ | ||
let json; | ||
let error = "Request returned HTTP code "+response.statusCode; | ||
try { | ||
json = JSON.parse(string); | ||
if(json && json.exception) { | ||
error = json.exception; | ||
} | ||
} catch (err) { | ||
|
||
} | ||
reject(Object.assign(new Error(error), { | ||
string, json, | ||
statusCode: response.statusCode, | ||
headers: response.headers | ||
})); | ||
} | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
|
||
response.on("error", reject); | ||
}); | ||
} | ||
|
||
get(path) { | ||
return new Promise((resolve, reject) => { | ||
const opts = this._requestOptions("GET", path); | ||
const request = https.request(opts, (res) => { | ||
resolve(this._parseJsonResponse(res)); | ||
}); | ||
request.on("error", reject); | ||
request.end(); | ||
}); | ||
} | ||
|
||
post(path, data) { | ||
return new Promise((resolve, reject) => { | ||
const postData = JSON.stringify(data); | ||
const opts = this._requestOptions("POST", path, { | ||
"Content-Type": "application/json", | ||
"Content-Length": postData.length | ||
}); | ||
|
||
const request = https.request(opts, (res) => { | ||
resolve(this._parseJsonResponse(res)); | ||
}); | ||
request.write(postData); | ||
request.on("error", reject); | ||
request.end(); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = JSONRequest; |
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