aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-http.el
diff options
context:
space:
mode:
authorJohnson Denen <johnson.denen@gmail.com>2017-04-08 07:23:07 -0400
committerjdenen <Johnson.Denen@ascenaretail.com>2017-04-09 08:55:36 -0400
commit96f4b3627b3cbb38b28ba07206fb6196e71e212a (patch)
treed4229a2dbfa8890fbddf6990beefe5498d9c524f /lisp/mastodon-http.el
parent1457f9f5cd435d395b474e1cdc0949e387e0665c (diff)
Refactor authorization
Diffstat (limited to 'lisp/mastodon-http.el')
-rw-r--r--lisp/mastodon-http.el43
1 files changed, 36 insertions, 7 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index 22c8c3f..0e49c08 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -1,3 +1,5 @@
+(require 'mastodon)
+
(defun mastodon--api-for (endpoint)
"Returns Mastondon API URL for ENDPOINT."
(concat mastodon-instance-url "/api/" mastodon--api-version "/" endpoint))
@@ -18,17 +20,44 @@ Response buffer is passed to the CALLBACK function."
"&")))
(url-retrieve url callback)))
-(defun mastodon--response-json (status)
- "Returns JSON string from `mastodon--http-post' response buffer."
- (let ((resp (with-current-buffer (current-buffer)
- (buffer-substring-no-properties (point-min) (point-max)))))
+(defun mastodon--response-buffer ()
+ (with-current-buffer (current-buffer)
+ (buffer-substring-no-properties (point-min) (point-max))))
+
+(defun mastodon--response-body-substring (pattern)
+ (let ((resp (mastodon--response-buffer)))
(progn
- (string-match "\{.*\}" resp)
+ (string-match pattern resp)
(match-string 0 resp))))
-(defun mastodon--json-hash-table (status)
+(defun mastodon--response-match-p (pattern)
+ (let ((resp (mastodon--response-buffer)))
+ (string-match-p pattern resp)))
+
+(defun mastodon--response-status-p ()
+ (when (mastodon--response-match-p "^HTTP/1.*$") t))
+
+(defun mastodon--response-json ()
+ (mastodon--response-body-substring "\{.*\}"))
+
+(defun mastodon--response-code ()
+ (let* ((status-line (mastodon--response-body-substring "^HTTP/1.*$")))
+ (progn
+ (string-match "[0-9][0-9][0-9]" status-line)
+ (match-string 0 status-line))))
+
+(defun mastodon--json-hash-table ()
"Reads JSON string from `mastodon--response-json' into a hash table."
(let ((json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string))
- (json-read-from-string (mastodon--response-json status))))
+ (json-read-from-string (mastodon--response-json))))
+
+(defun mastodon--http-response-triage (status success)
+ (when (not (mastodon--response-status))
+ (mastodon--http-response-triage status))
+ (if (string-prefix-p "2" (mastodon--response-code))
+ (funcall success)
+ (switch-to-buffer (current-buffer))))
+
+(provide 'mastodon-http)