async def dereference(self, profile: Profile, did_url: str) -> Union[Service, VerificationMethod]: """Dereference a DID URL to its corresponding DID Doc object.""" # TODO Use cached DID Docs when possible try: did_url = DIDUrl.parse(did_url) doc = await self.resolve(profile, did_url.did) return doc.dereference(did_url) except DIDError as err: raise ResolverError( "Failed to parse DID URL from {}".format(did_url)) from err
async def dereference(self, profile: Profile, did_url: str) -> Resource: """Dereference a DID URL to its corresponding DID Doc object.""" # TODO Use cached DID Docs when possible try: parsed = DIDUrl.parse(did_url) if not parsed.did: raise ValueError("Invalid DID URL") doc_dict = await self.resolve(profile, parsed.did) return pydid.deserialize_document(doc_dict).dereference(parsed) except DIDError as err: raise ResolverError( "Failed to parse DID URL from {}".format(did_url)) from err
async def dereference(self, profile: Profile, did_url: str, *, cls: Type[ResourceType] = Resource) -> ResourceType: """Dereference a DID URL to its corresponding DID Doc object.""" # TODO Use cached DID Docs when possible try: parsed = DIDUrl.parse(did_url) if not parsed.did: raise ValueError("Invalid DID URL") except DIDError as err: raise ResolverError( "Failed to parse DID URL from {}".format(did_url)) from err doc_dict = await self.resolve(profile, parsed.did) # Use non-conformant doc as the "least common denominator" try: return NonconformantDocument.deserialize(doc_dict).dereference_as( cls, parsed) except IDNotFoundError as error: raise ResolverError( "Failed to dereference DID URL: {}".format(error)) from error
def test_url_method(did, parts): did = DID(did) assert did.url(parts.get("path"), parts.get("query"), parts.get("fragment")) == DIDUrl(**parts)
def test_relative_url(): url = DIDUrl.parse("/some/path?query=value#fragment") assert not url.did assert url.path assert url.query assert url.fragment
def test_partial_match_url(): assert not DIDUrl.is_valid("did:bad:char'@='acters:example:1234abcd#4")
def test_is_valid_x(bad_url): assert not DIDUrl.is_valid(bad_url)
def test_is_valid(url): assert DIDUrl.is_valid(url)
def test_as_str(parts, url): assert DIDUrl.unparse(**parts) == url
def test_did_url_parse_x(bad_url): with pytest.raises(InvalidDIDUrlError): DIDUrl(bad_url)
def test_did_url_neq(lhs, rhs): lhs = DIDUrl(lhs) assert lhs != rhs rhs = DIDUrl(rhs) assert lhs != rhs assert lhs != {"not a": "DIDUrl"}
def test_did_url(url, parts): assert DIDUrl(url) == DIDUrl.unparse(**parts)
def test_did_url_from_parts(inputs, output): url = DIDUrl.unparse(**inputs) assert url == output assert repr(url)
def test_did_url_parse(url, parts): assert DIDUrl.parse(url) == DIDUrl(**parts)
def test_did_url(inputs, output): url = DIDUrl(**inputs) assert str(url) == output assert repr(url)