(no commit message)
[prss.git] / src / ttrss.c
index e64c7af..97f2803 100644 (file)
 #include <string.h>
 
 #include <json/json.h>
-#include <gtk/gtk.h>
 
-#include "phttp.h"
+#include "http.h"
 #include "ttrss.h"
 #include "url.h"
 
 static char *session_id;
 static char *session_url;
+static char *session_user;
+static char *session_pwd;
 
-static struct json_object *create_op(const char *op)
+void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
 {
-       struct json_object *j;
+       json_object_object_add(rq, k, json_object_new_string(str));
+}
+
+void ws_request_add_att_int(json_object *rq, const char *k, int v)
+{
+       json_object_object_add(rq, k, json_object_new_int(v));
+}
+
+struct json_object *ws_request_new(const char *op)
+{
+       struct json_object *rq;
+
+       rq = json_object_new_object();
 
-       j = json_object_new_object();
-       json_object_object_add(j, "op", json_object_new_string(op));
+       ws_request_add_att_str(rq, "op", op);
 
-       if (session_id && strcmp(op, "login"))
-               json_object_object_add(j,
-                                      "sid",
-                                      json_object_new_string(session_id));
+       if (session_id)
+               ws_request_add_att_str(rq, "sid", session_id);
 
-       return j;
+       return rq;
 }
 
-void ttrss_login(const char *url, const char *user, const char *password)
+void ws_init(const char *url, const char *user, const char *pwd)
 {
-       struct json_object *content, *rp, *error, *sid, *rq;
        char *tmp;
 
+       if (session_id)
+               session_id = NULL;
+
+       if (session_user)
+               free(session_user);
+       session_user = strdup(user);
+
+       if (session_pwd)
+               free(session_pwd);
+       session_pwd = strdup(pwd);
+
        if (session_url)
                free(session_url);
 
@@ -58,40 +78,105 @@ void ttrss_login(const char *url, const char *user, const char *password)
        strcpy(session_url, tmp);
        strcat(session_url, "/api/");
        free(tmp);
+}
+
+struct json_object *ws_reply_get_content(struct json_object *rp)
+{
+       return json_object_object_get(rp, "content");
+}
+
+struct json_object *ws_execute(struct json_object *rq)
+{
+       struct json_object *rp, *content;
+
+       rp = http_json_get(session_url, rq);
+
+       if (rp) {
+               content = ws_reply_get_content(rp);
+
+               if (content && !json_object_object_get(rp, "error")) {
+                       json_object_get(content);
+                       json_object_put(rp);
+                       return content;
+               }
+
+               json_object_put(rp);
+       }
+
+       return NULL;
+}
+
+int ws_get_api_version()
+{
+       struct json_object *rp, *rq;
+       int v;
+
+       rq = ws_request_new("getApiLevel");
 
-       rq = create_op("login");
-       json_object_object_add(rq, "user", json_object_new_string(user));
-       json_object_object_add(rq,
-                              "password",
-                              json_object_new_string(password));
+       rp = ws_execute(rq);
 
-       rp = post_json_object(session_url, rq);
        json_object_put(rq);
 
-       content = json_object_object_get(rp, "content");
-       if (!content) {
-               fprintf(stderr, "Login failed: no content\n");
-               return ;
+       if (rp) {
+               v = json_object_get_int(json_object_object_get(rp, "level"));
+
+               json_object_put(rp);
+       } else {
+               v = 0;
        }
 
-       error = json_object_object_get(content, "error");
-       if (error) {
-               fprintf(stderr, "Login failed\n");
-               return ;
+       return v;
+}
+
+char *ws_login()
+{
+       struct json_object *rq, *rp, *j;
+       char *str;
+
+       rq = ws_request_new("login");
+       ws_request_add_att_str(rq, "user", session_user);
+       ws_request_add_att_str(rq, "password", session_pwd);
+
+       rp = ws_execute(rq);
+       json_object_put(rq);
+
+       if (rp) {
+               j = json_object_object_get(rp, "session_id");
+               str = strdup(json_object_get_string(j));
+
+               json_object_put(rp);
+       } else {
+               str = NULL;
        }
 
-       sid = json_object_object_get(content, "session_id");
+       return str;
+}
 
-       if (session_id) {
+int ws_open_session()
+{
+       int version, result;
+
+       if (session_id)
                free(session_id);
-               session_id = NULL;
-       }
 
-       session_id = strdup(json_object_get_string(sid));
+       session_id = ws_login();
 
-       printf("Session id: %s\n", session_id);
+       if (session_id) {
+               version = ws_get_api_version();
+               printf("API version: %d\n", version);
+
+               if (version > 0) {
+                       result = 1;
+               } else {
+                       free(session_id);
+                       session_id = NULL;
+                       result = 0;
+               }
+       } else {
+               result =  0;
+       }
 
-       json_object_put(rp);
+       return result;
 }
 
 const char *ttrss_get_headline_content(struct headline *h)
@@ -101,11 +186,11 @@ const char *ttrss_get_headline_content(struct headline *h)
        printf("get_headlines %d\n", h->id);
 
        if (!h->content) {
-               rq = create_op("getArticle");
+               rq = ws_request_new("getArticle");
                json_object_object_add(rq, "article_id",
                                       json_object_new_int(h->id));
                
-               rp = post_json_object(session_url, rq);
+               rp = http_json_get(session_url, rq);
                
                json_object_put(rq);
                
@@ -137,10 +222,10 @@ static struct headline **get_headlines(int feed_id)
 
        printf("get_headlines %d\n", feed_id);
 
-       rq = create_op("getHeadlines");
+       rq = ws_request_new("getHeadlines");
        json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
 
        json_object_put(rq);
 
@@ -225,9 +310,9 @@ struct feed **ttrss_get_feeds()
 
        printf("ttrss_get_feeds\n");
 
-       rq = create_op("getFeeds");
+       rq = ws_request_new("getFeeds");
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
        json_object_put(rq);
 
        content = json_object_object_get(rp, "content");
@@ -284,12 +369,12 @@ void ttrss_set_article_unread(int id, int unread)
 
        printf("ttrss_set_article_unread %d %d\n", id, unread);
 
-       rq = create_op("updateArticle");
+       rq = ws_request_new("updateArticle");
        json_object_object_add(rq, "article_ids", json_object_new_int(id));
        json_object_object_add(rq, "field", json_object_new_int(2));
        json_object_object_add(rq, "mode", json_object_new_int(unread));
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
        
        json_object_put(rq);
        json_object_put(rp);