[gnome-maps/wip/osm-edit: 35/47] osmApi: Add code to upload objects



commit 3e6eef46055bbec93aa11334acbe2b0d7814d162
Author: Marcus Lundblad <ml update uu se>
Date:   Wed Feb 18 22:46:30 2015 +0100

    osmApi: Add code to upload objects

 src/osmConnection.js |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/osmEdit.js       |   30 ++++++++++++++++++++++-----
 src/osmNode.js       |    4 +++
 src/osmObject.js     |    3 ++
 src/osmRelation.js   |    4 +++
 src/osmWay.js        |    4 +++
 6 files changed, 92 insertions(+), 6 deletions(-)
---
diff --git a/src/osmConnection.js b/src/osmConnection.js
index c93aa2b..56d91b0 100644
--- a/src/osmConnection.js
+++ b/src/osmConnection.js
@@ -57,6 +57,8 @@ const OSMConnection = new Lang.Class({
        let request = new Soup.Message({ method: 'GET',
                                         uri: uri });
 
+       print('fetching object using URL: ' + url);
+       
        cancellable.connect((function() {
            this._session.cancel_message(request, Soup.STATUS_CANCELLED);
        }).bind(this));
@@ -157,12 +159,63 @@ const OSMConnection = new Lang.Class({
        let xml = object.toXML();
 
        print('about to upload object:\n' + xml + '\n');
+       let url = this._getCreateOrUpdateUrl(object);
+       let uri = new Soup.URI(url);
+       let msg = new Soup.Message({ method: 'PUT',
+                                    uri: uri });
+       msg.set_request('text/xml', Soup.MemoryUse.COPY, xml, xml.length);
+
+       print('uploading to URL: ' + url);
+       
+       this._session.queue_message(msg, (function(obj, message) {
+           print('got response: ' + message.status_code);
+           if (message.status_code !== Soup.KnownStatusCode.OK) {
+                callback(false, message.status_code, null);
+                return;
+            }
+
+            print ('data received: ' + message.response_body.data);
+           callback(true, message.status_code, message.response_body.data);
+        }));
+       
+    },
+
+    closeChangeset: function(changesetId, callback) {
+       let url = this._getCloseChangesetUrl(changesetId);
+       let uri = new Soup.URI(url);
+       let msg = new Soup.Message({ method: 'PUT',
+                                    uri: uri });
+
+       print('closing changeset with URL: ' + url);
+
+       this._session.queue_message(msg, (function(obj, message) {
+           print('got response: ' + message.status_code);
+           if (message.status_code !== Soup.KnownStatusCode.OK) {
+                callback(false, message.status_code);
+                return;
+            }
+
+            print ('data received: ' + message.response_body.data);
+           callback(true, message.status_code);
+        }));
     },
 
     _getOpenChangesetUrl: function() {
        return TEST_BASE_URL + '/' + API_VERSION + '/changeset/create';
     },
 
+    _getCloseChangesetUrl: function(changesetId) {
+       return TEST_BASE_URL + '/' + API_VERSION + '/changeset/' +
+           changesetId + '/close';
+    },
+ 
+    _getCreateOrUpdateUrl: function(object) {
+       if (object.id)
+           return TEST_BASE_URL + '/' + API_VERSION + '/' + object.type + '/' + object.id;
+       else
+           return TEST_BASE_URL + '/' + API_VERSION + '/' + object.type + '/create';
+    },
+
     _authenticate: function(session, msg, auth, retrying, user_data) {
        print('authenticating session');
        
diff --git a/src/osmEdit.js b/src/osmEdit.js
index 6bc88e7..cbde7d4 100644
--- a/src/osmEdit.js
+++ b/src/osmEdit.js
@@ -79,21 +79,39 @@ const OSMEditManager = new Lang.Class({
     uploadObject: function(object, comment, source, callback) {
        this._osmConnection.openChangeset(comment, source,
                                          function(success, status,
-                                                  changeset) {
+                                                  changesetId) {
                                              print('open changeset CB:');
                                              print('success: ' + success);
                                              print('status: ' + status);
-                                             print('changeset: ' + changeset);
+                                             print('changeset: ' + changesetId);
                                              if (success)
                                                  this._uploadObject(object,
-                                                                    changeset,
+                                                                    changesetId,
                                                                     callback);
                                              else
-                                                 callback(false, status, null);
+                                                 callback(false, status);
                                          }.bind(this));
     },
 
-    _uploadObject: function(object, changeset, callback) {
-       this._osmConnection.uploadObject(object, changeset, callback);
+    _uploadObject: function(object, changesetId, callback) {
+       this._osmConnection.uploadObject(object, changesetId,
+                                        function(success, status,
+                                                 response) {
+                                            print('upload object CB:');
+                                            print('success: ' + success);
+                                            print('status: ' + status);
+                                            print('response: ' + response);
+                                            if (success)
+                                                // TODO: set ID (if new object)
+                                                // or version, from response
+                                                this._closeChangeset(changesetId,
+                                                                     callback);
+                                            else
+                                                callback(false, status);
+                                        }.bind(this));
+    },
+
+    _closeChangeset: function(changesetId, callback) {
+       this._osmConnection.closeChangeset(changesetId, callback);
     }
 });
diff --git a/src/osmNode.js b/src/osmNode.js
index 8ec9683..4f4f0a8 100644
--- a/src/osmNode.js
+++ b/src/osmNode.js
@@ -43,6 +43,10 @@ const OSMNode = new Lang.Class({
        return this._lon;
     },
 
+    get type() {
+       return 'node';
+    },
+
     toXML: function() {
        let tags = this._serializeTagsToList();
        let attrs = this._serializeAttributes();
diff --git a/src/osmObject.js b/src/osmObject.js
index 3dbb864..3f13010 100644
--- a/src/osmObject.js
+++ b/src/osmObject.js
@@ -79,6 +79,9 @@ const OSMObject = new Lang.Class({
        delete this._tags[key];
     },
 
+    // Abstract
+    get type() { },
+    
     //Abstract
     toXML: function() { },
 
diff --git a/src/osmRelation.js b/src/osmRelation.js
index dbc4cb6..6f79926 100644
--- a/src/osmRelation.js
+++ b/src/osmRelation.js
@@ -37,6 +37,10 @@ const OSMRelation = new Lang.Class({
        return this._members;
     },
 
+    get type() {
+       return 'relation';
+    },
+
     toXML: function() {
        let tags = this._serializeTagsToList();
        let attrs = this._serializeAttributes();
diff --git a/src/osmWay.js b/src/osmWay.js
index 180a6ec..57dd66d 100644
--- a/src/osmWay.js
+++ b/src/osmWay.js
@@ -37,6 +37,10 @@ const OSMWay = new Lang.Class({
        return this._nodeRefs;
     },
 
+    get type() {
+       return 'way';
+    },
+
     toXML: function() {
        let tags = this._serializeTagsToList();
        let attrs = this._serializeAttributes();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]