# Copyright (c) 2019 Nico Alt # SPDX-License-Identifier: AGPL-3.0-only # License-Filename: LICENSE.md import os from threading import Thread from briar_wrapper.api_thread import ApiThread from briar_wrapper.constants import BRIAR_AUTH_TOKEN, BRIAR_DB from briar_wrapper.models.socket_listener import SocketListener class Api: auth_token = None socket_listener = None _api_thread = None def __init__(self, headless_jar): self._api_thread = ApiThread(self, headless_jar) @staticmethod def has_account(): return os.path.isfile(BRIAR_DB) def is_running(self): return self._api_thread.is_running() def login(self, password, callback): self._start_and_watch(callback) startup_thread = Thread(target=self._api_thread.login, args=(password,), daemon=True) startup_thread.start() def register(self, credentials, callback): if len(credentials) != 2: raise Exception("Can't process credentials") self._start_and_watch(callback) startup_thread = Thread(target=self._api_thread.register, args=(credentials,), daemon=True) startup_thread.start() def stop(self): self._api_thread.stop() def _start_and_watch(self, callback): self._api_thread.start() self._api_thread.watch(callback) def on_successful_startup(self, callback): self._load_auth_token() self.socket_listener = SocketListener(self) callback(True) def _load_auth_token(self): if not Api.has_account(): raise Exception("Can't load authentication token") with open(BRIAR_AUTH_TOKEN, 'r') as file: self.auth_token = file.read()