-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlon-api.el
344 lines (300 loc) · 12.2 KB
/
tlon-api.el
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
;;; tlon-api.el --- Make requests with the Babel APIs -*- lexical-binding: t; fill-column: 80 -*-
;; Copyright (C) 2025
;; Author: Pablo Stafforini
;; This file is NOT part of GNU Emacs.
;; This program 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.
;;
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Make requests with the Babel APIs.
;;; Code:
(require 'request)
(require 'tlon-core)
(require 'transient)
;;;; Variables
;; TODO: add `:repo-name' property, then construct `:route' dynamically with
;; `tlon-api-get-routes'
(defconst tlon-uqbar-api-routes
'((:route "update/babel-refs"
:type "POST"
:docstring "Apply CSL and regenerate BibTeX keys. Then run \"update/uqbar/es\" request.")
(:route "update/babel-refs/log"
:type "GET"
:docstring "Show log of \"update/babel-refs\" request.")
(:route "update/uqbar/%s"
:type "POST"
:docstring "Update `uqbar’ source files.")
(:route "update/uqbar/%s/log"
:type "GET"
:docstring "Show log of \"update/uqbar\" request.")
(:route "update/rebuild-frontend"
:type "POST"
:docstring "Rebuild `uqbar' frontend.")
(:route "update/rebuild-frontend/log"
:type "GET"
:docstring "Show log of \"update/rebuild-frontend\" request."))
"Routes for `uqbar' API requests.")
(defvar tlon-api-most-recent-log-buffer nil
"The name of the most recent log buffer.")
(defconst tlon-api-local-url
"https://local-dev.altruismoeficaz.net/"
"Local URL for the `uqbar' API.")
;;;; Functions
;;;###autoload
(defun tlon-api-request (route &optional force-update pop-to-buffer)
"Make a request for ROUTE with the `uqbar' API.
If FORCE-UPDATE is non-nil, or called with a prefix argument, force the update.
If POP-TO-BUFFER is non-nil, display the log buffer."
(interactive (list (tlon-select-api-route)
current-prefix-arg))
(let* ((route-url (concat (format "%sapi/%s" tlon-api-local-url route)
(when force-update "?force=true")))
(type (tlon-lookup (tlon-api-get-routes) :type :route route)))
(tlon-api-get-token
tlon-api-local-url
(lambda (access-token)
"Authenticate with ACCESS-TOKEN, then make request."
(if (not access-token)
(message "Failed to authenticate")
(request route-url
:type type
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " access-token)))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
"Print response, and possibly make new request depending on ROUTE."
(tlon-api-print-response route pop-to-buffer :data data)
(message
"`%s' request completed successfully. See the `Uqbar log' buffer for details." route)))))))))
(defun tlon-api-request-force (route)
"Make a force request for ROUTE with the `uqbar' API."
(interactive (list (tlon-select-api-route)))
(tlon-api-request route 'force))
;;;;;; Token
(defun tlon-api-get-token (site callback)
"Get API token for SITE.
CALLBACK is called with the token as its argument."
(let* ((data (tlon-api-get-credentials)))
(request (format "%sapi/auth/login" site)
:type "POST"
:headers '(("Content-Type" . "application/x-www-form-urlencoded"))
:data data
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
"Call CALLBACK with the access token in DATA."
(when data
(funcall callback (alist-get "access_token" data nil nil 'string=))))))))
;;;###autoload
(defun tlon-api-copy-token ()
"Copy API token to the kill ring."
(interactive)
(tlon-api-get-token tlon-api-local-url (lambda (access-token)
(kill-new access-token)
(message "API token copied to the kill ring."))))
;;;;;; Logs
;;;###autoload
(defun tlon-api-open-most-recent-log ()
"Open a buffer with the most recent log returned by the API."
(interactive)
(unless tlon-api-most-recent-log-buffer
(user-error "No log buffer to open"))
(if (get-buffer tlon-api-most-recent-log-buffer)
(pop-to-buffer tlon-api-most-recent-log-buffer)
(tlon-api-request tlon-api-most-recent-log-buffer nil 'pop-to-buffer)))
;;;###autoload
(defun tlon-api-open-local-log ()
"Open a buffer with the local log in `uqbar-api'."
(interactive)
(let ((file (file-name-concat (tlon-repo-lookup :dir :name "uqbar-api") "logs/uqbar-api.log")))
(find-file file)))
(cl-defun tlon-api-print-response (route pop-to-buffer &key data &allow-other-keys)
"Print DATA returned from API ROUTE.
If POP-TO-BUFFER is non-nil, display the response in a buffer."
(setq tlon-api-most-recent-log-buffer route)
(with-current-buffer (get-buffer-create tlon-api-most-recent-log-buffer)
(erase-buffer)
(insert (json-encode data))
(json-pretty-print-buffer)
(tlon-fix-source-filename-paths)
(tlon-make-paths-clickable)
(when pop-to-buffer
(pop-to-buffer (current-buffer)))))
(defun tlon-api-get-credentials ()
"Return a list of credentials for `uqbar' API requests."
(let ((username (tlon-user-lookup :github :name user-full-name))
(inhibit-message t))
(concat "username=" (url-hexify-string username)
"&password=" (url-hexify-string
(auth-source-pass-get 'secret
(concat "tlon/babel/altruismoeficaz.net/" username))))))
;;;;;; Routes
(defun tlon-api-get-routes ()
"Return the `uqbar' API routes reflecting the current translation language."
(mapcar
(lambda (x)
(if (and (listp x)
(stringp (plist-get x :route))
(string-match "%s" (plist-get x :route)))
(plist-put (copy-sequence x) :route (replace-regexp-in-string
"%s"
tlon-translation-language
(plist-get x :route)))
x))
(copy-sequence tlon-uqbar-api-routes)))
(defun tlon-select-api-route ()
"Prompt the user to select an API route from `tlon-uqbar-api-routes'."
(let* ((choices (mapcar (lambda (plist)
(let ((route (plist-get plist :route))
(docstring (plist-get plist :docstring)))
(cons (format "%-40.40s %-80.80s" route (propertize docstring 'face 'italic)) route)))
(tlon-api-get-routes)))
(user-choice (completing-read "Please select an API route: " choices nil t)))
(cdr (assoc user-choice choices))))
;;;;; Process output buffer
(declare-function json-mode "json-mode")
(defun tlon-fix-source-filename-paths (&optional buffer)
"Fix `:source_filename' paths in output log in BUFFER.
If BUFFER is nil, default to the current buffer."
(let ((buffer (or buffer (current-buffer))))
(with-current-buffer buffer
(goto-char (point-max))
(delete-trailing-whitespace)
(goto-char (point-min))
(let* ((json-array-type 'list)
(json-object-type 'alist)
(json-data (json-read))
;; Modify the JSON
(json-modified
(mapcar (lambda (json-object)
(when-let* ((old-filename (cdr (assoc 'source_filename json-object)))
(new-filename (file-name-concat paths-dir-tlon-repos old-filename)))
(setf (cdr (assoc 'source_filename json-object)) new-filename))
json-object)
json-data)))
(erase-buffer)
(insert (json-encode json-modified))
(json-pretty-print-buffer)
(json-mode)))))
(defun tlon-make-paths-clickable (&optional buffer)
"Make file paths in the current buffer clickable.
The paths and also be opened with RET.
If BUFFER is nil, default to the current buffer."
(let ((buffer (or buffer (or (current-buffer)))))
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(goto-address-mode 1)
(while (re-search-forward "\"\\([^\"]+\\)\"" nil t)
(let* ((match (match-string-no-properties 0))
(url (substring match 1 -1))
(path (when (file-exists-p url)
(abbreviate-file-name (expand-file-name url)))))
(when path
(make-button (match-beginning 0) (match-end 0)
'action (lambda (_) (find-file path))
'follow-link t))))
(local-set-key (kbd "<RET>") 'ffap)))))
;;;;; BibTeX data
(defun tlon-api-get-citation (key &optional csl)
"Get citation for BibTeX KEY in CSL style from the Babel API.
CSL is the citation style: it can be `long' (default), `short', `audio-long' or
`audio-short'.
If citation is not found, return nil."
(when-let* ((csl (or csl 'long))
(url (tlon-api-get-citation-url key csl))
(json (tlon-api-get-citation-json url)))
(let ((json-key (pcase csl
('long-audio 'long)
('short-audio 'short)
(_ csl))))
(alist-get json-key json))))
(defun tlon-api-get-citation-url (key csl)
"Return the URL for the citation with KEY in CSL style."
(let* ((string-formatter (file-name-concat tlon-api-local-url "api/citations/%s/%s"))
(type (pcase csl ((or 'long 'short) "text") ((or 'long-audio 'short-audio) "audio"))))
(format string-formatter key type)))
(defun tlon-api-get-citation-json (url)
"Return the JSON response from URL."
(let* ((command (format "curl -sS -X 'GET' \ '%s' \ -H 'accept: application/json'" url))
(output (shell-command-to-string command)))
(if (string-match "could not resolve host" output)
(user-error "Failed to get citation. Is your local environment set up correctly?")
(with-temp-buffer
(insert output)
(goto-char (point-min))
(json-read)))))
;;;;; Magit integration
;; TODO do this properly by getting request from `tlon-api-get-routes'
(defun tlon-magit-trigger-api-request (&rest _)
"Trigger appropriate request when a commit is pushed in Magit."
(let ((uqbar-es (tlon-repo-lookup :dir :name "uqbar-es"))
(babel-refs (tlon-repo-lookup :dir :name "babel-refs"))
route)
;; can’t be done with `pcase'
(cond
((string= default-directory uqbar-es)
(setq route "update/uqbar/es"))
((string= default-directory babel-refs)
(setq route "update/babel-refs")))
(when route
(run-with-timer
3 nil (lambda ()
"Run `tlon-api-request' once the push is expected to complete."
(tlon-api-request route))))))
(dolist (fun (list 'magit-push-current-to-upstream
'magit-push-current-to-pushremote))
(advice-add fun :after 'tlon-magit-trigger-api-request))
;;;;; File uploading
(declare-function files-extras-read-file "files-extras")
(defun tlon-upload-file-to-server (&optional file destination delete-after-upload)
"Upload FILE asynchronously to DESTINATION in server.
If DELETE-AFTER-UPLOAD is non-nil, delete FILE after uploading."
(interactive)
(let* ((file (or file (files-extras-read-file file)))
(file-sans-fir (file-name-nondirectory file))
(destination (or destination (read-directory-name "Destination: "))))
(start-process "scp-upload" "*scp-upload*"
"scp"
(expand-file-name file)
destination)
(message "Uploading `%s' to `%s'..." file-sans-fir destination)
(set-process-sentinel
(get-buffer-process "*scp-upload*")
(lambda (_ event)
(when (string= event "finished\n")
(when delete-after-upload
(delete-file file))
(message "`%s' successfully uploaded to `%s'." file-sans-fir destination))
(unless (string= event "finished\n")
(progn
(message "Upload failed: %s" event)
(display-buffer "*scp-upload*"))))))
(when (derived-mode-p 'dired-mode)
(revert-buffer)))
;;;;; Transient
;;;###autoload (autoload 'tlon-api-menu "tlon-api" nil t)
(transient-define-prefix tlon-api-menu ()
"`api' menu."
["Requests"
("q" "uqbar" tlon-api-request)
("Q" "uqbar force" tlon-api-request-force)
""
"Logs"
("l" "open most recent" tlon-api-open-most-recent-log)
("L" "open local" tlon-api-open-local-log)
""
"Misc"
("t" "copy access token" tlon-api-copy-token)])
(provide 'tlon-api)
;;; tlon-api.el ends here