Skip to content

Commit fea9cff

Browse files
committed
Simplify webhook sending in debug mode
1 parent 7abeea6 commit fea9cff

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

packages/examples/cvat/exchange-oracle/debug.py

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
from typing import Any
88
from unittest import mock
99

10+
import inspect
11+
import uuid
1012
import uvicorn
1113
from httpx import URL
1214

1315
from src.chain.kvstore import register_in_kvstore
1416
from src.core.config import Config
1517
from src.db import SessionLocal
1618
from src.services import cloud
17-
from src.services import cvat as cvat_service
19+
from src.services import cvat as cvat_db_service
1820
from src.services.cloud import BucketAccessInfo
1921
from src.utils.logging import format_sequence, get_function_logger
22+
from src.utils.time import utcnow
2023

2124

2225
@contextmanager
@@ -28,7 +31,9 @@ def _mock_cvat_cloud_storage_params(logger: Logger) -> Generator[None, None, Non
2831
def patched_make_cvat_cloud_storage_params(bucket_info: BucketAccessInfo) -> dict:
2932
original_host_url = bucket_info.host_url
3033

31-
if Config.development_config.cvat_in_docker:
34+
if Config.development_config.cvat_in_docker and (
35+
"localhost" in original_host_url or "127.0.0.1" in original_host_url
36+
):
3237
bucket_info.host_url = str(
3338
URL(original_host_url).copy_with(
3439
host=Config.development_config.exchange_oracle_host
@@ -110,6 +115,8 @@ def _mock_webhook_signature_checking(_: Logger) -> Generator[None, None, None]:
110115
- from reputation oracle -
111116
encoded with Config.localhost.reputation_oracle_address wallet address
112117
or signature "reputation_oracle<number>"
118+
119+
<number> is optional in all cases.
113120
"""
114121

115122
from src.chain.escrow import (
@@ -133,6 +140,33 @@ def patched_get_available_webhook_types(chain_id, escrow_address):
133140
d[Config.localhost.reputation_oracle_address.lower()] = OracleWebhookTypes.reputation_oracle
134141
return d
135142

143+
from src.services.webhook import inbox as original_inbox
144+
145+
class PatchedInbox:
146+
def __init__(self):
147+
pass
148+
149+
def __getattr__(self, name: str):
150+
return getattr(original_inbox, name)
151+
152+
def create_webhook(
153+
self,
154+
session,
155+
escrow_address,
156+
chain_id,
157+
type: OracleWebhookTypes,
158+
signature = None,
159+
event_type = None,
160+
event_data = None,
161+
event = None,
162+
):
163+
if signature in OracleWebhookTypes:
164+
signature = f"{type.value}-{utcnow().isoformat(sep='T')}-{uuid.uuid4()}"
165+
166+
_orig_params = inspect.signature(original_inbox.create_webhook).parameters
167+
_args = {k: v for k, v in locals().items() if k in _orig_params}
168+
return original_inbox.create_webhook(**_args)
169+
136170
with (
137171
mock.patch("src.schemas.webhook.validate_address", lambda x: x),
138172
mock.patch(
@@ -143,6 +177,7 @@ def patched_get_available_webhook_types(chain_id, escrow_address):
143177
"src.endpoints.webhook.validate_oracle_webhook_signature",
144178
patched_validate_oracle_webhook_signature,
145179
),
180+
mock.patch("src.services.webhook.inbox", PatchedInbox())
146181
):
147182
yield
148183

@@ -165,7 +200,7 @@ def decode_plain_json_token(self, token) -> dict[str, Any]:
165200

166201
if (user_wallet := token_data.get("wallet_address")) and not token_data.get("email"):
167202
with SessionLocal.begin() as session:
168-
user = cvat_service.get_user_by_id(session, user_wallet)
203+
user = cvat_db_service.get_user_by_id(session, user_wallet)
169204
if not user:
170205
raise Exception(f"Could not find user with wallet address '{user_wallet}'")
171206

@@ -236,7 +271,30 @@ def apply_local_development_patches() -> Generator[None, None, None]:
236271
yield
237272

238273

239-
if __name__ == "__main__":
274+
def run_server():
275+
uvicorn.run(
276+
app="src:app",
277+
host="0.0.0.0", # noqa: S104
278+
port=int(Config.port),
279+
workers=Config.workers_amount,
280+
)
281+
282+
283+
# def fix_escrow():
284+
# from src.handlers.job_fixing import entrypoint
285+
286+
# return entrypoint()
287+
288+
289+
import sys
290+
from argparse import ArgumentParser
291+
292+
293+
def main(args: list[str] | None = None) -> int:
294+
parser = ArgumentParser()
295+
parser.add_argument("-e", "--entrypoint", default=run_server)
296+
parsed_args = parser.parse_args(args)
297+
240298
with ExitStack() as es:
241299
is_dev = Config.environment == "development"
242300
if is_dev:
@@ -245,9 +303,10 @@ def apply_local_development_patches() -> Generator[None, None, None]:
245303
Config.validate()
246304
register_in_kvstore()
247305

248-
uvicorn.run(
249-
app="src:app",
250-
host="0.0.0.0", # noqa: S104
251-
port=int(Config.port),
252-
workers=Config.workers_amount,
253-
)
306+
parsed_args.entrypoint()
307+
308+
return 0
309+
310+
311+
if __name__ == "__main__":
312+
sys.exit(main(sys.argv[1:]))

packages/examples/cvat/recording-oracle/debug.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from logging import Logger
55
from pathlib import PurePosixPath
66
from unittest import mock
7+
import inspect
8+
import uuid
79

810
import uvicorn
911

@@ -12,6 +14,7 @@
1214
from src.services import cloud
1315
from src.services.cloud import BucketAccessInfo
1416
from src.utils.logging import format_sequence, get_function_logger
17+
from src.utils.time import utcnow
1518

1619

1720
@contextmanager
@@ -109,6 +112,33 @@ def patched_get_available_webhook_types(chain_id, escrow_address):
109112
d[Config.localhost.exchange_oracle_address.lower()] = OracleWebhookTypes.exchange_oracle
110113
return d
111114

115+
from src.services.webhook import inbox as original_inbox
116+
117+
class PatchedInbox:
118+
def __init__(self):
119+
pass
120+
121+
def __getattr__(self, name: str):
122+
return getattr(original_inbox, name)
123+
124+
def create_webhook(
125+
self,
126+
session,
127+
escrow_address,
128+
chain_id,
129+
type: OracleWebhookTypes,
130+
signature = None,
131+
event_type = None,
132+
event_data = None,
133+
event = None,
134+
):
135+
if signature in OracleWebhookTypes:
136+
signature = f"{type.value}-{utcnow().isoformat(sep='T')}-{uuid.uuid4()}"
137+
138+
_orig_params = inspect.signature(original_inbox.create_webhook).parameters
139+
_args = {k: v for k, v in locals().items() if k in _orig_params}
140+
return original_inbox.create_webhook(**_args)
141+
112142
with (
113143
mock.patch("src.schemas.webhook.validate_address", lambda x: x),
114144
mock.patch(
@@ -119,6 +149,7 @@ def patched_get_available_webhook_types(chain_id, escrow_address):
119149
"src.endpoints.webhook.validate_oracle_webhook_signature",
120150
patched_validate_oracle_webhook_signature,
121151
),
152+
mock.patch("src.services.webhook.inbox", PatchedInbox())
122153
):
123154
yield
124155

0 commit comments

Comments
 (0)