From c2c1723a16e0701326c36df2b62977a66c79873a Mon Sep 17 00:00:00 2001 From: Andrey Chernov <почта@почта.ru> Date: Mon, 2 Mar 2026 21:53:21 +0300 Subject: [PATCH] =?UTF-8?q?=D1=87=D1=91=D1=82=20=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.py | 15 +++++++++------ models.py | 4 ++-- repo.py | 9 ++++++--- service.py | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) 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 + }