12345678910111213141516171819202122232425262728293031 |
- from typing import List
- import requests
- from dataclasses import dataclass
- @dataclass
- class User:
- id: int
- username: str
- class Users:
- def __init__(self) -> None:
- self.users = self.__get_users()
- def __get_users(self) -> List[User]:
- r = requests.get('https://jsonplaceholder.typicode.com/users')
- users = r.json()
- return [User(
- id=u['id'],
- username=u['username']) for u in users]
- def get_all(self) -> List[User]:
- return self.users
- def get_user_by_id(self, user_id: int) -> User:
- for u in self.users:
- if u.id == user_id:
- return u
- return User(666, 'Unknown user')
|