forked from core-api/python-openapi-codec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
35 lines (26 loc) · 1007 Bytes
/
__init__.py
File metadata and controls
35 lines (26 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
from coreapi.codecs.base import BaseCodec
from coreapi.compat import force_bytes
from coreapi.document import Document
from coreapi.exceptions import ParseError
from openapi_codec.encode import generate_swagger_object
from openapi_codec.decode import _parse_document
__version__ = "1.0.0"
class OpenAPICodec(BaseCodec):
media_type = "application/openapi+json"
supports = ['encoding', 'decoding']
def load(self, bytes, base_url=None):
"""
Takes a bytestring and returns a document.
"""
try:
data = json.loads(bytes.decode('utf-8'))
except ValueError as exc:
raise ParseError('Malformed JSON. %s' % exc)
doc = _parse_document(data, base_url)
if not isinstance(doc, Document):
raise ParseError('Top level node must be a document.')
return doc
def dump(self, document, **kwargs):
data = generate_swagger_object(document)
return force_bytes(json.dumps(data))