diff --git a/src/fime/task_completer.py b/src/fime/task_completer.py index ad1471d..308b9e3 100644 --- a/src/fime/task_completer.py +++ b/src/fime/task_completer.py @@ -8,6 +8,8 @@ from queue import Queue, Empty from loguru import logger from requests import Response +from fime.util import add_auth + try: from PySide6 import QtCore, QtWidgets except ImportError: @@ -30,6 +32,8 @@ class TaskCompleter(QtWidgets.QCompleter): self.setFilterMode(QtCore.Qt.MatchFlag.MatchContains) self.setCaseSensitivity(QtCore.Qt.CaseInsensitive) self.session = FuturesSession() + self.session.headers["Accept"] = "application/json" + add_auth(config, self.session) self.config = config self.picker_url = None self.search_url = None @@ -81,7 +85,7 @@ class TaskCompleter(QtWidgets.QCompleter): def update_picker(self, text): self.text = text if self.text == self.currentCompletion(): - # do not update, after auto completion was used + # do not update, after auto-completion was used return if self.escalate: self.update_search() @@ -93,10 +97,6 @@ class TaskCompleter(QtWidgets.QCompleter): params={ "query": self.text }, - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, ) with self.rif_counter_lock: self.rif_counter += 1 @@ -133,10 +133,6 @@ class TaskCompleter(QtWidgets.QCompleter): "maxResults": 10, "fields": "key,summary", }, - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, ) with self.rif_counter_lock: self.rif_counter += 1 @@ -162,13 +158,7 @@ class TaskCompleter(QtWidgets.QCompleter): stripped = self.text.strip() if not self.issue_key_regex.match(stripped): return - future = self.session.get( - self.issue_url_tmpl.format(stripped.upper()), - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, - ) + future = self.session.get(self.issue_url_tmpl.format(stripped.upper())) with self.rif_counter_lock: self.rif_counter += 1 future.add_done_callback(partial(self.issue_response_callback, self.text)) diff --git a/src/fime/util.py b/src/fime/util.py index fb66ef7..b579bd6 100644 --- a/src/fime/util.py +++ b/src/fime/util.py @@ -1,6 +1,9 @@ import enum from loguru import logger +from requests import Session + +from fime.config import Config, AuthMethods try: from PySide6 import QtCore, QtGui, QtWidgets @@ -51,3 +54,13 @@ class Status(enum.Enum): PROGRESS = enum.auto() OK = enum.auto() ERROR = enum.auto() + + +def add_auth(config: Config, session: Session): + match config.auth_mode: + case AuthMethods.TOKEN: + session.headers["Authorization"] = f"Bearer {config.jira_token}" + case AuthMethods.COOKIES: + raise NotImplemented + case _: + raise AssertionError("Unknown auth method") diff --git a/src/fime/worklog_rest.py b/src/fime/worklog_rest.py index a04a2e9..820a1d3 100644 --- a/src/fime/worklog_rest.py +++ b/src/fime/worklog_rest.py @@ -12,7 +12,7 @@ from requests_futures.sessions import FuturesSession from fime.config import Config from fime.exceptions import FimeException -from fime.util import Status +from fime.util import Status, add_auth class WorklogRest: @@ -23,6 +23,8 @@ class WorklogRest: self.worklog_url = os.path.join(config.jira_url, "rest/api/2/issue/{}/worklog") self.worklog_update_url = os.path.join(config.jira_url, "rest/api/2/issue/{issue_key}/worklog/{worklog_id}") self.session = FuturesSession() + self.session.headers["Accept"] = "application/json" + add_auth(config, self.session) self._user = None self._user_future = self._req_user() self._issue_state: Dict[str, Tuple[Status, str]] = dict() @@ -31,13 +33,7 @@ class WorklogRest: self._issues_lock = Lock() def _req_user(self): - future = self.session.get( - self.user_url, - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, - ) + future = self.session.get(self.user_url) future.add_done_callback(self._resp_user) return future @@ -60,13 +56,7 @@ class WorklogRest: return ret def _req_issue(self, issue_key: str, pdate: date): - future = self.session.get( - self.issue_url.format(issue_key), - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, - ) + future = self.session.get(self.issue_url.format(issue_key)) future.add_done_callback(partial(self._resp_issue, issue_key, pdate)) def _resp_issue(self, issue_key: str, pdate: date, future: Future): @@ -79,13 +69,7 @@ class WorklogRest: self._issue_state[issue_key] = (Status.ERROR, "Could not find specified issue") def _req_worklog_check(self, issue_key: str, issue_title: str, pdate: date): - future = self.session.get( - self.worklog_url.format(issue_key), - headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", - }, - ) + future = self.session.get(self.worklog_url.format(issue_key)) future.add_done_callback(partial(self._resp_worklog_check, issue_key, issue_title, pdate)) def _resp_worklog_check(self, issue_key: str, issue_title: str, pdate: date, future: Future): @@ -144,8 +128,6 @@ class WorklogRest: future = self.session.post( self.worklog_url.format(issue_key), headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", "Content-Type": "application/json", }, json={ @@ -161,8 +143,6 @@ class WorklogRest: future = self.session.put( self.worklog_update_url.format(issue_key=issue_key, worklog_id=worklog_id), headers={ - "Authorization": f"Bearer {self.config.jira_token}", - "Accept": "application/json", "Content-Type": "application/json", }, json={