Beispiel #1
0
    def from_cbor_data(cls, obj):
        if not isinstance(obj, dict):
            raise link_header.ParseException("Entry is not a dict")

        if any(k in _CBOR_ENCODING for k in obj):
            # it says "MUST NOT accept"
            raise link_header.ParseException("Unencoded attribute")

        return cls.from_json_data(
                {_CBOR_DECODING.get(k, k): v for (k, v) in obj.items()})
Beispiel #2
0
    def from_json_string(cls, encoded):
        import json

        try:
            array = json.loads(encoded)
        except json.JSONDecodeError:
            raise link_header.ParseException("Not valid JSON")
        if not isinstance(array, list):
            raise link_header.ParseException("Not a JSON array")
        return cls(Link.from_json_data(x) for x in array)
Beispiel #3
0
    def from_cbor_bytes(cls, encoded):
        import cbor

        try:
            # FIXME: don't silently accept trailing bytes
            array = cbor.loads(encoded)
        except: # FIXME in library: give one subclassable "failure to decode" error
            raise link_header.ParseException("Not valid CBOR")
        if not isinstance(array, list):
            raise link_header.ParseException("Not a CBOR array")
        return cls(Link.from_cbor_data(x) for x in array)
Beispiel #4
0
    def from_json_data(cls, obj):
        if not isinstance(obj, dict):
            raise link_header.ParseException("Entry is not a dict")

        href = obj.get('href')
        if not isinstance(href, str):
            raise link_header.ParseException("href is not a single string")
        link = Link(href)
        for k, values in obj.items():
            if not isinstance(values, list):
                values = (values,)
            for v in values:
                if isinstance(v, str):
                    link.attr_pairs.append((k, v))
                elif v is True:
                    link.attr_pairs.append((k, None))
                elif isinstance(v, dict):
                    raise link_header.ParseException("Language tags not supported by link_header")
                else:
                    raise link_header.ParseException("Unsupported value type")
        return link