|
1 | 1 | """OpenAPI core shortcuts module""" |
2 | | -from jsonschema.validators import RefResolver |
3 | | -from openapi_spec_validator import default_handlers |
4 | | - |
5 | | -from openapi_core.schema.media_types.exceptions import OpenAPIMediaTypeError |
6 | | -from openapi_core.schema.parameters.exceptions import OpenAPIParameterError |
7 | | -from openapi_core.schema.request_bodies.exceptions import ( |
8 | | - OpenAPIRequestBodyError, |
| 2 | +# backward compatibility |
| 3 | +from openapi_core.schema.shortcuts import create_spec |
| 4 | +from openapi_core.validation.request.shortcuts import ( |
| 5 | + spec_validate_body as validate_body, |
| 6 | + spec_validate_parameters as validate_parameters, |
| 7 | +) |
| 8 | +from openapi_core.validation.response.shortcuts import ( |
| 9 | + spec_validate_data as validate_data |
9 | 10 | ) |
10 | | -from openapi_core.schema.schemas.exceptions import OpenAPISchemaError |
11 | | -from openapi_core.schema.specs.factories import SpecFactory |
12 | | -from openapi_core.validation.request.validators import RequestValidator |
13 | | -from openapi_core.validation.response.validators import ResponseValidator |
14 | | - |
15 | | - |
16 | | -def create_spec(spec_dict, spec_url=''): |
17 | | - spec_resolver = RefResolver( |
18 | | - spec_url, spec_dict, handlers=default_handlers) |
19 | | - spec_factory = SpecFactory(spec_resolver) |
20 | | - return spec_factory.create(spec_dict, spec_url=spec_url) |
21 | | - |
22 | | - |
23 | | -def validate_parameters(spec, request, request_factory=None): |
24 | | - if request_factory is not None: |
25 | | - request = request_factory(request) |
26 | | - |
27 | | - validator = RequestValidator(spec) |
28 | | - result = validator.validate(request) |
29 | | - |
30 | | - try: |
31 | | - result.raise_for_errors() |
32 | | - except ( |
33 | | - OpenAPIRequestBodyError, OpenAPIMediaTypeError, |
34 | | - OpenAPISchemaError, |
35 | | - ): |
36 | | - return result.parameters |
37 | | - else: |
38 | | - return result.parameters |
39 | | - |
40 | | - |
41 | | -def validate_body(spec, request, request_factory=None): |
42 | | - if request_factory is not None: |
43 | | - request = request_factory(request) |
44 | | - |
45 | | - validator = RequestValidator(spec) |
46 | | - result = validator.validate(request) |
47 | | - |
48 | | - try: |
49 | | - result.raise_for_errors() |
50 | | - except OpenAPIParameterError: |
51 | | - return result.body |
52 | | - else: |
53 | | - return result.body |
54 | | - |
55 | | - |
56 | | -def validate_data( |
57 | | - spec, request, response, |
58 | | - request_factory=None, |
59 | | - response_factory=None): |
60 | | - if request_factory is not None: |
61 | | - request = request_factory(request) |
62 | | - |
63 | | - if response_factory is not None: |
64 | | - response = response_factory(response) |
65 | | - |
66 | | - validator = ResponseValidator(spec) |
67 | | - result = validator.validate(request, response) |
68 | | - |
69 | | - result.raise_for_errors() |
70 | 11 |
|
71 | | - return result.data |
| 12 | +__all__ = [ |
| 13 | + 'create_spec', 'validate_body', 'validate_parameters', 'validate_data', |
| 14 | +] |
0 commit comments