class Service(StaffRoom): def __init__(self, num_employees: int = DEEFAULT_NUM_EMPLOYEES): self.__available_employees = Semaphore(num_employees) async def start_task(self) -> None: await self.__available_employees.acquire() async def stop_task(self) -> None: self.__available_employees.release()
class ApiSessionWithDialogs(): def __init__(self): self.session = CachedApiSession() self.login_semaphore = Semaphore() self.logged_in = False '''async def device_login(self): if not self.device_logged_in: localsecret = self.session_cache.get("localsecret") if localsecret is None: localsecret = await self.get("/user/device_login")["localsecret"] self.session_cache.set("localsecret", localsecret) else: await self.session.get("/user/device_login", {"localsecret" : localsecret}) self.device_logged_in = True''' async def get_with_login(self, method, params={}): try: result = await self.session.get(method, params) except LoginRequired: await self.login_semaphore.acquire() if not self.logged_in: await self.DoLogin() self.login_semaphore.release() return await self.session.get(method, params) return result async def get(self, method, params={}): try: result = await self.get_with_login(method, params) except Require2FA: async def On2FA(otp): result = await self.session.get("/user/otp", {"otp": otp}) print(result) dlg = Dialog2FA(Handle2FACoroutine=On2FA) await AsyncShowDialog(dlg) #otp = dlg.GetValue() result = await self.get_with_login(method, params) return result async def DoLogin(self): login_completed = Event() async def HandleLogin(username, password): result = await self.session.get("/user/login", { "email": username, "password": password }) login_completed.set() async def HandleCreateAccountStep1(username): await self.get("/user/register", {"email": username}) async def HandleCreateAccountStep2(confirmation_code, username, password, otp_secret): await self.get( "/user/register_confirm", { "confirmation_code": confirmation_code, "password": password, "otp_secret": otp_secret }) await self.session.get("/user/login", { "email": username, "password": password }) login_completed.set() login_dialog = LoginDialog( None, HandleLogin=HandleLogin, HandleCreateAccountStep1=HandleCreateAccountStep1, HandleCreateAccountStep2=HandleCreateAccountStep2) login_dialog.Show() await login_completed.wait() login_dialog.Hide()