Skip to content

Commit 553b722

Browse files
committed
initial version
1 parent e3d831c commit 553b722

27 files changed

+1138
-2
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include requirements.txt
2+
include requirements_dev.txt

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi-core
2+
************
3+
4+
Openapi-core is a Python library that adds client-side and server-side support
5+
for the `OpenAPI Specification v3.0.0 <github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md>`__.
6+
7+
Installation
8+
============
9+
10+
Recommended way (via pip):
11+
12+
::
13+
14+
$ pip install openapi-core
15+
16+
Alternatively you can download the code and install from the repository:
17+
18+
.. code-block:: bash
19+
20+
$ pip install -e git+https://github.com/p1c2u/openapi-core.git#egg=openapi_core
21+
22+
Related projects
23+
================
24+
* `openapi-spec-validator <https://github.com/p1c2u/openapi-spec-validator>`__

openapi_core/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""OpenAPI core module"""
2+
from openapi_core.shortcuts import create_spec
3+
4+
__author__ = 'Artur Maciąg'
5+
__email__ = 'maciag.artur@gmail.com'
6+
__version__ = '0.0.1'
7+
__url__ = 'https://github.com/p1c2u/openapi-core'
8+
__license__ = 'BSD 3-Clause License'
9+
10+
__all__ = ['create_spec', ]

openapi_core/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""OpenAPI core exceptions module"""
2+
3+
4+
class OpenAPIError(Exception):
5+
pass
6+
7+
8+
class OpenAPIMappingError(OpenAPIError):
9+
pass
10+
11+
12+
class MissingParameterError(OpenAPIMappingError):
13+
pass
14+
15+
16+
class InvalidContentTypeError(OpenAPIMappingError):
17+
pass

openapi_core/media_types.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""OpenAPI core mediaTypes module"""
2+
from six import iteritems
3+
4+
from openapi_core.schemas import SchemaFactory
5+
6+
7+
class MediaType(object):
8+
"""Represents an OpenAPI MediaType."""
9+
10+
def __init__(self, content_type, schema=None):
11+
self.content_type = content_type
12+
self.schema = schema
13+
14+
def unmarshal(self, value):
15+
if not self.schema:
16+
return value
17+
18+
return self.schema.unmarshal(value)
19+
20+
21+
class MediaTypeGenerator(object):
22+
23+
def __init__(self, dereferencer):
24+
self.dereferencer = dereferencer
25+
26+
def generate(self, content):
27+
for content_type, media_type in iteritems(content):
28+
schema_spec = media_type.get('schema')
29+
30+
schema = None
31+
if schema_spec:
32+
schema = self._create_schema(schema_spec)
33+
34+
yield content_type, MediaType(content_type, schema)
35+
36+
def _create_schema(self, schema_spec):
37+
return SchemaFactory(self.dereferencer).create(schema_spec)

openapi_core/operations.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
"""OpenAPI core operations module"""
3+
import logging
4+
5+
from six import iteritems
6+
7+
from openapi_core.parameters import ParametersGenerator
8+
from openapi_core.request_bodies import RequestBodyFactory
9+
10+
log = logging.getLogger(__name__)
11+
12+
13+
class Operation(object):
14+
"""Represents an OpenAPI Operation."""
15+
16+
def __init__(
17+
self, http_method, path_name, parameters, request_body=None,
18+
deprecated=False, operation_id=None):
19+
self.http_method = http_method
20+
self.path_name = path_name
21+
self.parameters = dict(parameters)
22+
self.request_body = request_body
23+
self.deprecated = deprecated
24+
self.operation_id = operation_id
25+
26+
def __getitem__(self, name):
27+
return self.parameters[name]
28+
29+
30+
class OperationsGenerator(object):
31+
"""Represents an OpenAPI Operation in a service."""
32+
33+
def __init__(self, dereferencer):
34+
self.dereferencer = dereferencer
35+
36+
def generate(self, path_name, path):
37+
path_deref = self.dereferencer.dereference(path)
38+
for http_method, operation in iteritems(path_deref):
39+
if http_method.startswith('x-') or http_method == 'parameters':
40+
continue
41+
42+
operation_deref = self.dereferencer.dereference(operation)
43+
deprecated = operation_deref.get('deprecated', False)
44+
parameters_list = operation_deref.get('parameters', [])
45+
parameters = self._generate_parameters(parameters_list)
46+
47+
request_body = None
48+
if 'requestBody' in operation_deref:
49+
request_body_spec = operation_deref.get('requestBody')
50+
request_body = self._create_request_body(request_body_spec)
51+
52+
yield (
53+
http_method,
54+
Operation(
55+
http_method, path_name, list(parameters),
56+
request_body=request_body, deprecated=deprecated,
57+
),
58+
)
59+
60+
def _generate_parameters(self, parameters):
61+
return ParametersGenerator(self.dereferencer).generate(parameters)
62+
63+
def _create_request_body(self, request_body_spec):
64+
return RequestBodyFactory(self.dereferencer).create(request_body_spec)

openapi_core/parameters.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""OpenAPI core parameters module"""
2+
import logging
3+
4+
from openapi_core.schemas import SchemaFactory
5+
6+
log = logging.getLogger(__name__)
7+
8+
9+
class Parameter(object):
10+
"""Represents an OpenAPI operation Parameter."""
11+
12+
def __init__(
13+
self, name, location, schema=None, default=None,
14+
required=False, deprecated=False, allow_empty_value=False,
15+
items=None, collection_format=None):
16+
self.name = name
17+
self.location = location
18+
self.schema = schema
19+
self.default = default
20+
self.required = required
21+
self.deprecated = deprecated
22+
self.allow_empty_value = allow_empty_value
23+
self.items = items
24+
self.collection_format = collection_format
25+
26+
def unmarshal(self, value):
27+
if not self.schema:
28+
return value
29+
30+
return self.schema.unmarshal(value)
31+
32+
33+
class ParametersGenerator(object):
34+
35+
def __init__(self, dereferencer):
36+
self.dereferencer = dereferencer
37+
38+
def generate(self, paramters):
39+
for parameter in paramters:
40+
parameter_deref = self.dereferencer.dereference(parameter)
41+
42+
default = parameter_deref.get('default')
43+
required = parameter_deref.get('required', False)
44+
45+
schema_spec = parameter_deref.get('schema', None)
46+
schema = None
47+
if schema_spec:
48+
schema = self._create_schema(schema_spec)
49+
50+
yield (
51+
parameter_deref['name'],
52+
Parameter(
53+
parameter_deref['name'], parameter_deref['in'],
54+
schema=schema, default=default, required=required,
55+
),
56+
)
57+
58+
def _create_schema(self, schema_spec):
59+
return SchemaFactory(self.dereferencer).create(schema_spec)

