示例#1
0
def test_test_utils():
    """Ensure the BlobStash utils can spawn a server."""
    b = BlobStash()
    b.cleanup()
    try:
        b.run()
        c = Client()
        resp = c.request("GET", "/", raw=True)
        assert resp.status_code == 404
    finally:
        b.shutdown()
        b.cleanup()
示例#2
0
class KVStoreClient:
    def __init__(self, base_url=None, api_key=None, client=None):
        if client:
            self._client = client
            return

        self._client = Client(base_url=base_url, api_key=api_key)

    def put(self, key, data, ref="", version=-1):
        # XXX(tsileo): check with `ref` and `data` as None
        return KeyValue(
            **self._client.request(
                "POST",
                "/api/kvstore/key/" + key,
                data=dict(data=data, ref=ref, version=version),
            )
        )

    def get(self, key, version=None):
        try:
            return KeyValue(**self._client.request("GET", "/api/kvstore/key/" + key))
        except HTTPError as error:
            if error.response.status_code == 404:
                raise KeyNotFoundError
            raise

    def get_versions(self, key, cursor=None, limit=None):
        if isinstance(key, KeyValue):
            key = key.key
        try:
            return KeyVersionsIterator(self._client, key, cursor=cursor, limit=limit)
        except HTTPError as error:
            if error.response.status_code == 404:
                raise KeyNotFoundError
            raise

    def iter(self, cursor=None, limit=None, **kwargs):
        return KeysIterator(self._client, cursor=cursor, limit=limit, **kwargs)

    def __iter__(self):
        return KeysIterator(self._client)
示例#3
0
class BlobStoreClient:
    def __init__(self, base_url=None, api_key=None, client=None):
        if client:
            self._client = client
            return

        self._client = Client(base_url=base_url, api_key=api_key)

    def put(self, blob):
        files = {blob.hash: blob.data}
        resp = self._client.request("POST",
                                    "/api/blobstore/upload",
                                    files=files,
                                    raw=True)
        resp.raise_for_status()

    def get(self, hash):
        resp = self._client.request("GET",
                                    "/api/blobstore/blob/{}".format(hash),
                                    raw=True)
        try:
            resp.raise_for_status()
            return Blob(hash, resp.content)
        except HTTPError as error:
            if error.response.status_code == 404:
                raise BlobNotFoundError
            raise

    def iter(self, cursor=None, limit=None, per_page=None, **kwargs):
        return BlobsIterator(self._client,
                             cursor=cursor,
                             limit=limit,
                             per_page=per_page,
                             **kwargs)

    def __iter__(self):
        return BlobsIterator(self._client)
示例#4
0
class DocStoreClient:
    """BlobStash DocStore client."""
    def __init__(self, base_url: str = None, api_key: str = None) -> None:
        self._client = Client(base_url=base_url,
                              api_key=api_key,
                              json_encoder=JSONEncoder)

    def __getitem__(self, key):
        return self.collection(key)

    def __getattr__(self, name):
        return self.collection(name)

    def collection(self, name):
        """Returns a `Collection` instance for the given name."""
        return Collection(self._client, name)

    def collections(self):
        """Returns all the available collections."""
        collections = []
        resp = self._client.request("GET", "/api/docstore/")
        for col in resp["collections"]:
            collections.append(self.collection(col))
        return collections

    def fadd_attachment(self, name=None, fileobj=None, content_type=None):
        return fadd_attachment(self._client, name, fileobj, content_type)

    def add_attachment(self, path):
        """Upload the file/dir at path, and return the key to embed the file/dir as an attachment/filetree pointer.

        >>> doc['my_super_text_file'] = client.add_attachment('/path/to/my/text_file.txt')

        """
        return add_attachment(self._client, path)

    def fget_attachment(self, attachment):
        """Returns a fileobj (that needs to be closed) with the content off the attachment."""
        return fget_attachment(self._client, attachment)

    def get_attachment(self, attachment, path):
        return get_attach(self._client, attachment, path)

    def __repr__(self):
        return "blobstash.docstore.DocStoreClient(base_url={!r})".format(
            self._client.base_url)

    def __str__(self):
        return self.__repr__()
示例#5
0
    def __init__(self, base_url=None, api_key=None, client=None):
        if client:
            self._client = client
            return

        self._client = Client(base_url=base_url, api_key=api_key)
示例#6
0
 def __init__(self, base_url: str = None, api_key: str = None) -> None:
     self._client = Client(base_url=base_url,
                           api_key=api_key,
                           json_encoder=JSONEncoder)
示例#7
0
class FileTreeClient:
    """BlobStash FileTree client."""
    def __init__(self, base_url=None, api_key=None, client=None):
        if client:
            self._client = client
            return

        self._client = Client(base_url=base_url, api_key=api_key)

    def fput_node(self, name, fileobj, content_type=None):
        """Upload the fileobj as name, and return the newly created node."""
        return Node.from_resp(
            self._client.request(
                "POST",
                "/api/filetree/upload",
                files=[("file", (name, fileobj, content_type))],
            ))

    def put_node(self, path):
        """Uppload the file at the given path, and return the newly created node."""
        name = Path(path).name
        with open(path, "rb") as f:
            return self.fput_node(name, f)

    def fget_node(self, ref_or_node):
        """Returns a file-like object for given node ref.

        It's up to the client to call `close` to release the connection.

        """
        if isinstance(ref_or_node, Node):
            ref = ref_or_node.ref
        else:
            ref = ref_or_node
        return self._client.request("GET",
                                    "/api/filetree/file/" + ref,
                                    raw=True,
                                    stream=True).raw

    def get_node(self, ref_or_node, path):
        """Download the content of the given node at path."""
        with open(path, "wb") as f:
            reader = self.fget_node(ref_or_node)
            try:
                while 1:
                    chunk = reader.read(1024)
                    if not chunk:
                        break
                    f.write(chunk)
            except Exception:  # pragma: no cover
                reader.close()
                raise

    def node(self, ref):
        """Returns the node for the given ref."""
        return Node.from_resp(
            self._client.request("GET", "/api/filetree/node/" + ref)["node"])

    def fs(self, name=None, ref=None, prefix=None):
        """Returns the filesystem for the given name if it exists."""
        return FS(self, name=name, ref=ref, prefix=prefix)

    def __repr__(self):
        return "blobstash.docstore.FileTreeClient(base_url={!r})".format(
            self._client.base_url)

    def __str__(self):
        return self.__repr__()