def _primative_to_document(data, base_url=None): """ Take Python primatives as returned from parsing JSON content, and return a Core API document. """ if isinstance(data, dict) and data.get('_type') == 'document': # Document meta = _get_dict(data, '_meta') url = _get_string(meta, 'url') url = urlparse.urljoin(base_url, url) title = _get_string(meta, 'title') content = _get_content(data, base_url=url) return Document(url=url, title=title, content=content) if isinstance(data, dict) and data.get('_type') == 'error': # Error meta = _get_dict(data, '_meta') title = _get_string(meta, 'title') content = _get_content(data, base_url=base_url) return Error(title=title, content=content) elif isinstance(data, dict) and data.get('_type') == 'link': # Link url = _get_string(data, 'url') url = urlparse.urljoin(base_url, url) action = _get_string(data, 'action') encoding = _get_string(data, 'encoding') transform = _get_string(data, 'transform') description = _get_string(data, 'description') fields = _get_list(data, 'fields') fields = [ Field(name=_get_string(item, 'name'), required=_get_bool(item, 'required'), location=_get_string(item, 'location'), type=_get_string(item, 'type'), description=_get_string(item, 'description')) for item in fields if isinstance(item, dict) ] return Link(url=url, action=action, encoding=encoding, transform=transform, description=description, fields=fields) elif isinstance(data, dict): # Map content = _get_content(data, base_url=base_url) return Object(content) elif isinstance(data, list): # Array content = [_primative_to_document(item, base_url) for item in data] return Array(content) # String, Integer, Number, Boolean, null. return data
def get(self, request): generator = SchemaGenerator( title='CyBorgBackup API', patterns=None, urlconf=None ) schema = generator.get_schema(request=request) # python core-api doesn't support the deprecation yet, so track it # ourselves and return it in a response header _deprecated = [] # By default, DRF OpenAPI serialization places all endpoints in # a single node based on their root path (/api). Instead, we want to # group them by topic/tag so that they're categorized in the rendered # output document = schema._data.pop('api') for path, node in document.items(): if isinstance(node, Object): for action in node.values(): topic = getattr(action, 'topic', None) if topic: schema._data.setdefault(topic, Object()) schema._data[topic]._data[path] = node if isinstance(action, Object): for link in action.links.values(): if link.deprecated: _deprecated.append(link.url) elif isinstance(node, Link): topic = getattr(node, 'topic', None) if topic: schema._data.setdefault(topic, Object()) schema._data[topic]._data[path] = node if not schema: raise exceptions.ValidationError( 'The schema generator did not return a schema Document' ) return Response( schema, headers={'X-Deprecated-Paths': json.dumps(_deprecated)} )
def _primitive_to_document(data, base_url=None): """ Take Python primitives as returned from parsing JSON content, and return a Core API document. """ if isinstance(data, dict) and data.get("_type") == "document": # Document meta = _get_dict(data, "_meta") url = _get_string(meta, "url") url = urlparse.urljoin(base_url, url) title = _get_string(meta, "title") description = _get_string(meta, "description") content = _get_content(data, base_url=url) return Document( url=url, title=title, description=description, media_type="application/coreapi+json", content=content, ) if isinstance(data, dict) and data.get("_type") == "error": # Error meta = _get_dict(data, "_meta") title = _get_string(meta, "title") content = _get_content(data, base_url=base_url) return Error(title=title, content=content) elif isinstance(data, dict) and data.get("_type") == "link": # Link url = _get_string(data, "url") url = urlparse.urljoin(base_url, url) action = _get_string(data, "action") encoding = _get_string(data, "encoding") transform = _get_string(data, "transform") title = _get_string(data, "title") description = _get_string(data, "description") fields = _get_list(data, "fields") fields = [ Field( name=_get_string(item, "name"), required=_get_bool(item, "required"), location=_get_string(item, "location"), schema=_get_schema(item, "schema"), ) for item in fields if isinstance(item, dict) ] return Link( url=url, action=action, encoding=encoding, transform=transform, title=title, description=description, fields=fields, ) elif isinstance(data, dict): # Map content = _get_content(data, base_url=base_url) return Object(content) elif isinstance(data, list): # Array content = [_primitive_to_document(item, base_url) for item in data] return Array(content) # String, Integer, Number, Boolean, null. return data