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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.idea/
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
39 changes: 27 additions & 12 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import asyncio
import json
from typing import Any, Dict, List
from urllib.request import urlopen
from typing import Any, Dict
from httpx import AsyncClient
from models import User


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)
class InvalidUserException(Exception):
pass


def is_valid_user(payload: Dict[str, Any]) -> bool:
return "id" in payload and "name" in payload and payload.get("email", "").count("@") >= 0
class GetUserGateway:
def __init__(self, client: AsyncClient) -> None:
self._client = client

async def get_user(self, url: str) -> User:
response = await self._client.get(url, timeout=3)
response.raise_for_status()
body = response.json()

if not self._is_valid_user(body):
raise InvalidUserException

return User(
id=int(body["id"]),
name=body["name"].strip(),
email=body.get("email"),
meta=body.get("meta", {}),
)

@staticmethod
def _is_valid_user(payload: Dict[str, Any]) -> bool:
return "id" in payload and "name" in payload and "@" in payload.get("email", "")
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: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "refactor"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"httpx>=0.28.1",
]
12 changes: 7 additions & 5 deletions repo.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import asyncio
import time
from typing import Any


class InMemoryRepo:
def __init__(self):
self.storage = {}
self.last_saved_at = None

def save(self, key, value):
time.sleep(0.05)
async def save(self, key: int, value: Any) -> None:
await asyncio.sleep(0.05)
self.storage[key] = value
self.last_saved_at = time.time()

def get(self, key, default=None):
def get(self, key: int, default=None):
return self.storage.get(key, default)

def all(self):
return self.storage
def all(self) -> dict[int, Any]:
return self.storage
41 changes: 14 additions & 27 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,37 @@

from models import User
from repo import InMemoryRepo
from client import HttpClient, is_valid_user
from client import GetUserGateway, InvalidUserException


class UserService:
def __init__(self, repo: InMemoryRepo):
self.repo = repo
self.client = HttpClient()

def parse(self, data: Dict[str, Any]) -> User:
return User(
id=int(data["id"]),
name=data["name"].strip(),
email=data.get("email"),
meta=data.get("meta", {}),
)
def __init__(self, repo: InMemoryRepo, gateway: GetUserGateway):
self._repo = repo
self._gateway = gateway

async def sync_users(self, urls: List[str]) -> Tuple[int, List[str]]:
tasks = []
tasks = [self._gateway.get_user(url) for url in urls]
errors = []

for url in urls:
tasks.append(asyncio.create_task(self.client.fetch_json(url)))

results = await asyncio.gather(*tasks, return_exceptions=True)

for i, r in enumerate(results):
if isinstance(r, Exception):
errors.append(f"{urls[i]}: {r}")
continue

if not is_valid_user(r):
if isinstance(r, InvalidUserException):
errors.append(f"{urls[i]}: invalid payload")
continue

user = self.parse(r)
if isinstance(r, Exception):
errors.append(f"{urls[i]}: {r}")
continue

self.repo.save(user.id, user)
user: User = r
await self._repo.save(user.id, user)

return len(urls) - len(errors), errors


def calc_stats(repo: InMemoryRepo) -> Dict[str, Any]:
users = repo.all().values()
total = len(list(users))
users = list(repo.all().values())
with_email = len([u for u in users if u.email])
domains = {}
for u in users:
Expand All @@ -55,7 +42,7 @@ def calc_stats(repo: InMemoryRepo) -> Dict[str, Any]:
domains[d] = domains.get(d, 0) + 1

return {
"total": total,
"total": len(users),
"with_email": with_email,
"top_domain": max(domains, key=domains.get) if domains else None,
}
}
91 changes: 91 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.