Add Cristian Greco as uploader
[debian/pino.git] / src / rest_api_abstract.vala
index 7585d33..67dd551 100644 (file)
@@ -97,11 +97,29 @@ public abstract class RestAPIAbstract : Object {
        protected RestUrls urls;
        public Account? account;
        
-       private Session? session = null;
+       private SessionAsync session_async;
+       private SessionSync session_sync;
        
        public RestAPIAbstract(Account? _account) {
                urls = new RestUrls(ServiceType.UNKNOWN);
                set_auth(_account);
+
+               session_async = new SessionAsync();
+               session_sync = new SessionSync();
+
+               session_async.timeout = 30; //seconds
+               session_sync.timeout = 30; //seconds
+
+               //Basic HTTP authorization
+        session_async.authenticate.connect(http_auth);
+        session_sync.authenticate.connect(http_auth);
+       }
+
+       private void http_auth(Message msg, Soup.Auth auth, bool retrying) {
+               if(retrying)
+                               return;
+
+               auth.authenticate(account.login, account.password);
        }
        
        private void select_urls() {
@@ -160,6 +178,9 @@ public abstract class RestAPIAbstract : Object {
                        case 2:
                                throw new RestError.CODE("Connection problems: can't connect to the server.");
                        
+                       case 4:
+                               throw new RestError.CODE("Connection problems: timeout.");
+                       
                        case 400:
                                throw new RestError.CODE("%d Rate limiting: you have reached the limit requests.".printf(status_code));
                        
@@ -184,12 +205,6 @@ public abstract class RestAPIAbstract : Object {
                throw new RestError.CODE("Account is not found");
        }
        
-       public void stop() {
-               if(session != null) {
-                       session.abort();
-               }
-       }
-       
        public string make_request(owned string req_url, string method,
                HashTable<string, string> params = new HashTable<string, string>(str_hash, str_equal),
                bool async = true, int retry = 3) throws RestError {
@@ -197,88 +212,71 @@ public abstract class RestAPIAbstract : Object {
                if(account == null)
                        no_account();
                
-               if(method == "GET") { //set get-parameters
-                       string query = "";
-                       warning(params.size().to_string());
-                       if(params.size() > 0) {
-                               query = "?";
-                               
-                               int tmp_iter = 0;
-                               foreach(string key in params.get_keys()) {
-                                       query += Soup.form_encode(key, params.lookup(key));
-                                       
-                                       if(tmp_iter < params.size() - 1)
-                                               query += "&";
-                                       
-                                       tmp_iter++;
-                               }
-                       }
-                       req_url += query;
-               }
-               
                //send signal about all requests
         request("%s: %s".printf(method, req_url));
         
-        //Session session;
+        Session session;
         
                if(async)
-                       session = new SessionAsync();
+                       session = session_async;
                else
-                       session = new SessionSync();
-               
-        Message message = new Message(method, req_url);
-        message.set_http_version(HTTPVersion.1_1);
-        
-        MessageHeaders headers = new MessageHeaders(MessageHeadersType.MULTIPART);
-        headers.append("User-Agent", "%s/%s".printf(Config.APPNAME, Config.APP_VERSION));
-        
-        message.request_headers = headers;
-        
-        if(method != "GET") { //set post/delete-parameters
-               string body = form_encode_hash(params);
-                       message.set_request("application/x-www-form-urlencoded",
-                               MemoryUse.COPY, body, (int)body.size());
-               }
+                       session = session_sync;
                
-               //Basic HTTP authorization
-        session.authenticate += (sess, msg, auth, retrying) => {
-                       if (retrying) return;
-                       auth.authenticate(account.login, account.password);
-               };
+               Message message = form_request_new_from_hash(method, req_url, params);
+               message.request_headers.append("User-Agent", "%s/%s".printf(Config.APPNAME, Config.APP_VERSION));
                
+               debug("and one more");
                int status_code = 0;
                for(int i = 0; i < retry; i++) {
-                       status_code = (int)session.send_message(message);
-                       if(status_code == 200 || status_code == 401)
+                       debug("go into loop");
+                       try {
+                               status_code = (int)session.send_message(message);
+                       } catch(GLib.Error e) {
+                               debug("we got some error: %s", e.message);
+                               break;
+                       }
+                       debug("something recieve %d", status_code);
+                       if(status_code == 200 || status_code == 401 || status_code == 4)
                                break;
                }
-               
+               debug("...");
                if(status_code != 200)
                        reply_tracking(status_code);
+
+               debug("end of make_request");
                
-               return (string)message.response_body.flatten().data;
+               return (string)message.response_body.data;
        }
        
        /* check user for DM availability */
        public bool check_friendship(string screen_name,
-               bool just_friend_check = false) throws RestError {
+               bool just_friend_check = false) throws RestError, ParseError {
                
                string req_url = urls.friendship();
                
                var map = new HashTable<string, string>(str_hash, str_equal);
                map.insert("source_screen_name", account.login);
                map.insert("target_screen_name", screen_name);
-               warning(req_url);
+               debug(req_url);
                string data = make_request(req_url, "GET", map);
-               
-               return parse_friendship(data, just_friend_check);
+
+               Parser.init();
+               bool result = parse_friendship(data, just_friend_check);
+               Parser.cleanup();
+
+               return result;
        }
        
-       private bool parse_friendship(string data, bool just_friend_check = false) {
+       private bool parse_friendship(string data,
+               bool just_friend_check = false) throws ParseError {
+
                bool followed_by = false;
                bool following = false;
                
                Xml.Doc* xmlDoc = Xml.Parser.parse_memory(data, (int)data.size());
+               if(xmlDoc == null)
+                       throw new ParseError.CODE("Invalid XML data");
+               
                Xml.Node* rootNode = xmlDoc->get_root_element();
                
                Xml.Node* iter;