blob: e6e5549ee68017e2463888ddf9709e433e5bc053 (
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
;;; mastodon-http.el --- HTTP request/response functions for mastodon.el
;; Copyright (C) 2017 Johnson Denen
;; Author: Johnson Denen <johnson.denen@gmail.com>
;; Homepage: https://github.com/jdenen/mastodon.el
;; This file is not part of GNU Emacs.
;; This file is part of mastodon.el.
;; mastodon.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; mastodon.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with mastodon.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; mastodon-http.el provides HTTP request/response functions.
;;; Code:
(require 'json)
(defgroup mastodon-http nil
"HTTP requests and responses for Mastodon."
:prefix "mastodon-http-"
:group 'mastodon)
(defun mastodon--api-for (endpoint)
"Return Mastondon API URL for ENDPOINT."
(concat mastodon-instance-url "/api/" mastodon--api-version "/" endpoint))
(defun mastodon--http-post (url callback args &optional headers)
"This function should be phased out in favor of `mastodon-http--post'.
Make POST request to URL.
Response buffer is passed to CALLBACK function.
ARGS and HEADERS alist arguments are part of the POST request."
(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 ()
"Capture response buffer content as string."
(with-current-buffer (current-buffer)
(buffer-substring-no-properties (point-min) (point-max))))
(defun mastodon--response-body-substring (pattern)
"Return substring matching PATTERN from `mastodon--response-buffer'."
(let ((resp (mastodon--response-buffer)))
(progn
(string-match pattern resp)
(match-string 0 resp))))
(defun mastodon--response-match-p (pattern)
"Return non-nil if `mastodon--response-buffer' matches PATTERN."
(let ((resp (mastodon--response-buffer)))
(string-match-p pattern resp)))
(defun mastodon--response-status-p ()
"Return non-nil if `mastodon--response-buffer' has an HTTP Response Status-Line."
(when (mastodon--response-match-p "^HTTP/1.*$") t))
(defun mastodon--response-json ()
"Return string of JSON response body from `mastodon--response-buffer'."
(mastodon--response-body-substring "\{.*\}"))
(defun mastodon--response-code ()
"Return HTTP Response Status Code from `mastodon--response-buffer'."
(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 ()
"Read JSON 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)
"Callback function to triage an HTTP response.
Recursively waits for `mastodon--response-buffer' to contain a Status-Line.
STATUS is passed by `url-retrieve'.
SUCCESS is a function called on a 2XX level response code.
If response code is not 2XX, switches to the response buffer created by `url-retrieve'."
(when (not (mastodon--response-status-p))
(mastodon--http-response-triage status))
(if (string-prefix-p "2" (mastodon--response-code))
(funcall success)
(switch-to-buffer (current-buffer))))
(defun mastodon-http--triage (response success)
(let ((status (with-current-buffer response
(mastodon--response-code))))
(if (string-prefix-p "2" status)
(funcall success)
(switch-to-buffer response))))
(defun mastodon-http--post (url args headers)
"POST synchronously to URL with ARGS and HEADERS.
Authorization header is included by default."
(let ((url-request-method "POST")
(url-request-data
(when args
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")))
(url-request-extra-headers
`(("Authorization" . ,(concat "Bearer " mastodon-auth--token))
,headers)))
(with-temp-buffer
(url-retrieve-synchronously url))))
(defun mastodon-http--get (url)
"Make GET request to URL.
Pass response buffer to CALLBACK function."
(let ((url-request-method "GET")
(url-request-extra-headers
`(("Authorization" . ,(concat "Bearer "
(mastodon-auth--access-token))))))
(url-retrieve-synchronously url)))
(defun mastodon-http--get-json (url)
"Make GET request to URL. Return JSON response vector."
(let ((json-vector
(with-current-buffer (mastodon-http--get url)
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(let ((json-string (buffer-substring-no-properties (point) (point-max))))
(json-read-from-string json-string)))))
json-vector))
(provide 'mastodon-http)
;;; mastodon-http.el ends here
|