[shotwell] Add OAuth1.UploadTransaction base class



commit c39348626287522b83abf1b2ce7a9eec9bf07f69
Author: Jens Georg <mail jensge org>
Date:   Thu Nov 9 00:42:17 2017 +0100

    Add OAuth1.UploadTransaction base class

 plugins/common/RESTSupport.vala                   |   60 ++++++++++++++++++--
 plugins/shotwell-publishing/FlickrPublishing.vala |   48 ++---------------
 plugins/shotwell-publishing/TumblrPublishing.vala |   55 ++-----------------
 3 files changed, 64 insertions(+), 99 deletions(-)
---
diff --git a/plugins/common/RESTSupport.vala b/plugins/common/RESTSupport.vala
index 5820d8f..6f54f96 100644
--- a/plugins/common/RESTSupport.vala
+++ b/plugins/common/RESTSupport.vala
@@ -123,6 +123,18 @@ public class Argument {
         this.value = value;
     }
 
+    public static string serialize_list(Argument[] args) {
+        var builder = new StringBuilder("");
+
+        foreach (var arg in args) {
+            builder.append(arg.to_string());
+            builder.append("&");
+        }
+        builder.truncate(builder.len - 1);
+
+        return builder.str;
+    }
+
     public static int compare(Argument arg1, Argument arg2) {
         return strcmp(arg1.key, arg2.key);
     }
@@ -154,6 +166,7 @@ public class Transaction {
     public signal void chunk_transmitted(int bytes_written_so_far, int total_bytes);
     public signal void network_error(Spit.Publishing.PublishingError err);
     public signal void completed();
+
     
     public Transaction(Session parent_session, HttpMethod method = HttpMethod.POST) {
         // if our creator doesn't specify an endpoint url by using the Transaction.with_endpoint_url
@@ -910,12 +923,7 @@ namespace OAuth1 {
             Publishing.RESTSupport.Argument[] sorted_args =
                 Publishing.RESTSupport.Argument.sort(base_string_arguments);
 
-            string arguments_string = "";
-            for (int i = 0; i < sorted_args.length; i++) {
-                arguments_string += (sorted_args[i].key + "=" + sorted_args[i].value);
-                if (i < sorted_args.length - 1)
-                    arguments_string += "&";
-            }
+            var arguments_string = Argument.serialize_list(sorted_args);
 
             string? signing_key = null;
             if (access_phase_token_secret != null) {
@@ -1031,6 +1039,46 @@ namespace OAuth1 {
             base.execute();
         }
     }
+
+    public class UploadTransaction : Publishing.RESTSupport.UploadTransaction {
+        protected Publishing.RESTSupport.OAuth1.Session session;
+        private Publishing.RESTSupport.Argument[] auth_header_fields;
+
+        public UploadTransaction(Publishing.RESTSupport.OAuth1.Session session,
+                                 Spit.Publishing.Publishable publishable,
+                                 string endpoint_uri) {
+            base.with_endpoint_url(session, publishable, endpoint_uri);
+
+            this.auth_header_fields = new Publishing.RESTSupport.Argument[0];
+
+            add_authorization_header_field("oauth_nonce", session.get_oauth_nonce());
+            add_authorization_header_field("oauth_signature_method", "HMAC-SHA1");
+            add_authorization_header_field("oauth_version", "1.0");
+            add_authorization_header_field("oauth_callback", "oob");
+            add_authorization_header_field("oauth_timestamp", session.get_oauth_timestamp());
+            add_authorization_header_field("oauth_consumer_key", session.get_consumer_key());
+            add_authorization_header_field("oauth_token", session.get_access_phase_token());
+        }
+
+        public void add_authorization_header_field(string key, string value) {
+            auth_header_fields += new Publishing.RESTSupport.Argument(key, value);
+        }
+
+        public string get_authorization_header_string() {
+            return "OAuth " + Argument.serialize_list(auth_header_fields);
+        }
+
+        public void authorize() {
+            session.sign_transaction(this);
+
+            string authorization_header = get_authorization_header_string();
+
+            debug("executing upload transaction: authorization header string = '%s'",
+                    authorization_header);
+            add_header("Authorization", authorization_header);
+
+        }
+    }
 }
 
 }
diff --git a/plugins/shotwell-publishing/FlickrPublishing.vala 
b/plugins/shotwell-publishing/FlickrPublishing.vala
index 4ef642e..a31c809 100644
--- a/plugins/shotwell-publishing/FlickrPublishing.vala
+++ b/plugins/shotwell-publishing/FlickrPublishing.vala
@@ -502,27 +502,15 @@ internal class AccountInfoFetchTransaction : Publishing.RESTSupport.OAuth1.Trans
     }
 }
 
