From 17e4ed67833ef18cb84ee38c4365969d1ce70a90 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 1 Jun 2022 00:02:51 +0200 Subject: [PATCH] Refactor JSON functions --- mattermost.el | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/mattermost.el b/mattermost.el index ead6f05..9ee4dee 100644 --- a/mattermost.el +++ b/mattermost.el @@ -45,13 +45,18 @@ "Returns a keyword from a string" (read (concat ":" str))) -(defun mattermost-parse-json () +(defun mattermost-json-parse-string (string) + "Parses a JSON string" + (with-temp-buffer + (insert string) + (beginning-of-buffer) + (mattermost-read-json))) + +(defun mattermost-read-json () "Parses the JSON in the current buffer after a url-request" (let ((json-object-type 'plist) (json-array-type 'list) (json-key-type 'keyword)) - (with-current-buffer (get-buffer-create "asdf") - (insert (format "%s" (buffer-string)))) (goto-char url-http-end-of-headers) (json-read))) @@ -88,8 +93,8 @@ if the user is not present" (defun mattermost--get-auth-challenge () "Returns the JSON representation of the websocket authentication challenge" (json-encode `(:seq 1 - :action "authentication_challenge" - :data (:token ,mattermost-token)))) + :action "authentication_challenge" + :data (:token ,mattermost-token)))) (defun mattermost--connect-websocket () "Returns a websocket configured to connect to the Mattermost @@ -116,7 +121,7 @@ if the user is not present" (beginning-of-buffer) (search-forward "Token: ") (let ((token (word-at-point)) - (response (mattermost-parse-json))) + (response (mattermost-read-json))) (setq mattermost-token token) (setq mattermost-user-id (plist-get response :id)) (mattermost--connect-websocket) @@ -131,7 +136,7 @@ if the user is not present" (if mattermost-token (add-to-list 'url-request-extra-headers `("Authorization" . ,(concat "Bearer " mattermost-token)))) (with-current-buffer (url-retrieve-synchronously (concat mattermost-server-url "/api/v4" url) t) - (mattermost-parse-json)))) + (mattermost-read-json)))) (defun mattermost-get-user (user-id) "Returns the user information" @@ -154,7 +159,7 @@ if the user is not present" ;; ToDo: define this better (defvar mattermost-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "C-c f") '(lambda () (interactive) (message "keyyyy"))) + (define-key map (kbd "C-c f") #'(lambda () (interactive) (message "keyyyy"))) map) "The keymap for mattermost-mode") @@ -201,7 +206,8 @@ user to check their status and select between them") (unless (null mattermost-websocket) (websocket-close mattermost-websocket)) (setq mattermost-token nil - mattermost-user-id nil)) + mattermost-user-id nil + mattermost-websocket nil)) (provide 'mattermost) ;;; mattermost.el ends here