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"
(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)))
@ -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