Check if nbviewer URL would show an error
If the nbviewer widget would show a 404 or other error we don't want to
display it. We check the status before rendering the template by making
a HEAD request to nbviewer, if the status code is >=400 we don't show
the preview.
"""
Main handler classes for requests
"""
+from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.web import HTTPError, authenticated
from tornado.httputil import url_concat
from tornado.log import app_log
@@ -15,6 +16,7 @@ SPEC_NAMES = {
"zenodo": "Zenodo"
}
+
class MainHandler(BaseHandler):
"""Main handler for requests"""
@@ -35,7 +37,7 @@ class ParameterizedMainHandler(BaseHandler):
"""Main handler that allows different parameter settings"""
@authenticated
- def get(self, provider_prefix, _unescaped_spec):
+ async def get(self, provider_prefix, _unescaped_spec):
prefix = '/v2/' + provider_prefix
spec = self.get_spec_from_request(prefix)
spec = spec.rstrip("/")
@@ -69,6 +71,18 @@ class ParameterizedMainHandler(BaseHandler):
blob_or_tree = 'blob' if filepath else 'tree'
nbviewer_url = f'{nbviewer_url}/{org}/{repo_name}/{blob_or_tree}/{ref}/{filepath}'
+
+ # Check if the nbviewer URL is valid and would display something
+ # useful to the reader, if not we don't show it
+ client = AsyncHTTPClient()
+ request = HTTPRequest(nbviewer_url,
+ method="HEAD",
+ user_agent="BinderHub",
+ )
+ response = await client.fetch(request, raise_error=False)
+ if response.code >= 400:
+ nbviewer_url = None
+
self.render_template(
"loading.html",
base_url=self.settings['base_url'],
