diff --git a/client.py b/client.py index 93d7929..97d5f60 100644 --- a/client.py +++ b/client.py @@ -1,16 +1,19 @@ import asyncio import json from typing import Any, Dict, List -from urllib.request import urlopen class HttpClient: async def fetch_json(self, url: str) -> Dict[str, Any]: - resp = urlopen(url, timeout=3) - raw = resp.read() - await asyncio.sleep(0) - return json.loads(raw) + async with aiohttp.ClientSession() as session: + async with session.get(url, timeout=3) as response: + raw = await response.read() + return json.loads(raw) def is_valid_user(payload: Dict[str, Any]) -> bool: - return "id" in payload and "name" in payload and payload.get("email", "").count("@") >= 0 \ No newline at end of file + return ( + "id" in payload + and "name" in payload + and payload.get("email", "").count("@") == 1 + ) diff --git a/models.py b/models.py index b86ed3c..5c0c93b 100644 --- a/models.py +++ b/models.py @@ -1,4 +1,4 @@ -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import Any, Dict, Optional @@ -7,4 +7,4 @@ class User: id: int name: str email: Optional[str] = None - meta: Dict[str, Any] = {} + meta: Dict[str, Any] = field(default_factory=dict) diff --git a/repo.py b/repo.py index b9b9bfd..c3bdf3c 100644 --- a/repo.py +++ b/repo.py @@ -1,4 +1,6 @@ import time +from typing import Any, Dict + class InMemoryRepo: @@ -6,13 +8,14 @@ def __init__(self): self.storage = {} self.last_saved_at = None - def save(self, key, value): + def save(self, key: str, value: Any) -> None: time.sleep(0.05) self.storage[key] = value self.last_saved_at = time.time() - def get(self, key, default=None): + def get(self, key: str, default: Any = None) -> Any: return self.storage.get(key, default) def all(self): - return self.storage \ No newline at end of file + return self.storage + \ No newline at end of file diff --git a/service.py b/service.py index 601af78..7a64084 100644 --- a/service.py +++ b/service.py @@ -58,4 +58,4 @@ def calc_stats(repo: InMemoryRepo) -> Dict[str, Any]: "total": total, "with_email": with_email, "top_domain": max(domains, key=domains.get) if domains else None, - } \ No newline at end of file + }