Skip to content

Commit

Permalink
use api.osm.org url for OSM API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrasd committed Oct 10, 2023
1 parent 5d00f60 commit c4d1390
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
3 changes: 2 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ In addition, the following parameters are available as **URL query parameters**:

Environment variables or a dotenv file can be used to configure certain aspects of iD at build time.

* __`ID_API_CONNECTION_URL`__, __`ID_API_CONNECTION_CLIENT_ID`__, __`ID_API_CONNECTION_CLIENT_SECRET`__ - Custom [Oauth2](https://wiki.openstreetmap.org/wiki/OAuth#OAuth_2.0_2) connection details to an OSM API server.
* __`ID_API_CONNECTION_URL`__, __`ID_API_CONNECTION_CLIENT_ID`__, __`ID_API_CONNECTION_CLIENT_SECRET`__ - Custom [OAuth2](https://wiki.openstreetmap.org/wiki/OAuth#OAuth_2.0_2) connection details to an OSM API server.
* __`ID_API_CONNECTION_API_URL`__ Optional url to use for OSM API calls aftern the initial authentication is complete when using a custom OAuth2 connection (see above). If unspecified, `ID_API_CONNECTION_URL` will be used for both the authentication and subsequent API calls.
* __`ID_API_CONNECTION`__ - Either `live` or `dev`, if only either one should be made offered for editing.
* __`ID_PRESETS_CDN_URL`__ - The URL where iD should fetch it's tagging presets from. Needs to point to a CORS enabled web server which is serving the `package.json` and `dist` folder of a repository built on [`@ideditor/schema-builder`](https://github.com/ideditor/schema-builder).
* __`ENV__ID_OCI_CDN_URL`__ - URL to a hosted version of the [osm-community-index](https://github.com/osmlab/osm-community-index)
Expand Down
1 change: 1 addition & 0 deletions config/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const nsiCdnUrl = ENV__ID_NSI_CDN_URL
const defaultOsmApiConnections = {
live: {
url: 'https://www.openstreetmap.org',
apiUrl: 'https://api.openstreetmap.org',
client_id: '0tmNTmd0Jo1dQp4AUmMBLtGiD9YpMuXzHefitcuVStc',
client_secret: 'BTlNrNxIPitHdL4sP2clHw5KLoee9aKkA7dQbc0Bj7Q'
},
Expand Down
73 changes: 44 additions & 29 deletions modules/services/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var tiler = utilTiler();
var dispatch = d3_dispatch('apiStatusChange', 'authLoading', 'authDone', 'change', 'loading', 'loaded', 'loadedNotes');

var urlroot = osmApiConnections[0].url;
var apiUrlroot = osmApiConnections[0].apiUrl || urlroot;
var redirectPath = window.location.origin + window.location.pathname;
var oauth = osmAuth({
url: urlroot,
Expand Down Expand Up @@ -669,7 +670,11 @@ export default {
}

if (this.authenticated()) {
return oauth.xhr({ method: 'GET', path: path }, done);
return oauth.xhr({
method: 'GET',
prefix: false,
path: urlroot + path
}, done);
} else {
var url = urlroot + path;
var controller = new AbortController();
Expand Down Expand Up @@ -796,7 +801,8 @@ export default {
} else { // Open a new changeset..
var options = {
method: 'PUT',
path: '/api/0.6/changeset/create',
prefix: false,
path: apiUrlroot + '/api/0.6/changeset/create',
headers: { 'Content-Type': 'text/xml' },
content: JXON.stringify(changeset.asJXON())
};
Expand All @@ -817,7 +823,8 @@ export default {
// Upload the changeset..
var options = {
method: 'POST',
path: '/api/0.6/changeset/' + changesetID + '/upload',
prefix: false,
path: apiUrlroot + '/api/0.6/changeset/' + changesetID + '/upload',
headers: { 'Content-Type': 'text/xml' },
content: JXON.stringify(changeset.osmChangeJXON(changes))
};
Expand All @@ -843,7 +850,8 @@ export default {
// Still attempt to close changeset, but ignore response because #2667
oauth.xhr({
method: 'PUT',
path: '/api/0.6/changeset/' + changeset.id + '/close',
prefix: false,
path: apiUrlroot + '/api/0.6/changeset/' + changeset.id + '/close',
headers: { 'Content-Type': 'text/xml' }
}, function() { return true; });
}
Expand Down Expand Up @@ -873,10 +881,11 @@ export default {
}

utilArrayChunk(toLoad, 150).forEach(function(arr) {
oauth.xhr(
{ method: 'GET', path: '/api/0.6/users.json?users=' + arr.join() },
wrapcb(this, done, _connectionID)
);
oauth.xhr({
method: 'GET',
prefix: false,
path: apiUrlroot + '/api/0.6/users.json?users=' + arr.join()
}, wrapcb(this, done, _connectionID));
}.bind(this));

function done(err, payload) {
Expand All @@ -899,10 +908,11 @@ export default {
return callback(undefined, _userCache.user[uid]);
}

oauth.xhr(
{ method: 'GET', path: '/api/0.6/user/' + uid + '.json' },
wrapcb(this, done, _connectionID)
);
oauth.xhr({
method: 'GET',
prefix: false,
path: apiUrlroot + '/api/0.6/user/' + uid + '.json'
}, wrapcb(this, done, _connectionID));

function done(err, payload) {
if (err) return callback(err);
Expand All @@ -923,10 +933,11 @@ export default {
return callback(undefined, _userDetails);
}

oauth.xhr(
{ method: 'GET', path: '/api/0.6/user/details.json' },
wrapcb(this, done, _connectionID)
);
oauth.xhr({
method: 'GET',
prefix: false,
path: apiUrlroot + '/api/0.6/user/details.json'
}, wrapcb(this, done, _connectionID));

function done(err, payload) {
if (err) return callback(err);
Expand Down Expand Up @@ -956,10 +967,11 @@ export default {
function gotDetails(err, user) {
if (err) { return callback(err); }

oauth.xhr(
{ method: 'GET', path: '/api/0.6/changesets?user=' + user.id },
wrapcb(this, done, _connectionID)
);
oauth.xhr({
method: 'GET',
prefix: false,
path: apiUrlroot + '/api/0.6/changesets?user=' + user.id
}, wrapcb(this, done, _connectionID));
}

function done(err, xml) {
Expand All @@ -981,7 +993,7 @@ export default {
// Fetch the status of the OSM API
// GET /api/capabilities
status: function(callback) {
var url = urlroot + '/api/capabilities';
var url = apiUrlroot + '/api/capabilities';
var errback = wrapcb(this, done, _connectionID);
d3_xml(url)
.then(function(data) { errback(null, data); })
Expand Down Expand Up @@ -1195,10 +1207,11 @@ export default {

var path = '/api/0.6/notes?' + utilQsString({ lon: note.loc[0], lat: note.loc[1], text: comment });

_noteCache.inflightPost[note.id] = oauth.xhr(
{ method: 'POST', path: path },
wrapcb(this, done, _connectionID)
);
_noteCache.inflightPost[note.id] = oauth.xhr({
method: 'POST',
prefix: false,
path: urlroot + path
}, wrapcb(this, done, _connectionID));


function done(err, xml) {
Expand Down Expand Up @@ -1247,10 +1260,11 @@ export default {
path += '?' + utilQsString({ text: note.newComment });
}

_noteCache.inflightPost[note.id] = oauth.xhr(
{ method: 'POST', path: path },
wrapcb(this, done, _connectionID)
);
_noteCache.inflightPost[note.id] = oauth.xhr({
method: 'POST',
prefix: false,
path: urlroot + path
}, wrapcb(this, done, _connectionID));


function done(err, xml) {
Expand Down Expand Up @@ -1289,6 +1303,7 @@ export default {

switch: function(newOptions) {
urlroot = newOptions.url;
apiUrlroot = newOptions.apiUrl || urlroot;

// Copy the existing options, but omit 'access_token'.
// (if we did preauth, access_token won't work on a different server)
Expand Down
17 changes: 17 additions & 0 deletions test/spec/services/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,23 @@ describe('iD.serviceOsm', function () {
done();
});
});

it('uses apiUrl', function(done) {
fetchMock.mock('https://api.openstreetmap.org' + path, {
body: response,
status: 200,
headers: { 'Content-Type': 'application/json' }
});

connection.switch({ url: 'https://api.openstreetmap.org' });

connection.loadFromAPI(path, function (err) {
expect(err).to.not.be.ok;
expect(fetchMock.calls().length).to.eql(1);
expect(fetchMock.calls()[0][0]).to.eql('https://api.openstreetmap.org' + path);
done();
});
});
});


Expand Down

0 comments on commit c4d1390

Please sign in to comment.