aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-http.el
blob: 0e49c088dde4b11680b2ff250c1f224f09a39a64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(require 'mastodon)

(defun mastodon--api-for (endpoint)
  "Returns Mastondon API URL for ENDPOINT."
  (concat mastodon-instance-url "/api/" mastodon--api-version "/" endpoint))

(defun mastodon--http-post (url callback args &optional headers)
  "Sends ARGS to URL as a POST request.

Response buffer is passed to the CALLBACK function."
  (let ((url-request-method "POST")
        (url-request-extra-headers
         (append '(("Content-Type" . "application/x-www-form-urlencoded")) headers))
        (url-request-data
         (mapconcat (lambda (arg)
                      (concat (url-hexify-string (car arg))
                              "="
                              (url-hexify-string (cdr arg))))
                    args
                    "&")))
    (url-retrieve url callback)))

(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 pattern resp)
      (match-string 0 resp))))

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

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