This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mock_connect.py
331 lines (259 loc) · 8.4 KB
/
mock_connect.py
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
# Installation:
# virtualenv flask
# source flask/bin/activate
# pip install flask
# To run:
# FLASK_APP=mock_connect.py flask run --host=0.0.0.0
# Use the API key below (0123456789abcdef0123456789abcdef) in rsconnect-jupyter
import io
import tarfile
import uuid
from datetime import datetime
from json import dumps, loads
from functools import wraps
from pprint import pprint
# noinspection PyPackageRequirements
from flask import (
Flask,
Blueprint,
abort,
after_this_request,
g,
request,
url_for,
jsonify,
)
def error(code, reason):
def set_code(response):
response.status_code = code
return response
after_this_request(set_code)
return {"error": reason}
class IdGenerator(object):
def __init__(self):
self._value = 0
def next(self):
self._value = self._value + 1
return self._value
app = Flask(__name__)
apps, app_id_generator = {}, IdGenerator()
bundles, bundle_id_generator = {}, IdGenerator()
tasks, task_id_generator = {}, IdGenerator()
api_keys = {"0123456789abcdef0123456789abcdef": "admin"}
# noinspection SpellCheckingInspection
users = {
"admin": {
"username": "admin",
"active_time": "2018-08-30T23:49:18.421238194Z",
"first_name": "Super",
"last_name": "User",
"locked": False,
"privileges": [
"add_users",
"add_vanities",
"change_app_permissions",
"change_apps",
"change_groups",
"change_usernames",
"change_users",
"change_variant_schedule",
"create_groups",
"edit_run_as",
"edit_runtime",
"lock_users",
"publish_apps",
"remove_apps",
"remove_groups",
"remove_users",
"remove_vanities",
"view_app_settings",
"view_apps",
],
"guid": "29a74070-2c13-4ef9-a898-cfc6bcf0f275",
"user_role": "administrator",
"updated_time": "2018-08-29T19:25:23.68280816Z",
"confirmed": True,
"created_time": "2018-08-29T19:25:23.68280816Z",
"password": "",
"email": "[email protected]",
}
}
def authenticated(f):
@wraps(f)
def wrapper(*args, **kw):
auth = request.headers.get("Authorization")
if auth is None or not auth.startswith("Key "):
abort(401)
key = auth[4:]
if key not in api_keys:
abort(401)
g.user = users[api_keys[key]]
return f(*args, **kw)
return wrapper
def json(f):
@wraps(f)
def wrapper(*args, **kw):
return jsonify(f(*args, **kw))
return wrapper
def item_by_id(d):
def decorator(f):
@wraps(f)
def wrapper(object_id, *args, **kw):
item = d.get(object_id)
if item is None:
return dumps(error(404, "Not found"))
return f(item, *args, **kw)
return wrapper
return decorator
api = Blueprint("api", __name__)
@app.route("/")
def index():
return "<html><body>Welcome to Mock Connect!</body></html>"
@api.route("me")
@authenticated
@json
def me():
return g.user
def timestamp():
return datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
@api.route("applications", methods=["GET", "POST"])
@authenticated
@json
def applications():
if request.method == "POST":
connect_app = request.get_json(force=True)
name = connect_app.get("name")
if name and [existing_app for existing_app in apps.values() if existing_app.get("name") == name]:
return error(409, "An object with that name already exists.")
connect_app["id"] = app_id_generator.next()
connect_app["guid"] = str(uuid.uuid4())
connect_app["url"] = "{0}content/{1}".format(url_for("index", _external=True), connect_app["id"])
connect_app["owner_username"] = g.user.get("username")
connect_app["owner_first_name"] = g.user.get("first_name")
connect_app["owner_last_name"] = g.user.get("last_name")
connect_app["owner_email"] = g.user.get("email")
connect_app["owner_locked"] = g.user.get("locked")
connect_app["bundle_id"] = None
connect_app["needs_config"] = True
connect_app["access_type"] = None
connect_app["description"] = ""
connect_app["app_mode"] = None
connect_app["created_time"] = timestamp()
connect_app.setdefault("title", "")
apps[str(connect_app["id"])] = connect_app
return connect_app
else:
count = int(request.args.get("count", 10000))
search = request.args.get("search")
def match(app_to_match):
return search is None or (app_to_match.get("title") or "").startswith(search)
matches = list(filter(match, apps.values()))[:count]
return {
"count": len(matches),
"total": len(matches),
"applications": matches,
}
# noinspection PyUnresolvedReferences
@api.route("applications/<object_id>", methods=["GET", "POST"])
@authenticated
@json
@item_by_id(apps)
def application(connect_app):
if request.method == "GET":
return connect_app
else:
connect_app.update(request.get_json(force=True))
return connect_app
# noinspection PyUnresolvedReferences
@api.route("applications/<object_id>/config")
@authenticated
@json
@item_by_id(apps)
def config(connect_app):
return {"config_url": "{0}content/apps/{1}".format(url_for("index", _external=True), connect_app["id"])}
# noinspection PyUnresolvedReferences
@api.route("applications/<object_id>/upload", methods=["POST"])
@authenticated
@json
@item_by_id(apps)
def upload(connect_app):
bundle_id = bundle_id_generator.next()
ts = timestamp()
bundle = {
"id": bundle_id,
"app_id": connect_app["id"],
"created_time": ts,
"updated_time": ts,
}
bundles[bundle_id] = (bundle, request.data)
return bundle
def read_bundle_file(tarball, filename):
bio = io.BytesIO(tarball)
with tarfile.open("r:gz", fileobj=bio) as tar:
return tar.extractfile(filename).read()
def read_manifest(tarball):
manifest_data = read_bundle_file(tarball, "manifest.json").decode("utf-8")
return loads(manifest_data)
def read_html(tarball):
manifest = read_manifest(tarball)
meta = manifest["metadata"]
# noinspection SpellCheckingInspection
filename = meta.get("primary_html") or meta.get("entrypoint")
return read_bundle_file(tarball, filename).decode("utf-8")
app_modes = {
"static": 4,
"jupyter-static": 7,
"jupyter-voila": 16,
}
# noinspection PyUnresolvedReferences
@api.route("applications/<object_id>/deploy", methods=["POST"])
@authenticated
@json
@item_by_id(apps)
def deploy(connect_app):
bundle_id = request.get_json(force=True).get("bundle")
if bundle_id is None:
return error(400, "bundle_id is required") # message and status code probably wrong
if bundle_id not in bundles:
return error(404, "bundle %s not found" % bundle_id) # message and status code probably wrong
bundle, tarball = bundles[bundle_id]
manifest = read_manifest(tarball)
pprint(manifest)
old_app_mode = connect_app["app_mode"]
# noinspection SpellCheckingInspection
new_app_mode = app_modes[manifest["metadata"]["appmode"]]
if old_app_mode is not None and old_app_mode != new_app_mode:
return error(400, "Cannot change app mode once deployed") # message and status code probably wrong
connect_app["app_mode"] = new_app_mode
connect_app["bundle_id"] = bundle_id
connect_app["last_deployed_time"] = timestamp()
task_id = task_id_generator.next()
task = {
"id": task_id,
"user_id": 0,
"finished": True,
"code": 0,
"error": "",
"last_status": 0,
"status": ["Building static content", "Deploying static content"],
}
tasks[str(task_id)] = task
return task
# noinspection PyUnresolvedReferences
@api.route("tasks/<object_id>")
@authenticated
@json
@item_by_id(tasks)
def get_task(task):
return task
@api.route("server_settings")
@json
def server_settings():
return {"not_empty": True}
# noinspection PyUnresolvedReferences
@app.route("/content/apps/<object_id>")
@item_by_id(apps)
def content(connect_app):
bundle, tarball = bundles[connect_app["bundle_id"]]
return read_html(tarball)
app.register_blueprint(api, url_prefix="/__api__")