-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add User-Agent header to outgoing HTTP requests (#19)
Excludes requests made by the AWS SDK to read/write data in S3, but includes all other HTTP requests made during operation, e.g. when getting changeset metadata from the OSM API, or when querying the Overpass server.
- Loading branch information
Showing
6 changed files
with
330 additions
and
348 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
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 |
---|---|---|
@@ -1,49 +1,23 @@ | ||
const https = require('https'); | ||
const fetch = require('node-fetch'); | ||
const { version } = require("../package.json"); | ||
|
||
const request = async (url, method = 'GET', postData, token) => { | ||
const [h, ...rest] = url.split('://')[1].split('/'); | ||
const path = `/${rest.join('/')}`; | ||
const [host, port] = h.split(':'); | ||
async function request(url, options = {}) { | ||
if (!options.headers) { | ||
options.headers = {} | ||
} | ||
|
||
const params = { | ||
method, | ||
host, | ||
port: port || url.startsWith('https://') ? 443 : 80, | ||
path: path || '/', | ||
}; | ||
options.headers['User-Agent'] = `OSMCha osm-adiff-service ${version}` | ||
|
||
if (token && postData) params.headers = { | ||
Authorization: token, | ||
'Content-Type': 'application/json', | ||
'Content-Length' : Buffer.byteLength(postData, 'utf8') | ||
}; | ||
let res = await fetch(url, options); | ||
|
||
if (res.ok) { | ||
return res; | ||
} else { | ||
let error = new Error(`HTTP ${res.status} ${res.statusText}`); | ||
error.status = res.status; | ||
error.statusText = res.statusText; | ||
throw error; | ||
} | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const req = https.request(params, res => { | ||
if (res.statusCode < 200 || res.statusCode >= 303) { | ||
return reject(new Error(`Status Code: ${res.statusCode}`)); | ||
} | ||
|
||
const data = []; | ||
|
||
res.on('data', chunk => { | ||
data.push(chunk); | ||
}); | ||
|
||
res.on('end', () => resolve(Buffer.concat(data).toString())); | ||
}); | ||
|
||
req.on('error', reject); | ||
|
||
if (postData) { | ||
req.write(postData); | ||
} | ||
|
||
req.end(); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
request, | ||
}; | ||
module.exports = { request }; |
Oops, something went wrong.