openapi_core/paths.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""OpenAPI core paths module"""
2+
from six import iteritems
3+
4+
from openapi_core.operations import OperationsGenerator
5+
6+
7+
class Path(object):
8+
"""Represents an OpenAPI Path."""
9+
10+
def __init__(self, name, operations):
11+
self.name = name
12+
self.operations = dict(operations)
13+
14+
def __getitem__(self, http_method):
15+
return self.operations[http_method]
16+
17+
18+
class PathsGenerator(object):
19+
20+
def __init__(self, dereferencer):
21+
self.dereferencer = dereferencer
22+
23+
def generate(self, paths):
24+
paths_deref = self.dereferencer.dereference(paths)
25+
for path_name, path in iteritems(paths_deref):
26+
operations = self._generate_operations(path_name, path)
27+
yield path_name, Path(path_name, list(operations))
28+
29+
def _generate_operations(self, path_name, path):
30+
return OperationsGenerator(self.dereferencer).generate(path_name, path)

openapi_core/request_bodies.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""OpenAPI core requestBodies module"""
2+
from openapi_core.media_types import MediaTypeGenerator
3+
4+
5+
class RequestBody(object):
6+
"""Represents an OpenAPI RequestBody."""
7+
8+
def __init__(self, content, required=False):
9+
self.content = dict(content)
10+
self.required = required
11+
12+
def __getitem__(self, content_type):
13+
return self.content[content_type]
14+
15+
16+
class RequestBodyFactory(object):
17+
18+
def __init__(self, dereferencer):
19+
self.dereferencer = dereferencer
20+
21+
def create(self, request_body_spec):
22+
request_body_deref = self.dereferencer.dereference(
23+
request_body_spec)
24+
content = request_body_deref['content']
25+
media_types = self._generate_media_types(content)
26+
required = request_body_deref.get('required', False)
27+
return RequestBody(media_types, required=required)
28+
29+
def _generate_media_types(self, content):
30+
return MediaTypeGenerator(self.dereferencer).generate(content)

0 commit comments

Comments
 (0)