Ejemplo n.º 1
0
    async def list_artifacts(self, ipfs_uri, api_key=None, tries=2):
        """Return a list of artificats from a given ipfs_uri.

        Args:
            ipfs_uri (str): IPFS URI to get artifiacts from.
            api_key (str): Override default API key

        Returns:
            List[(str, str)]: A list of tuples. First tuple element is the artifact name, second tuple element
            is the artifact hash.
        """
        if not is_valid_ipfs_uri(ipfs_uri):
            logger.warning('Invalid IPFS URI: %s', ipfs_uri)
            return []

        path = f'/artifacts/{ipfs_uri}'

        # Chain parameter doesn't matter for artifacts, just set to side
        success, result = await self.make_request('GET',
                                                  path,
                                                  'side',
                                                  api_key=api_key,
                                                  tries=tries)
        if not success:
            logger.error('Expected artifact listing, received',
                         extra={'extra': result})
            return []

        result = {} if result is None else result
        return [(a.get('name', ''), a.get('hash', '')) for a in result]
Ejemplo n.º 2
0
        async def __anext__(self):
            if not is_valid_ipfs_uri(self.ipfs_uri):
                raise StopAsyncIteration

            i = self.i
            self.i += 1

            if i < MAX_ARTIFACTS:
                content = await self.client.get_artifact(self.ipfs_uri, i, api_key=self.api_key)
                if content:
                    return content

            raise StopAsyncIteration
Ejemplo n.º 3
0
    async def get_artifact(self, ipfs_uri, index, api_key=None, tries=2):
        """Retrieve an artifact from IPFS via polyswarmd

        Args:
            ipfs_uri (str): IPFS hash of the artifact to retrieve
            index (int): Index of the sub artifact to retrieve
            api_key (str): Override default API key
        Returns:
            (bytes): Content of the artifact
        """
        if not is_valid_ipfs_uri(ipfs_uri):
            raise ValueError('Invalid IPFS URI')

        uri = f'{self.polyswarmd_uri}/artifacts/{ipfs_uri}/{index}'
        logger.debug('getting artifact from uri: %s', uri)

        params = dict(self.params)

        # Allow overriding API key per request
        if api_key is None:
            api_key = self.api_key
        headers = {'Authorization': api_key} if api_key is not None else None

        while tries > 0:
            tries -= 1

            try:
                async with self.__session.get(uri,
                                              params=params,
                                              headers=headers) as raw_response:
                    # Handle "Too many requests" rate limit by not hammering server, and instead sleeping a bit
                    if raw_response.status == 429:
                        logger.warning(
                            'Hit polyswarmd rate limits, sleeping then trying again'
                        )
                        await asyncio.sleep(RATE_LIMIT_SLEEP)
                        tries += 1
                        continue

                    if raw_response.status == 200:
                        return await raw_response.read()
            except (OSError, aiohttp.ServerDisconnectedError):
                logger.error('Connection to polyswarmd refused')
            except asyncio.TimeoutError:
                logger.error('Connection to polyswarmd timed out')

        return None