Exemplo n.º 1
0
 def fetch_uri_contents(self, uri: str) -> bytes:
     ipfs_hash = extract_ipfs_path_from_uri(uri)
     contents = self.client.cat(ipfs_hash)
     validation_hash = generate_file_hash(contents)
     if validation_hash != ipfs_hash:
         raise ValidationError(
             f"Hashed IPFS contents retrieved from uri: {uri} do not match its content hash."
         )
     return contents
Exemplo n.º 2
0
    def from_ipfs(cls, ipfs_uri: str) -> 'Package':
        """
        Instantiate a Package object from an IPFS uri.
        TODO: Defaults to Infura gateway, needs extension
        to support other gateways and local nodes
        """
        if is_ipfs_uri(ipfs_uri):
            ipfs_path = extract_ipfs_path_from_uri(ipfs_uri)
            package_data = fetch_ipfs_package(ipfs_path)
        else:
            raise TypeError(
                "The Package.from_ipfs method only accepts a valid IPFS uri."
                "{0} is not a valid IPFS uri.".format(ipfs_uri))

        return cls(package_data)
Exemplo n.º 3
0
def test_extract_ipfs_path_from_uri(value, expected):
    actual = extract_ipfs_path_from_uri(value)
    assert actual == expected
Exemplo n.º 4
0
 def fetch_uri_contents(self, uri: str) -> bytes:
     ipfs_hash = extract_ipfs_path_from_uri(uri)
     gateway_uri = self.base_uri + ipfs_hash
     response = requests.get(gateway_uri)
     response.raise_for_status()
     return response.content
Exemplo n.º 5
0
 def fetch_uri_contents(self, ipfs_uri: str) -> bytes:
     ipfs_hash = extract_ipfs_path_from_uri(ipfs_uri)
     contents = self.client.cat(ipfs_hash)
     return contents