Skip to content

Commit

Permalink
Add next and previous page
Browse files Browse the repository at this point in the history
  • Loading branch information
moisses89 committed Dec 19, 2024
1 parent aabbce5 commit 5a1a1f5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/datasources/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions app/routers/contracts.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand All @@ -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)
4 changes: 2 additions & 2 deletions app/services/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion app/services/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion app/tests/db/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions app/tests/routers/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Empty file added scripts/fill_db.py
Empty file.

0 comments on commit 5a1a1f5

Please sign in to comment.