def decode(self, bytestring, **options): try: spore_desc = json.loads(bytestring.decode('utf-8')) except ValueError as exc: raise ParseError('Malformed JSON. %s' % exc) base_url = options.get('base_url', '') coreapi_doc = parse_spore_description(spore_desc, base_url) if not isinstance(coreapi_doc, Document): raise ParseError('Top level node must be a document.') return coreapi_doc
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 load(self, bytes, **kwargs): """ Takes a bytestring and returns a document. """ base_url = kwargs.get('base_url', None) try: data = json.loads(bytes.decode('utf-8')) except ValueError as exc: raise ParseError('Malformed JSON. %s' % exc) doc = _primative_to_document(data, base_url) if not (isinstance(doc, Document)): raise ParseError('Top level node must be a document.') return doc
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 = _primative_to_document(data, base_url) if isinstance(doc, Object): doc = Document(content=dict(doc)) elif not (isinstance(doc, Document) or isinstance(doc, Error)): raise ParseError('Top level node should be a document or error.') return doc
def decode(self, bytestring, **options): """ Return raw JSON data. """ try: return json.loads(bytestring.decode("utf-8"), object_pairs_hook=collections.OrderedDict) except ValueError as exc: raise ParseError("Malformed JSON. %s" % exc)
def load(self, bytes, base_url=None): """ Return raw JSON data. """ try: return json.loads(bytes.decode('utf-8'), object_pairs_hook=collections.OrderedDict) except ValueError as exc: raise ParseError('Malformed JSON. %s' % exc)
def decode(self, bytestring, **options): """ Takes a bytestring and returns a document. """ base_url = options.get("base_url") try: data = json.loads(bytestring.decode("utf-8")) except ValueError as exc: raise ParseError("Malformed JSON. %s" % exc) doc = _primitive_to_document(data, base_url) if isinstance(doc, Object): doc = Document(content=dict(doc)) elif not (isinstance(doc, Document) or isinstance(doc, Error)): raise ParseError("Top level node should be a document or error.") return doc
def _get_document_base_url(data, base_url=None): """ Get the base url to use when constructing absolute paths from the relative ones provided in the schema defination. """ prefered_schemes = ['https', 'http'] if base_url: url_components = urlparse.urlparse(base_url) default_host = url_components.netloc default_scheme = url_components.scheme else: default_host = '' default_scheme = None host = _get_string(data, 'host', default=default_host) path = _get_string(data, 'basePath', default='/') path = '/' + path.lstrip('/') path = path.rstrip('/') + '/' if not host: # No host is provided, and we do not have an initial URL. return path schemes = _get_list(data, 'schemes') if not schemes: # No schemes provided, use the initial URL, or a fallback. scheme = default_scheme or prefered_schemes[0] elif default_scheme in schemes: # Schemes provided, the initial URL matches one of them. scheme = default_scheme else: # Schemes provided, the initial URL does not match, pick a fallback. for scheme in prefered_schemes: if scheme in schemes: break else: raise ParseError('Unsupported transport schemes "%s"' % schemes) return '%s://%s%s' % (scheme, host, path)