List of existing requests check moved to its own route

pull/1062/head
kompotkot 2024-05-01 10:20:25 +00:00
rodzic 12345e42a3
commit e1c62d9932
2 zmienionych plików z 49 dodań i 31 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional, Set, Tuple
from uuid import UUID
from bugout.data import BugoutResource
@ -329,6 +329,10 @@ class CallRequestResponse(BaseModel):
return Web3.toChecksumAddress(v)
class CallRequestsCheck(BaseModel):
existing_requests: Set[Tuple[str, str]] = Field(default_factory=set)
class CompleteCallRequestsAPIRequest(BaseModel):
tx_hash: str

Wyświetl plik

@ -318,6 +318,45 @@ async def list_requests_route(
return [contracts_actions.parse_call_request_response(r) for r in requests]
@app.get(
"/requests/check",
response_model=data.CallRequestsCheck,
)
async def check_requests_route(
request_data: data.CreateCallRequestsAPIRequest = Body(...),
user: BugoutUser = Depends(request_user_auth),
db_session: Session = Depends(db.yield_db_session),
) -> data.CallRequestsCheck:
"""
Implemented for pre-check until list of requests to be pushed into database.
"""
try:
incoming_requests: Set[Tuple[str, str]] = {
(Web3.toChecksumAddress(r.caller), r.request_id)
for r in request_data.specifications
}
existing_requests = contracts_actions.get_call_request_from_tuple(
db_session=db_session,
metatx_requester_id=user.id,
requests=incoming_requests,
contract_id=request_data.contract_id,
contract_address=request_data.contract_address,
)
except Exception as err:
logger.error(repr(err))
raise EngineHTTPException(status_code=500)
existing_requests_set: Set[Tuple[str, str]] = set()
if len(existing_requests) != 0:
existing_requests_set = {
(er.caller, str(er.request_id)) for er in existing_requests
}
return data.CallRequestsCheck(
existing_requests=existing_requests_set,
)
@app.get(
"/requests/{request_id}", tags=["requests"], response_model=data.CallRequestResponse
)
@ -353,46 +392,21 @@ async def create_requests(
data: data.CreateCallRequestsAPIRequest = Body(...),
user: BugoutUser = Depends(request_user_auth),
db_session: Session = Depends(db.yield_db_session),
verify: bool = Query(False),
) -> int:
"""
Allows API user to register call requests from given contract details, TTL, and call specifications.
At least one of `contract_id` or `contract_address` must be provided in the request body.
"""
if verify is True:
try:
requests: Set[Tuple[str, str]] = {
(Web3.toChecksumAddress(r.caller), r.request_id)
for r in data.specifications
}
existing_requests = contracts_actions.get_call_request_from_tuple(
db_session=db_session,
metatx_requester_id=user.id,
requests=requests,
contract_id=data.contract_id,
contract_address=data.contract_address,
)
except Exception as err:
logger.error(repr(err))
raise EngineHTTPException(status_code=500)
if len(existing_requests) != 0:
existing_request_ids = [str(r.request_id) for r in existing_requests]
raise EngineHTTPException(
status_code=409,
detail=f"Call request with request_id's: [{','.join(existing_request_ids)}] already registered",
)
try:
num_requests = contracts_actions.create_request_calls(
db_session=db_session,
metatx_requester_id=user.id,
registered_contract_id=data.contract_id,
contract_address=data.contract_address,
call_specs=data.specifications,
ttl_days=data.ttl_days,
live_at=data.live_at,
registered_contract_id=request_data.contract_id,
contract_address=request_data.contract_address,
call_specs=request_data.specifications,
ttl_days=request_data.ttl_days,
live_at=request_data.live_at,
)
except contracts_actions.InvalidAddressFormat as err:
raise EngineHTTPException(