users.py 696 B

12345678910111213141516171819202122232425262728293031
  1. from typing import List
  2. import requests
  3. from dataclasses import dataclass
  4. @dataclass
  5. class User:
  6. id: int
  7. username: str
  8. class Users:
  9. def __init__(self) -> None:
  10. self.users = self.__get_users()
  11. def __get_users(self) -> List[User]:
  12. r = requests.get('https://jsonplaceholder.typicode.com/users')
  13. users = r.json()
  14. return [User(
  15. id=u['id'],
  16. username=u['username']) for u in users]
  17. def get_all(self) -> List[User]:
  18. return self.users
  19. def get_user_by_id(self, user_id: int) -> User:
  20. for u in self.users:
  21. if u.id == user_id:
  22. return u
  23. return User(666, 'Unknown user')