From 5a1a1f58873a54df7c08da5510a5a1287e32f097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s?= <7888669+moisses89@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:36:58 +0100 Subject: [PATCH] Add next and previous page --- app/datasources/db/models.py | 2 +- app/routers/contracts.py | 5 +++-- app/services/contract.py | 4 ++-- app/services/pagination.py | 3 ++- app/tests/db/test_model.py | 8 +++++++- app/tests/routers/test_contracts.py | 7 ++++--- scripts/fill_db.py | 0 7 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 scripts/fill_db.py diff --git a/app/datasources/db/models.py b/app/datasources/db/models.py index e5e29b1..b82e152 100644 --- a/app/datasources/db/models.py +++ b/app/datasources/db/models.py @@ -46,7 +46,7 @@ class Abi(SqlQueryBase, SQLModel, table=True): relevance: int | None = Field(nullable=False, default=0) abi_json: dict = Field(default_factory=dict, sa_column=Column(JSON)) source_id: int | None = Field( - nullable=True, default=None, foreign_key="abisource.id" + nullable=False, default=None, foreign_key="abisource.id" ) source: AbiSource | None = Relationship(back_populates="abis") diff --git a/app/routers/contracts.py b/app/routers/contracts.py index 44726a3..7e0882a 100644 --- a/app/routers/contracts.py +++ b/app/routers/contracts.py @@ -1,6 +1,6 @@ from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException, Query +from fastapi import APIRouter, Depends, HTTPException, Query, Request from hexbytes import HexBytes from safe_eth.eth.utils import fast_is_checksum_address @@ -20,6 +20,7 @@ @router.get("/{address}", response_model=PaginatedResponse[ContractsPublic]) async def list_contracts( + request: Request, address: str, chain_ids: Annotated[list[int] | None, Query()] = None, limit: int = Query(None), @@ -29,5 +30,5 @@ async def list_contracts( if not fast_is_checksum_address(address): raise HTTPException(status_code=400, detail="Address is not checksumed") - contracts_service = ContractService(limit, offset) + contracts_service = ContractService(str(request.base_url), limit, offset) return await contracts_service.get_contract(session, HexBytes(address), chain_ids) diff --git a/app/services/contract.py b/app/services/contract.py index 9f91712..42978eb 100644 --- a/app/services/contract.py +++ b/app/services/contract.py @@ -8,8 +8,8 @@ class ContractService: - def __init__(self, limit: int | None, offset: int | None): - self.pagination = GenericPagination(model=Contract) + def __init__(self, base_url: str, limit: int | None, offset: int | None): + self.pagination = GenericPagination(base_url=base_url, model=Contract) self.pagination.set_limit(limit) self.pagination.set_offset(offset) diff --git a/app/services/pagination.py b/app/services/pagination.py index 0055255..11356eb 100644 --- a/app/services/pagination.py +++ b/app/services/pagination.py @@ -18,6 +18,7 @@ class PaginatedResponse(BaseModel, Generic[T]): class GenericPagination: def __init__( self, + base_url: str, model, default_page_size: int = 10, max_page_size: int = 100, @@ -26,7 +27,7 @@ def __init__( self.model = model self.limit = default_page_size self.offset = 0 - self.base_url = "" + self.base_url = base_url def set_limit(self, limit): if limit: diff --git a/app/tests/db/test_model.py b/app/tests/db/test_model.py index 331626c..197029d 100644 --- a/app/tests/db/test_model.py +++ b/app/tests/db/test_model.py @@ -22,7 +22,13 @@ async def test_project(self, session: AsyncSession): @database_session async def test_abi(self, session: AsyncSession): - abi = Abi(abi_hash=b"A Test Abi", abi_json={"name": "A Test Project"}) + abi_source = AbiSource(name="A Test Source", url="https://test.com") + await abi_source.create(session) + abi = Abi( + abi_hash=b"A Test Abi", + abi_json={"name": "A Test Project"}, + source_id=abi_source.id, + ) await abi.create(session) result = await abi.get_all(session) self.assertEqual(result[0], abi) diff --git a/app/tests/routers/test_contracts.py b/app/tests/routers/test_contracts.py index 7aa80b5..4db9c7f 100644 --- a/app/tests/routers/test_contracts.py +++ b/app/tests/routers/test_contracts.py @@ -5,7 +5,7 @@ from sqlmodel.ext.asyncio.session import AsyncSession from ...datasources.db.database import database_session -from ...datasources.db.models import Abi, Contract +from ...datasources.db.models import Abi, AbiSource, Contract from ...main import app from ..db.db_async_conn import DbAsyncConn from ..mocks.abi_mock import mock_abi_json @@ -20,8 +20,9 @@ def setUpClass(cls): @database_session async def test_view_contracts(self, session: AsyncSession): - - abi = Abi(abi_json=mock_abi_json) + source = AbiSource(name="Etherscan", url="https://api.etherscan.io/api") + await source.create(session) + abi = Abi(abi_json=mock_abi_json, source_id=source.id) await abi.create(session) address_expected = "0x6eEF70Da339a98102a642969B3956DEa71A1096e" address = HexBytes(address_expected) diff --git a/scripts/fill_db.py b/scripts/fill_db.py new file mode 100644 index 0000000..e69de29