示例#1
0
    def download(self, shard, slicesize=1024):
        """ Downloads a file from the web-core API.

        :param shards: An iterable of upstream.shard.Shard instances
        :param dest: Path to place file as string, otherwise save to CWD using
        filehash as filename
        :param slicesize: Size of shards to write to disk in bytes
        :return: True if success, else None
        :raise FileError: If dest is not a valid filepath or if already exists
        """
        try:
            assert shard.filehash
        except AssertionError:
            raise ShardError("Shard missing filehash.")

        url = "%s/api/download/%s" % (self.server, shard.uri)

        r = requests.get(url, stream=True)
        try:
            r.raise_for_status()
        except requests.exceptions.HTTPError as e:
            err = ResponseError(str(e))
            err.response = r
            raise err

        return r
示例#2
0
    def download(self, shard, slicesize=1024):
        """ Downloads a file from the web-core API.

        :param shards: An iterable of upstream.shard.Shard instances
        :param dest: Path to place file as string, otherwise save to CWD using
        filehash as filename
        :param slicesize: Size of shards to write to disk in bytes
        :return: True if success, else None
        :raise FileError: If dest is not a valid filepath or if already exists
        """
        try:
            assert shard.filehash
        except AssertionError:
            raise ShardError("Shard missing filehash.")

        url = "%s/api/download/%s" % (self.server, shard.uri)

        r = requests.get(url, stream=True)
        try:
            r.raise_for_status()
        except requests.exceptions.HTTPError as e:
            err = ResponseError(str(e))
            err.response = r
            raise err

        return r
示例#3
0
    def upload(self, filepath, shard_size=0, start_pos=0, read_size=1024, callback=None):
        """ Uploads a shard via POST to the specified node
        to the web-core API.  See API docs:
        https://github.com/Storj/web-core#api-documentation

        :param filepath: Path to file as a string
        :return: upstream.shard.Shard
        :raise LookupError: IF
        """
        # Open the file and upload it via POST
        url = self.server + "/api/upload"  # web-core API
        r = self._upload_form_encoded(
            url, filepath, shard_size=shard_size, start_pos=start_pos, read_size=read_size, callback=callback
        )

        # Make sure that the API call is actually there
        if r.status_code == 404:
            raise ResponseError("API call not found.")
        elif r.status_code == 402:
            raise ResponseError("Payment required.")
        elif r.status_code == 500:
            raise ResponseError("Server error.")
        elif r.status_code == 201:
            shard = Shard()
            shard.from_json(r.text)
            return shard
        else:
            err = ResponseError("Received status code %s %s" % (r.status_code, r.reason))
            err.response = r
            raise err
示例#4
0
    def upload(self,
               filepath,
               shard_size=0,
               start_pos=0,
               read_size=1024,
               callback=None):
        """ Uploads a shard via POST to the specified node
        to the web-core API.  See API docs:
        https://github.com/Storj/web-core#api-documentation

        :param filepath: Path to file as a string
        :return: upstream.shard.Shard
        :raise LookupError: IF
        """
        # Open the file and upload it via POST
        url = self.server + "/api/upload"  # web-core API
        r = self._upload_form_encoded(url,
                                      filepath,
                                      shard_size=shard_size,
                                      start_pos=start_pos,
                                      read_size=read_size,
                                      callback=callback)

        # Make sure that the API call is actually there
        if r.status_code == 404:
            raise ResponseError("API call not found.")
        elif r.status_code == 402:
            raise ResponseError("Payment required.")
        elif r.status_code == 500:
            raise ResponseError("Server error.")
        elif r.status_code == 201:
            shard = Shard()
            shard.from_json(r.text)
            return shard
        else:
            err = ResponseError("Received status code %s %s" %
                                (r.status_code, r.reason))
            err.response = r
            raise err