Add some asynchronous commands

* avandu.el (avandu--prep-params):
  (avandu--send-command-async): New functions.

  (avandu--send-command-sync): Renamed from `avandu--send-command'.
  Use `avandu--prep-params' to prepare the data sent to the server.

  (avandu-update-article):
  (avandu-feed-catchup):
  (avandu-logout): Use `avandu--send-command-async' to send data. We
  should get a report on its success, but we don't need to work with
  the result.
This commit is contained in:
Tom Willemsen 2012-08-22 22:05:16 +02:00
parent 6d056c6a63
commit 91e25431c4

View file

@ -342,7 +342,28 @@ as-is, or if it's a function return the result of that function."
(funcall avandu-password) (funcall avandu-password)
avandu-password)) avandu-password))
(defun avandu--send-command (data) (defun avandu--prep-params (data)
"Prepare DATA to be sent to Tiny Tiny RSS."
(json-encode (if avandu--session-id
(append `((sid . ,avandu--session-id))
data)
data)))
(defun avandu--send-command-async (data func)
"Send a command with parameters DATA to tt-rss asynchronously.
The current session-id is added to the request and then DATA is
passed on to `json-encode'.
DATA should be an association list with at least an OP value.
FUNC should be a callback function as defined by
`url-retrieve'."
(let* ((url-request-data (avandu--prep-params data))
(url-request-method "POST"))
(unless (url-retrieve avandu-tt-rss-api-url func)
(message "Complete."))))
(defun avandu--send-command-sync (data)
"Send a command with parameters DATA to tt-rss. The current "Send a command with parameters DATA to tt-rss. The current
session-id is added to the request and then DATA is passed on to session-id is added to the request and then DATA is passed on to
`json-encode'. `json-encode'.
@ -350,16 +371,11 @@ session-id is added to the request and then DATA is passed on to
DATA should be an association list with at least an OP value. DATA should be an association list with at least an OP value.
For example: For example:
(avandu--send-command '((op . \"isLoggedIn\"))) (avandu--send-command-sync '((op . \"isLoggedIn\")))
This function returns the result of `json-read' passed over the This function returns the result of `json-read' passed over the
returned json." returned json."
(let* ((url-request-data (let* ((url-request-data (avandu--prep-params data))
(json-encode
(if avandu--session-id
(append `((sid . ,avandu--session-id))
data)
data)))
(url-request-method "POST") (url-request-method "POST")
(buffer (url-retrieve-synchronously avandu-tt-rss-api-url)) (buffer (url-retrieve-synchronously avandu-tt-rss-api-url))
result) result)
@ -373,7 +389,7 @@ returned json."
(defun avandu-categories (&optional unread) (defun avandu-categories (&optional unread)
"Get the created categories. If UNREAD is non-nil only get "Get the created categories. If UNREAD is non-nil only get
categories with feeds with unread articles in them." categories with feeds with unread articles in them."
(avandu--send-command (avandu--send-command-sync
`((op . "getCategories") `((op . "getCategories")
,@(when unread `((unread_only . ,unread)))))) ,@(when unread `((unread_only . ,unread))))))
@ -389,7 +405,7 @@ There are a number of special category IDs:
-2 -- Labels -2 -- Labels
-3 -- All feeds, excluding virtual feeds (e.g. Labels and such) -3 -- All feeds, excluding virtual feeds (e.g. Labels and such)
-4 -- All feeds, including virtual feeds" -4 -- All feeds, including virtual feeds"
(avandu--send-command (avandu--send-command-sync
`((op . "getFeeds") `((op . "getFeeds")
,@(when category `((cat_id . ,category))) ,@(when category `((cat_id . ,category)))
,@(when unread `((unread_only . ,unread))) ,@(when unread `((unread_only . ,unread)))
@ -440,7 +456,7 @@ There are some special feed IDs:
(view-mode (plist-get plist :view-mode)) (view-mode (plist-get plist :view-mode))
(include-attachments (plist-get plist :include-attachments)) (include-attachments (plist-get plist :include-attachments))
(since-id (plist-get plist :since-id))) (since-id (plist-get plist :since-id)))
(avandu--send-command (avandu--send-command-sync
`((op . "getHeadlines") `((op . "getHeadlines")
(feed_id . ,feed-id) (feed_id . ,feed-id)
,@(when limit `((limit . ,limit))) ,@(when limit `((limit . ,limit)))
@ -472,18 +488,20 @@ FIELD should be one of:
3 -- Article Note 3 -- Article Note
When updating FIELD 3 DATA functions as the note's contents." When updating FIELD 3 DATA functions as the note's contents."
(avandu--send-command `((op . "updateArticle") (avandu--send-command-async `((op . "updateArticle")
(article_ids . ,article-ids) (article_ids . ,article-ids)
(mode . ,mode) (mode . ,mode)
(field . ,field) (field . ,field)
,@(when data `((data . ,data)))))) ,@(when data `((data . ,data))))
(lambda (status)
(message "Update done."))))
(defun avandu-get-article (article-ids) (defun avandu-get-article (article-ids)
"Get one or more articles from Tiny Tiny RSS with ARTICLE-IDS, "Get one or more articles from Tiny Tiny RSS with ARTICLE-IDS,
if you're using version 1.5.0 or higher this can also be a if you're using version 1.5.0 or higher this can also be a
comma-separated list of ids." comma-separated list of ids."
(avandu--send-command `((op . "getArticle") (avandu--send-command-sync `((op . "getArticle")
(article_id . ,article-ids)))) (article_id . ,article-ids))))
;; Commands ;; Commands
(defun avandu-browse-article () (defun avandu-browse-article ()
@ -504,14 +522,16 @@ When updating FIELD 3 DATA functions as the note's contents."
(interactive) (interactive)
(let* ((button (button-at (point))) (let* ((button (button-at (point)))
(id (button-get button 'feed-id))) (id (button-get button 'feed-id)))
(avandu--send-command `((op . "catchupFeed") (avandu--send-command-async `((op . "catchupFeed")
(feed_id . ,id)))) (feed_id . ,id))
(lambda (status)
(message "Catch-up complete."))))
(revert-buffer)) (revert-buffer))
(defun avandu-logged-in-p () (defun avandu-logged-in-p ()
"Send a request to tt-rss to see if we're (still) logged "Send a request to tt-rss to see if we're (still) logged
in. This function returns t if we are, or nil if we're not." in. This function returns t if we are, or nil if we're not."
(let* ((response (avandu--send-command '((op . "isLoggedIn")))) (let* ((response (avandu--send-command-sync '((op . "isLoggedIn"))))
(result (avu-prop response status))) (result (avu-prop response status)))
(if (eq result :json-false) (if (eq result :json-false)
nil nil
@ -526,7 +546,7 @@ otherwise."
(unless (and avandu-user avandu-password) (unless (and avandu-user avandu-password)
(avandu--get-credentials)) (avandu--get-credentials))
(let ((result (avandu--send-command (let ((result (avandu--send-command-sync
`((op . "login") `((op . "login")
(user . ,avandu-user) (user . ,avandu-user)
(password . ,(avandu--password)))))) (password . ,(avandu--password))))))
@ -539,7 +559,9 @@ otherwise."
(defun avandu-logout () (defun avandu-logout ()
"Logout from Tiny Tiny RSS." "Logout from Tiny Tiny RSS."
(interactive) (interactive)
(avandu--send-command '((op . "logout"))) (avandu--send-command-async '((op . "logout"))
(lambda (status)
(message "Logged out.")))
(avandu--clear-data)) (avandu--clear-data))
(defun avandu-mark-article-read (id) (defun avandu-mark-article-read (id)
@ -568,7 +590,7 @@ BUTTON is nil, try to use a button at `point'."
feeds." feeds."
(interactive) (interactive)
(avandu--check-login) (avandu--check-login)
(let* ((result (avandu--send-command '((op . "getUnread")))) (let* ((result (avandu--send-command-sync '((op . "getUnread"))))
(count (avu-prop result unread))) (count (avu-prop result unread)))
(when (called-interactively-p 'any) (when (called-interactively-p 'any)
@ -599,7 +621,8 @@ feeds."
(defun avandu-tt-rss-api-level () (defun avandu-tt-rss-api-level ()
"Get the API level of your Tiny Tiny RSS instance." "Get the API level of your Tiny Tiny RSS instance."
(interactive) (interactive)
(let ((level (avu-prop (avandu--send-command '((op . "getApiLevel"))) (let ((level (avu-prop (avandu--send-command-sync
'((op . "getApiLevel")))
level))) level)))
(when (called-interactively-p 'any) (when (called-interactively-p 'any)
(message "API Level: %d" level)) (message "API Level: %d" level))
@ -609,7 +632,8 @@ feeds."
(defun avandu-tt-rss-version () (defun avandu-tt-rss-version ()
"Get the version of your Tiny Tiny RSS instance." "Get the version of your Tiny Tiny RSS instance."
(interactive) (interactive)
(let ((version (avu-prop (avandu--send-command '((op . "getVersion"))) (let ((version (avu-prop (avandu--send-command-sync
'((op . "getVersion")))
version))) version)))
(when (called-interactively-p 'any) (when (called-interactively-p 'any)
(message "Tiny Tiny RSS Version: %s" version)) (message "Tiny Tiny RSS Version: %s" version))