aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-http.el
blob: 22c8c3fb1ed79a441f55d7279d6d9c5d9a1bb8bd (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
(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-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)))))
    (progn
      (string-match "\{.*\}" resp)
      (match-string 0 resp))))

(defun mastodon--json-hash-table (status)
  "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))))