-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathein-query.el
223 lines (197 loc) · 10.4 KB
/
ein-query.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
;;; ein-query.el --- jQuery like interface on top of curl -*- lexical-binding: t -*-
;; Copyright (C) 2012- Takafumi Arakaki
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
;; This file is NOT part of GNU Emacs.
;; ein-query.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.
;; ein-query.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 ein-query.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'request)
(require 'url)
(require 'ein-core)
(require 'ein-log)
(defcustom ein:query-timeout 10000
"Default query timeout for HTTP access in millisecond."
:type '(choice (integer :tag "Timeout [ms]" 1000)
(const :tag "No timeout" nil))
:group 'ein)
(defvar ein:query-xsrf-cache (make-hash-table :test 'equal)
"Remember the last xsrf token by host.
This is a hack in case we catch cookie jar in transition.
The proper fix is to sempahore between competing curl processes.")
(defvar ein:query-authorization-tokens (make-hash-table :test 'equal)
"Jupyterhub authorization token by (host . username).")
(defun ein:query-get-cookies (host path-prefix)
"Return (:path :expire :name :value) for HOST, matching PATH-PREFIX."
(when-let ((filename (request--curl-cookie-jar)))
(with-temp-buffer
(insert-file-contents filename)
(cl-loop for (domain _flag path _secure _http-only expire name value)
in (request--netscape-cookie-parse)
when (and (string= domain host)
(cl-search path-prefix path))
collect `(:path ,path :expire ,expire :name ,name :value ,value)))))
(defun ein:query-prepare-header (url settings &optional securep)
"Ensure that REST calls to the jupyter server have the correct _xsrf argument."
(let* ((host (url-host (url-generic-parse-url url)))
(paths* (let* ((warning-minimum-level :emergency)
(warning-minimum-log-level :emergency)
(root-url (car (ein:notebooklist-parse-nbpath url))))
(if root-url
(let ((root-path (url-filename (url-generic-parse-url root-url))))
(unless (zerop (length root-path))
(list (file-name-as-directory root-path))))
(let* ((url* url)
(parsed-url* (url-generic-parse-url url*))
paths*)
(while (not (zerop (length (url-filename parsed-url*))))
(push (file-name-as-directory (url-filename parsed-url*)) paths*)
(setq url* (file-name-directory (directory-file-name url*))
parsed-url* (url-generic-parse-url url*)))
paths*))))
(paths (progn (cl-pushnew "/" paths* :test #'equal) (reverse paths*)))
(cookies (cl-some (lambda (path)
(request-cookie-alist host path securep))
paths))
(xsrf (or (cdr (assoc-string "_xsrf" cookies))
(gethash host ein:query-xsrf-cache)))
(key (ein:query-divine-authorization-tokens-key url))
(token (aand key
(gethash key ein:query-authorization-tokens)
(cons "Authorization" (format "token %s" it)))))
(setq settings (plist-put settings :headers
(append (plist-get settings :headers)
(list (cons "User-Agent" "Mozilla/5.0")))))
(when token
(setq settings (plist-put settings :headers
(append (plist-get settings :headers)
(list token)))))
(when xsrf
(setq settings (plist-put settings :headers
(append (plist-get settings :headers)
(list (cons "X-XSRFTOKEN" xsrf)))))
(setf (gethash host ein:query-xsrf-cache) xsrf))
(setq settings (plist-put settings :encoding 'binary))
settings))
(defun ein:query-divine-authorization-tokens-key (url)
"Infer semblance of jupyterhub root.
From https://hub.data8x.berkeley.edu/hub/user/806b3e7/notebooks/Untitled.ipynb,
get (\"hub.data8x.berkeley.edu\" . \"806b3e7\")"
(-when-let* ((parsed-url (url-generic-parse-url url))
(url-host (url-host parsed-url))
(slash-path (car (url-path-and-query parsed-url)))
(components (split-string slash-path "/" t)))
(awhen (member "user" components)
(cons url-host (cl-second it)))))
(cl-defun ein:query-singleton-ajax (url &rest settings
&key (timeout ein:query-timeout)
&allow-other-keys)
(if (executable-find request-curl)
(let ((request-backend 'curl))
(when timeout
(setq settings (plist-put settings :timeout (/ timeout 1000.0))))
(unless (plist-member settings :sync)
(setq settings (plist-put settings :sync ein:force-sync)))
(apply #'request (url-encode-url url) (ein:query-prepare-header url settings)))
(ein:display-warning
(format "The %s program was not found, aborting" request-curl)
:error)))
(defun ein:query-kernelspecs (url-or-port callback &optional iteration)
"Send for kernelspecs of URL-OR-PORT with CALLBACK arity 0 (just a semaphore)"
(setq iteration (or iteration 0))
(ein:query-singleton-ajax
(ein:url url-or-port "api/kernelspecs")
:type "GET"
:parser #'ein:json-read
:complete (apply-partially #'ein:query-kernelspecs--complete url-or-port)
:success (apply-partially #'ein:query-kernelspecs--success url-or-port callback)
:error (apply-partially #'ein:query-kernelspecs--error url-or-port callback iteration)))
(defun ein:normalize-kernelspec-language (name)
"Normalize the kernelspec language string"
(if (stringp name)
(replace-regexp-in-string "[ ]" "-" name)
name))
(cl-defun ein:query-kernelspecs--success (url-or-port callback
&key data _symbol-status _response
&allow-other-keys)
(let ((ks (list :default (plist-get data :default)))
(specs (ein:plist-iter (plist-get data :kernelspecs))))
(setf (gethash url-or-port *ein:kernelspecs*)
(ein:flatten (dolist (spec specs ks)
(let ((name (car spec))
(info (cdr spec)))
(push (list name (make-ein:$kernelspec :name (plist-get info :name)
:display-name (plist-get (plist-get info :spec)
:display_name)
:resources (plist-get info :resources)
:language (ein:normalize-kernelspec-language
(plist-get (plist-get info :spec)
:language))
:spec (plist-get info :spec)))
ks))))))
(when callback (funcall callback)))
(cl-defun ein:query-kernelspecs--error
(url-or-port callback iteration
&key data response error-thrown &allow-other-keys
&aux
(response-status (request-response-status-code response))
(hub-p (request-response-header response "x-jupyterhub-version")))
(if (< iteration 3)
(if (and hub-p (eq response-status 405))
(ein:query-kernelspecs--success url-or-port callback :data data)
(ein:log 'verbose "Retry kernelspecs #%s in response to %s"
iteration response-status)
(ein:query-kernelspecs url-or-port callback (1+ iteration)))
(ein:log 'error
"ein:query-kernelspecs--error %s: ERROR %s DATA %s"
url-or-port (car error-thrown) (cdr error-thrown))
(when callback (funcall callback))))
(cl-defun ein:query-kernelspecs--complete (_url-or-port &key data response &allow-other-keys
&aux (resp-string (format "STATUS: %s DATA: %s" (request-response-status-code response) data)))
(ein:log 'debug "ein:query-kernelspecs--complete %s" resp-string))
(defun ein:query-notebook-api-version (url-or-port callback)
"Get notebook version of URL-OR-PORT with CALLBACK arity 0 (a semaphore)."
(ein:query-singleton-ajax
(ein:url url-or-port "api/spec.yaml")
;; the melpa yaml package was taking too long, unfortunately
:parser (lambda ()
(if (re-search-forward "api\\s-+version: \\(\\S-+\\)"
nil t)
;; emacs-25.3 doesn't have the right string-trim
(string-remove-prefix
"\"" (string-remove-suffix
"\"" (match-string-no-properties 1)))
""))
:complete (apply-partially #'ein:query-notebook-api-version--complete
url-or-port callback)))
(cl-defun ein:query-notebook-api-version--complete
(url-or-port callback
&key data response
&allow-other-keys &aux
(resp-string (format "STATUS: %s DATA: %s"
(request-response-status-code response) data))
(hub-p (request-response-header response "x-jupyterhub-version")))
(ein:log 'debug "ein:query-notebook-api-version--complete %s" resp-string)
(if (not (zerop (string-to-number data)))
(setf (gethash url-or-port *ein:notebook-api-version*) data)
(if hub-p
(let ((key (ein:query-divine-authorization-tokens-key url-or-port)))
(remhash key ein:query-authorization-tokens)
(ein:display-warning
(format "Server for user %s requires start, aborting"
(or (cdr key) "unknown"))
:error)
(setq callback nil))
(ein:log 'warn "notebook api version currently unknowable")))
(when callback (funcall callback)))
(provide 'ein-query)
;;; ein-query.el ends here