-private class UploadTransaction : Publishing.RESTSupport.UploadTransaction {
+private class UploadTransaction : Publishing.RESTSupport.OAuth1.UploadTransaction {
     private PublishingParameters parameters;
-    private Publishing.RESTSupport.OAuth1.Session session;
-    private Publishing.RESTSupport.Argument[] auth_header_fields;
 
     public UploadTransaction(Publishing.RESTSupport.OAuth1.Session session, PublishingParameters parameters,
         Spit.Publishing.Publishable publishable) {
-        base.with_endpoint_url(session, publishable, "https://api.flickr.com/services/upload";);
+        base(session, publishable, "https://api.flickr.com/services/upload";);
 
         this.parameters = parameters;
-        this.session = session;
-        this.auth_header_fields = new Publishing.RESTSupport.Argument[0];
-
-        add_authorization_header_field("oauth_nonce", session.get_oauth_nonce());
-        add_authorization_header_field("oauth_signature_method", "HMAC-SHA1");
-        add_authorization_header_field("oauth_version", "1.0");
-        add_authorization_header_field("oauth_callback", "oob");
-        add_authorization_header_field("oauth_timestamp", session.get_oauth_timestamp());
-        add_authorization_header_field("oauth_consumer_key", session.get_consumer_key());
-        add_authorization_header_field("oauth_token", session.get_access_phase_token());
-        
+
         add_argument("is_public", ("%d".printf(parameters.visibility_specification.everyone_level)));
         add_argument("is_friend", ("%d".printf(parameters.visibility_specification.friends_level)));
         add_argument("is_family", ("%d".printf(parameters.visibility_specification.family_level)));
@@ -542,35 +530,9 @@ private class UploadTransaction : Publishing.RESTSupport.UploadTransaction {
 
         set_binary_disposition_table(disposition_table);
     }
-    
-    public void add_authorization_header_field(string key, string value) {
-        auth_header_fields += new Publishing.RESTSupport.Argument(key, value);
-    }
-    
-    public string get_authorization_header_string() {
-        string result = "OAuth ";
-        
-        for (int i = 0; i < auth_header_fields.length; i++) {
-            result += auth_header_fields[i].key;
-            result += "=";
-            result += ("\"" + auth_header_fields[i].value + "\"");
-            
-            if (i < auth_header_fields.length - 1)
-                result += ", ";
-        }
-        
-        return result;
-    }
-    
+
     public override void execute() throws Spit.Publishing.PublishingError {
-        session.sign_transaction(this);
-        
-        string authorization_header = get_authorization_header_string();
-        
-        debug("executing upload transaction: authorization header string = '%s'",
-            authorization_header);
-        add_header("Authorization", authorization_header);
-        
+        this.authorize();
         base.execute();
     }
 }
diff --git a/plugins/shotwell-publishing/TumblrPublishing.vala 
b/plugins/shotwell-publishing/TumblrPublishing.vala
index 870e0c1..2518b01 100644
--- a/plugins/shotwell-publishing/TumblrPublishing.vala
+++ b/plugins/shotwell-publishing/TumblrPublishing.vala
@@ -574,11 +574,7 @@ namespace Publishing.Tumblr {
             }
         }
 
-        internal class UploadTransaction : Publishing.RESTSupport.UploadTransaction {
-            private Publishing.RESTSupport.OAuth1.Session session;
-            private Publishing.RESTSupport.Argument[] auth_header_fields;
-
-
+        internal class UploadTransaction : Publishing.RESTSupport.OAuth1.UploadTransaction {
             //Workaround for Soup.URI.encode() to support binary data (i.e. string with \0)
             private string encode( uint8[] data ){
                 var s = new StringBuilder();
@@ -599,41 +595,11 @@ namespace Publishing.Tumblr {
 
             public UploadTransaction(Publishing.RESTSupport.OAuth1.Session 
session,Spit.Publishing.Publishable publishable, string blog_url)  {
                 debug("Init upload transaction");
-                base.with_endpoint_url(session, 
publishable,"https://api.tumblr.com/v2/blog/%s/post".printf(blog_url) );
-                this.session = session;
-
-            }
-
+                base(session, publishable,"https://api.tumblr.com/v2/blog/%s/post".printf(blog_url) );
 
-
-            public void add_authorization_header_field(string key, string value) {
-                auth_header_fields += new Publishing.RESTSupport.Argument(key, value);
-            }
-
-            public string get_authorization_header_string() {
-                string result = "OAuth ";
-
-                for (int i = 0; i < auth_header_fields.length; i++) {
-                    result += auth_header_fields[i].key;
-                    result += "=";
-                    result += ("\"" + auth_header_fields[i].value + "\"");
-
-                    if (i < auth_header_fields.length - 1)
-                        result += ", ";
-                }
-
-                return result;
             }
 
             public override void execute() throws Spit.Publishing.PublishingError {
-                add_authorization_header_field("oauth_nonce", session.get_oauth_nonce());
-                add_authorization_header_field("oauth_signature_method", "HMAC-SHA1");
-                add_authorization_header_field("oauth_version", "1.0");
-                add_authorization_header_field("oauth_timestamp", session.get_oauth_timestamp());
-                add_authorization_header_field("oauth_consumer_key", session.get_consumer_key());
-                add_authorization_header_field("oauth_token", session.get_access_phase_token());
-
-
                 string payload;
                 size_t payload_length;
                 try {
@@ -659,24 +625,13 @@ namespace Publishing.Tumblr {
 
                 }
 
-
-                session.sign_transaction(this);
-
-                string authorization_header = get_authorization_header_string();
-
-                debug("executing upload transaction: authorization header string = '%s'",
-                        authorization_header);
-                add_header("Authorization", authorization_header);
+                this.authorize();
 
                 Publishing.RESTSupport.Argument[] request_arguments = get_arguments();
                 assert(request_arguments.length > 0);
 
-                string request_data = "";
-                for (int i = 0; i < request_arguments.length; i++) {
-                    request_data += (request_arguments[i].key + "=" + request_arguments[i].value);
-                    if (i < request_arguments.length - 1)
-                        request_data += "&";
-                }
+                var request_data = Publishing.RESTSupport.Argument.serialize_list(request_arguments);
+
                 Soup.Message outbound_message = new Soup.Message( "POST", get_endpoint_url());
                 outbound_message.set_request("application/x-www-form-urlencoded", Soup.MemoryUse.COPY, 
request_data.data);
 


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