Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SDK for ReductStore API v1.6 #92

Merged
merged 8 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added:

- Add external session and context manager to Client, [PR-90](https://github.com/reductstore/reduct-py/pull/90)
- External session and context manager to Client, [PR-90](https://github.com/reductstore/reduct-py/pull/90)
- `Bucket.remove_entry` method and `limit` kwarg to `Bucket.query`, [PR-92](https://github.com/reductstore/reduct-py/pull/92)

## [1.5.0] - 2023-06-30

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This package provides an asynchronous HTTP client for interacting with the [Redu

## Features

* Supports the [ReductStore HTTP API v1.5](https://docs.reduct.store/http-api)
* Supports the [ReductStore HTTP API v1.6](https://docs.reduct.store/http-api)
* Bucket management
* API Token management
* Write, read and query data
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies = ["aiohttp~=3.8", "pydantic~=1.9", "deprecation~=2.1"]
test = [
"pytest~=7.1",
"pytest-mock~=3.10",
"pytest-asyncio~=0.18"
"pytest-asyncio~=0.18",
"requests~=2.26",
]

lint = ["pylint~=2.14"]
Expand Down
14 changes: 14 additions & 0 deletions reduct/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ async def remove(self):
"""
await self._http.request_all("DELETE", f"/b/{self.name}")

async def remove_entry(self, entry_name: str):
"""
Remove entry from bucket
Args:
entry_name: name of entry
Raises:
ReductError: if there is an HTTP error
"""
await self._http.request_all("DELETE", f"/b/{self.name}/{entry_name}")

@asynccontextmanager
async def read(
self, entry_name: str, timestamp: Optional[int] = None, head: bool = False
Expand Down Expand Up @@ -241,6 +251,7 @@ async def query(
include (dict): query records which have all labels from this dict
exclude (dict): query records which doesn't have all labels from this
head (bool): if True: get only the header of a recod with metadata
limit (int): limit the number of records
Returns:
AsyncIterator[Record]: iterator to the records

Expand Down Expand Up @@ -353,6 +364,9 @@ async def _query(self, entry_name, start, stop, ttl, **kwargs):
if "continuous" in kwargs:
params["continuous"] = "true" if kwargs["continuous"] else "false"

if "limit" in kwargs:
params["limit"] = kwargs["limit"]

url = f"/b/{self.name}/{entry_name}"
data = await self._http.request_all(
"GET",
Expand Down
20 changes: 20 additions & 0 deletions tests/bucket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from reduct import ReductError, BucketSettings, QuotaType, Record, BucketFullInfo
from tests.conftest import requires_api


@pytest.mark.asyncio
Expand All @@ -27,6 +28,14 @@ async def test__remove_not_exist(client):
await bucket.remove()


@pytest.mark.asyncio
@requires_api("1.6")
async def test__remove_entry(bucket_1):
"""Should remove an entry in a bucket"""
await bucket_1.remove_entry("entry-2")
assert "entry-2" not in [entry.name for entry in await bucket_1.get_entry_list()]


@pytest.mark.asyncio
async def test__set_settings(bucket_1):
"""Should set new settings"""
Expand Down Expand Up @@ -260,6 +269,17 @@ async def test_query_records_last(bucket_1):
assert records[0].timestamp == 4_000_000


@pytest.mark.asyncio
@requires_api("1.6")
async def test_query_records_limit(bucket_1):
"""Should query records for until last record"""
records: List[Record] = [
record async for record in bucket_1.query("entry-1", start=0, limit=1)
]
assert len(records) == 1
assert records[0].timestamp == 1000000


@pytest.mark.asyncio
async def test_query_records_all(bucket_1):
"""Should query records all data"""
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import pytest_asyncio
import requests

from reduct import Client, Bucket

Expand All @@ -18,6 +19,17 @@ def requires_env(key):
)


def requires_api(version):
"""Skip test if API version is not supported"""
current_version = requests.get("http://127.0.0.1:8383/info", timeout=1.0).headers[
"x-reduct-api"
]
return pytest.mark.skipif(
version > current_version,
reason=f"Not suitable API version {current_version} for current test",
)


@pytest.fixture(name="url")
def _url() -> str:
return "http://127.0.0.1:8383"
Expand Down
Loading