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
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)
def test_extract_ipfs_path_from_uri(value, expected): actual = extract_ipfs_path_from_uri(value) assert actual == expected
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
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