Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions client.py
Original file line number Diff line number Diff line change
@@ -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
return (
"id" in payload
and "name" in payload
and payload.get("email", "").count("@") == 1
)
4 changes: 2 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Any, Dict, Optional


Expand All @@ -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)
9 changes: 6 additions & 3 deletions repo.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import time
from typing import Any, Dict



class InMemoryRepo:
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
return self.storage

2 changes: 1 addition & 1 deletion service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}