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