Refactor JSON functions

This commit is contained in:
Miguel de la Cruz 2022-06-01 00:02:51 +02:00
parent 23c1d56799
commit 17e4ed6783

View file

@ -45,13 +45,18 @@
"Returns a keyword from a string" "Returns a keyword from a string"
(read (concat ":" str))) (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" "Parses the JSON in the current buffer after a url-request"
(let ((json-object-type 'plist) (let ((json-object-type 'plist)
(json-array-type 'list) (json-array-type 'list)
(json-key-type 'keyword)) (json-key-type 'keyword))
(with-current-buffer (get-buffer-create "asdf")
(insert (format "%s" (buffer-string))))
(goto-char url-http-end-of-headers) (goto-char url-http-end-of-headers)
(json-read))) (json-read)))
@ -116,7 +121,7 @@ if the user is not present"
(beginning-of-buffer) (beginning-of-buffer)
(search-forward "Token: ") (search-forward "Token: ")
(let ((token (word-at-point)) (let ((token (word-at-point))
(response (mattermost-parse-json))) (response (mattermost-read-json)))
(setq mattermost-token token) (setq mattermost-token token)
(setq mattermost-user-id (plist-get response :id)) (setq mattermost-user-id (plist-get response :id))
(mattermost--connect-websocket) (mattermost--connect-websocket)
@ -131,7 +136,7 @@ if the user is not present"
(if mattermost-token (if mattermost-token
(add-to-list 'url-request-extra-headers `("Authorization" . ,(concat "Bearer " 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) (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) (defun mattermost-get-user (user-id)
"Returns the user information" "Returns the user information"
@ -154,7 +159,7 @@ if the user is not present"
;; ToDo: define this better ;; ToDo: define this better
(defvar mattermost-mode-map (defvar mattermost-mode-map
(let ((map (make-sparse-keymap))) (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) map)
"The keymap for mattermost-mode") "The keymap for mattermost-mode")
@ -201,7 +206,8 @@ user to check their status and select between them")
(unless (null mattermost-websocket) (unless (null mattermost-websocket)
(websocket-close mattermost-websocket)) (websocket-close mattermost-websocket))
(setq mattermost-token nil (setq mattermost-token nil
mattermost-user-id nil)) mattermost-user-id nil
mattermost-websocket nil))
(provide 'mattermost) (provide 'mattermost)
;;; mattermost.el ends here ;;; mattermost.el ends here