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 2 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
only_value: bool = False
aliel marked this conversation as resolved.
Show resolved Hide resolved

@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.only_value and query_params.keys and len(query_params.keys) == 1:
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
3 changes: 2 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
Loading