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

Add param to only return the value of an aggregate by its key #557

Merged
merged 3 commits into from
Feb 23, 2024
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
24 changes: 16 additions & 8 deletions src/aleph/web/controllers/aggregates.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import datetime as dt
import logging
from typing import List, Optional, Dict
from typing import Dict, List, Optional

from aiohttp import web
from pydantic import BaseModel, validator, ValidationError
from aleph.db.accessors.aggregates import get_aggregates_by_owner, refresh_aggregate
from aleph.db.models import AggregateDb
from pydantic import BaseModel, ValidationError, validator
from sqlalchemy import select

from aleph.db.accessors.aggregates import (
get_aggregates_by_owner,
refresh_aggregate,
)
from aleph.db.models import AggregateDb
from .utils import LIST_FIELD_SEPARATOR

LOGGER = logging.getLogger(__name__)
Expand All @@ -22,6 +19,7 @@ class AggregatesQueryParams(BaseModel):
keys: Optional[List[str]] = None
limit: int = DEFAULT_LIMIT
with_info: bool = False
value_only: bool = False

@validator(
"keys",
Expand Down Expand Up @@ -62,17 +60,27 @@ async def address_aggregate(request: web.Request) -> web.Response:

aggregates = list(
get_aggregates_by_owner(
session=session, owner=address, with_info=query_params.with_info
session=session, owner=address,
with_info=query_params.with_info, keys=query_params.keys
)
)

if not aggregates:
raise web.HTTPNotFound(text="No aggregate found for this address")

if query_params.value_only and query_params.keys and len(query_params.keys) == 1:
MHHukiewitz marked this conversation as resolved.
Show resolved Hide resolved
output = {}
target_key = query_params.keys[0]
for result in aggregates:
output[result[0]] = result[1]
MHHukiewitz marked this conversation as resolved.
Show resolved Hide resolved

return web.json_response(output[target_key])
aliel marked this conversation as resolved.
Show resolved Hide resolved

output = {
"address": address,
"data": {},
}

info: Dict = {}
data: Dict = {}

Expand Down
21 changes: 20 additions & 1 deletion tests/api/test_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import aiohttp
import pytest

from aleph.db.models import MessageDb

AGGREGATES_URI = "/api/v0/aggregates/{address}.json"
Expand Down Expand Up @@ -100,10 +99,12 @@ async def test_get_aggregates_filter_by_key(

# Multiple keys
address, keys = ADDRESS_1, ["test_target", "test_reference"]
except_key = "test_key"
aggregates = await get_aggregates_expect_success(
ccn_api_client, address=address, keys=",".join(keys), with_info=False
)
assert aggregates["address"] == address
assert except_key not in aggregates["data"]
for key in keys:
assert (
aggregates["data"][key] == EXPECTED_AGGREGATES[address][key]
Expand Down Expand Up @@ -164,3 +165,21 @@ async def test_get_aggregates_invalid_params(
errors = await response.json()
assert len(errors) == 1
assert errors[0]["loc"] == ["limit"], errors

@pytest.mark.asyncio
async def test_get_aggregates_return_value_only(
ccn_api_client, fixture_aggregate_messages: Sequence[MessageDb]
):
"""
Should return the value without wrapping (aggregate info)
"""

assert fixture_aggregate_messages # To avoid unused parameter warnings

address, key = ADDRESS_1, "test_target"
aggregates = await get_aggregates_expect_success(
ccn_api_client, address=address, keys=key,
with_info=False, value_only="1"
)
assert address not in aggregates
assert aggregates == EXPECTED_AGGREGATES[address][key]
Loading