##// END OF EJS Templates
Use 501 error code instead of 400
Jessica B. Hamrick -
Show More
@@ -1,122 +1,122 b''
1 1 """Tornado handlers for the sessions web service."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import json
7 7
8 8 from tornado import web
9 9
10 10 from ...base.handlers import IPythonHandler, json_errors
11 11 from IPython.utils.jsonutil import date_default
12 12 from IPython.html.utils import url_path_join, url_escape
13 13 from IPython.kernel.kernelspec import NoSuchKernel
14 14
15 15
16 16 class SessionRootHandler(IPythonHandler):
17 17
18 18 @web.authenticated
19 19 @json_errors
20 20 def get(self):
21 21 # Return a list of running sessions
22 22 sm = self.session_manager
23 23 sessions = sm.list_sessions()
24 24 self.finish(json.dumps(sessions, default=date_default))
25 25
26 26 @web.authenticated
27 27 @json_errors
28 28 def post(self):
29 29 # Creates a new session
30 30 #(unless a session already exists for the named nb)
31 31 sm = self.session_manager
32 32 cm = self.contents_manager
33 33 km = self.kernel_manager
34 34
35 35 model = self.get_json_body()
36 36 if model is None:
37 37 raise web.HTTPError(400, "No JSON data provided")
38 38 try:
39 39 name = model['notebook']['name']
40 40 except KeyError:
41 41 raise web.HTTPError(400, "Missing field in JSON data: notebook.name")
42 42 try:
43 43 path = model['notebook']['path']
44 44 except KeyError:
45 45 raise web.HTTPError(400, "Missing field in JSON data: notebook.path")
46 46 try:
47 47 kernel_name = model['kernel']['name']
48 48 except KeyError:
49 49 self.log.debug("No kernel name specified, using default kernel")
50 50 kernel_name = None
51 51
52 52 # Check to see if session exists
53 53 if sm.session_exists(name=name, path=path):
54 54 model = sm.get_session(name=name, path=path)
55 55 else:
56 56 try:
57 57 model = sm.create_session(name=name, path=path, kernel_name=kernel_name)
58 58 except NoSuchKernel:
59 59 msg = ("The '%s' kernel is not available. Please pick another "
60 60 "suitable kernel instead, or install that kernel." % kernel_name)
61 61 status_msg = 'Kernel not found'
62 62 msg = dict(full=msg, short=status_msg)
63 raise web.HTTPError(400, json.dumps(msg))
63 raise web.HTTPError(501, json.dumps(msg))
64 64
65 65 location = url_path_join(self.base_url, 'api', 'sessions', model['id'])
66 66 self.set_header('Location', url_escape(location))
67 67 self.set_status(201)
68 68 self.finish(json.dumps(model, default=date_default))
69 69
70 70 class SessionHandler(IPythonHandler):
71 71
72 72 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
73 73
74 74 @web.authenticated
75 75 @json_errors
76 76 def get(self, session_id):
77 77 # Returns the JSON model for a single session
78 78 sm = self.session_manager
79 79 model = sm.get_session(session_id=session_id)
80 80 self.finish(json.dumps(model, default=date_default))
81 81
82 82 @web.authenticated
83 83 @json_errors
84 84 def patch(self, session_id):
85 85 # Currently, this handler is strictly for renaming notebooks
86 86 sm = self.session_manager
87 87 model = self.get_json_body()
88 88 if model is None:
89 89 raise web.HTTPError(400, "No JSON data provided")
90 90 changes = {}
91 91 if 'notebook' in model:
92 92 notebook = model['notebook']
93 93 if 'name' in notebook:
94 94 changes['name'] = notebook['name']
95 95 if 'path' in notebook:
96 96 changes['path'] = notebook['path']
97 97
98 98 sm.update_session(session_id, **changes)
99 99 model = sm.get_session(session_id=session_id)
100 100 self.finish(json.dumps(model, default=date_default))
101 101
102 102 @web.authenticated
103 103 @json_errors
104 104 def delete(self, session_id):
105 105 # Deletes the session with given session_id
106 106 sm = self.session_manager
107 107 sm.delete_session(session_id)
108 108 self.set_status(204)
109 109 self.finish()
110 110
111 111
112 112 #-----------------------------------------------------------------------------
113 113 # URL to handler mappings
114 114 #-----------------------------------------------------------------------------
115 115
116 116 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
117 117
118 118 default_handlers = [
119 119 (r"/api/sessions/%s" % _session_id_regex, SessionHandler),
120 120 (r"/api/sessions", SessionRootHandler)
121 121 ]
122 122
General Comments 0
You need to be logged in to leave comments. Login now