Skip to content

Commit d927c4c

Browse files
authored
Make openapi optional and lazy (#253)
1 parent 0096568 commit d927c4c

File tree

7 files changed

+47
-11
lines changed

7 files changed

+47
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ accommodate creating JupyterLab-like applications from a more limited scope.
1414

1515
`pip install jupyterlab_server`
1616

17+
To include optional `openapi` dependencies, use:
18+
19+
`pip install jupyterlab_server[openapi]`
20+
21+
To include optional `pytest_plugin` dependencies, use:
22+
23+
`pip install jupyterlab_server[test]`
24+
1725
## Usage
1826

1927
See the full documentation for [API docs](https://jupyterlab-server.readthedocs.io/en/stable/api/index.html) and [REST endpoint descriptions](https://jupyterlab-server.readthedocs.io/en/stable/api/rest.html).

docs/source/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ JupyterLab Server API Reference:
1414
handlers
1515
process
1616
rest
17+
spec

docs/source/api/spec.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=============
2+
OpenAPI Specs
3+
=============
4+
5+
Module: :mod:`jupyterlab_server.spec`
6+
=====================================
7+
8+
.. automodule:: jupyterlab_server.spec
9+
10+
.. currentmodule:: jupyterlab_server.spec
11+
12+
.. autofunction:: get_openapi_spec
13+
14+
.. autofunction:: get_openapi_spec_dict

jupyterlab_server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .app import LabServerApp
55
from .licenses_app import LicensesApp
66
from .handlers import add_handlers, LabHandler, LabConfig
7-
from .spec import openapi_spec, openapi_spec_dict
7+
from .spec import get_openapi_spec, get_openapi_spec_dict
88
from .translation_utils import translator
99
from .workspaces_app import WorkspaceExportApp, WorkspaceImportApp, WorkspaceListApp
1010
from .workspaces_handler import slugify, WORKSPACE_EXTENSION

jupyterlab_server/spec.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import os
22
from pathlib import Path
33

4-
from ruamel.yaml import YAML
5-
from openapi_core import create_spec
6-
74
HERE = Path(os.path.dirname(__file__)).resolve()
8-
path = HERE / 'rest-api.yml'
9-
yaml = YAML(typ='safe')
10-
openapi_spec_dict = yaml.load(path.read_text(encoding='utf-8'))
11-
openapi_spec = create_spec(openapi_spec_dict)
5+
6+
7+
def get_openapi_spec():
8+
"""Get the OpenAPI spec object."""
9+
from openapi_core import create_spec
10+
openapi_spec_dict = get_openapi_spec_dict()
11+
return create_spec(openapi_spec_dict)
12+
13+
14+
def get_openapi_spec_dict():
15+
"""Get the OpenAPI spec as a dictionary."""
16+
from ruamel.yaml import YAML
17+
path = HERE / 'rest-api.yml'
18+
yaml = YAML(typ='safe')
19+
return yaml.load(path.read_text(encoding='utf-8'))

jupyterlab_server/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import requests
1616
import tornado
1717

18-
from jupyterlab_server.spec import openapi_spec
18+
from jupyterlab_server.spec import get_openapi_spec
1919

2020

2121
HERE = Path(os.path.dirname(__file__)).resolve()
@@ -95,6 +95,7 @@ def wrap_response(response):
9595

9696
def validate_request(response):
9797
"""Validate an API request"""
98+
openapi_spec = get_openapi_spec()
9899
validator = RequestValidator(openapi_spec)
99100
request = wrap_request(response.request, openapi_spec)
100101
result = validator.validate(request)

setup.cfg

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ install_requires =
3232
jinja2>=3.0.3
3333
json5
3434
jsonschema>=3.0.1
35-
openapi_core>=0.14.2
3635
packaging
3736
requests
38-
ruamel.yaml
37+
3938
jupyter_server~=1.8
4039

4140
[options.extras_require]
@@ -49,6 +48,11 @@ test =
4948
strict-rfc3339
5049
wheel
5150
openapi-spec-validator<0.5
51+
openapi_core>=0.14.2
52+
ruamel.yaml
53+
openapi =
54+
openapi_core>=0.14.2
55+
ruamel.yaml
5256

5357
[options.packages.find]
5458
exclude =

0 commit comments

Comments
 (0)