Skip to content

Commit

Permalink
Correctly send authenticated requests to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Mar 22, 2017
1 parent 12bc3e6 commit 0aa9b33
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions spyder_notebook/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ def __init__(self, plugin, name):
layout.addWidget(self.find_widget)
self.setLayout(layout)

def add_token(self, url):
"""Make a login url with next_arg."""
token_url = url + '?token={}'.format(self.token)
return token_url

def register(self, server_info):
"""Register attributes that can be computed with the server info."""
# Path relative to the server directory
Expand All @@ -151,13 +156,11 @@ def register(self, server_info):
# Server token
self.token = server_info.get('token', '')

# Full url used to render the notebook as a web page
file_arg = url_path_join('/notebooks', url_escape(self.path))
file_arg = file_arg.replace('/', r'%2F')
url = url_path_join(self.server_url, 'notebooks',
url_escape(self.path))

self.file_url = url_path_join(self.server_url,
'login?token={}&next={}'.format(self.token,
file_arg))
# Set file url to load this notebook
self.file_url = self.add_token(url)

def go_to(self, url_or_text):
"""Go to page *address*"""
Expand Down Expand Up @@ -186,22 +189,26 @@ def save(self):

def shutdown_kernel(self):
"""Shutdown the kernel of the client."""
sessions_url = url_path_join(self.server_url, 'api/sessions')
sessions_url = self.add_token(url_path_join(self.server_url,
'api/sessions'))
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)
kernel_id = None

if os.name == 'nt':
path = self.path.replace('\\', '/')
else:
path = self.path

for session in sessions:
if session['notebook']['path'] == path:
kernel_id = session['kernel']['id']
break

if kernel_id:
delete_url = url_path_join(self.server_url,
'api/kernels/',
kernel_id)
delete_url = self.add_token(url_path_join(self.server_url,
'api/kernels/',
kernel_id))
delete_req = requests.delete(delete_url)
if delete_req.status_code != 204:
QMessageBox.warning(
Expand Down

0 comments on commit 0aa9b33

Please sign in to comment.