Move oauth details into connection

This commit is contained in:
John Firebaugh
2012-11-24 13:30:31 -04:00
parent 22e9eb39cc
commit efb521aeb5
4 changed files with 57 additions and 39 deletions
+49 -1
View File
@@ -2,7 +2,8 @@ iD.Connection = function() {
var apiURL = 'http://www.openstreetmap.org/api/0.6/',
connection = {},
refNodes = {},
user = {};
user = {},
oauth = iD.OAuth().setAPI(apiURL);
// Request data within the bbox from an external OSM server.
function bboxFromAPI(box, callback) {
@@ -88,9 +89,52 @@ iD.Connection = function() {
return iD.Graph(entities);
}
function authenticate(callback) {
return oauth.authenticate(callback);
}
function authenticated() {
return oauth.authenticated();
}
function createChangeset(modified) {
oauth.xhr({
method: 'PUT',
path: '/changeset/create',
options: { header: { 'Content-Type': 'text/xml' } },
content: iD.format.XML.changeset()
},
function (changeset_id) {
oauth.xhr({
method: 'POST',
path: '/changeset/' + changeset_id + '/upload',
options: { header: { 'Content-Type': 'text/xml' } },
content: iD.format.XML.osmChange(user.id, changeset_id, modified)
}, function () {
oauth.xhr({
method: 'PUT',
path: '/changeset/' + changeset_id + '/close'
}, function () {
alert('saved! ' + apiURL.replace('/api/0.6/', '/browse') + '/changeset/' + changeset_id);
});
});
});
}
function userDetails(callback) {
oauth.xhr({ method: 'GET', path: '/user/details' }, function(user_details) {
var u = user_details.getElementsByTagName('user')[0];
callback({
display_name: u.attributes.display_name.nodeValue,
id: u.attributes.id.nodeValue
});
});
}
connection.url = function(x) {
if (!arguments.length) return apiURL;
apiURL = x;
oauth.setAPI(x);
return connection;
};
@@ -103,6 +147,10 @@ iD.Connection = function() {
connection.bboxFromAPI = bboxFromAPI;
connection.wayFromAPI = wayFromAPI;
connection.loadFromURL = loadFromURL;
connection.userDetails = userDetails;
connection.authenticate = authenticate;
connection.authenticated = authenticated;
connection.createChangeset = createChangeset;
connection.objectData = objectData;
connection.apiURL = apiURL;
+1 -3
View File
@@ -1,4 +1,4 @@
iD.OAuth = function(map) {
iD.OAuth = function() {
var baseurl = 'http://api06.dev.openstreetmap.org',
apibase = 'http://api06.dev.openstreetmap.org/api/0.6',
oauth_secret = 'aMnOOCwExO2XYtRVWJ1bI9QOdqh1cay2UgpbhA6p',
@@ -90,7 +90,5 @@ iD.OAuth = function(map) {
return oauth;
};
map.oauth = oauth;
return oauth;
};
+5 -13
View File
@@ -11,9 +11,6 @@ var iD = function(container) {
var controller = iD.Controller(map);
var oauth = iD.OAuth(map)
.setAPI('http://api06.dev.openstreetmap.org/api/0.6');
var bar = container.append('div')
.attr('id', 'bar');
@@ -75,7 +72,7 @@ var iD = function(container) {
.attr('id', 'save')
.html("Save<small id='as-username'></small>")
.on('click', function() {
oauth.authenticate(function() {
connection.authenticate(function() {
map.commit();
});
});
@@ -136,15 +133,10 @@ var iD = function(container) {
});
}
if (oauth.authenticated()) {
oauth.xhr({ method: 'GET', path: '/user/details' }, function(user_details) {
var u = user_details.getElementsByTagName('user')[0];
connection.user({
display_name: u.attributes.display_name.nodeValue,
id: u.attributes.id.nodeValue
});
d3.select('.messages').text('logged in as ' +
connection.user().display_name);
if (connection.authenticated()) {
connection.userDetails(function(user_details) {
connection.user(user_details);
d3.select('.messages').text('logged in as ' + user_details.display_name);
});
}
};
+2 -22
View File
@@ -535,29 +535,9 @@ iD.Map = function(elem, connection) {
}
function commit() {
var modified = _.filter(history.graph().entities, function(e) {
connection.createChangeset(_.filter(history.graph().entities, function(e) {
return e.modified;
});
var userid = connection.user().id;
map.oauth.xhr({
method: 'PUT',
path: '/changeset/create',
options: { header: { 'Content-Type': 'text/xml' } },
content: iD.format.XML.changeset() }, function(changeset_id) {
map.oauth.xhr({
method: 'POST',
path: '/changeset/' + changeset_id + '/upload',
options: { header: { 'Content-Type': 'text/xml' } },
content: iD.format.XML.osmChange(userid, changeset_id, modified)
}, function() {
map.oauth.xhr({
method: 'PUT',
path: '/changeset/' + changeset_id + '/close'
}, function() {
alert('saved! ' + connection.url().replace('/api/0.6/', '/browse') + '/changeset/' + changeset_id);
});
});
});
}));
}
map.handleDrag = handleDrag;