mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-10-26 17:41:58 -06:00
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
"""SQLite-backed GitHub token persistence."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import closing
|
|
from typing import Optional
|
|
|
|
from .connection import SQLiteConnectionFactory
|
|
|
|
__all__ = ["SQLiteGitHubRepository"]
|
|
|
|
|
|
class SQLiteGitHubRepository:
|
|
"""Store and retrieve GitHub API tokens for the Engine."""
|
|
|
|
def __init__(
|
|
self,
|
|
connection_factory: SQLiteConnectionFactory,
|
|
*,
|
|
logger: Optional[logging.Logger] = None,
|
|
) -> None:
|
|
self._connections = connection_factory
|
|
self._log = logger or logging.getLogger("borealis.engine.repositories.github")
|
|
|
|
def load_token(self) -> Optional[str]:
|
|
"""Return the stored GitHub token if one exists."""
|
|
|
|
with closing(self._connections()) as conn:
|
|
cur = conn.cursor()
|
|
cur.execute("SELECT token FROM github_token LIMIT 1")
|
|
row = cur.fetchone()
|
|
|
|
if not row:
|
|
return None
|
|
|
|
token = (row[0] or "").strip()
|
|
return token or None
|
|
|
|
def store_token(self, token: Optional[str]) -> None:
|
|
"""Persist *token*, replacing any prior value."""
|
|
|
|
normalized = (token or "").strip()
|
|
|
|
with closing(self._connections()) as conn:
|
|
cur = conn.cursor()
|
|
cur.execute("DELETE FROM github_token")
|
|
if normalized:
|
|
cur.execute("INSERT INTO github_token (token) VALUES (?)", (normalized,))
|
|
conn.commit()
|
|
|
|
self._log.info("stored-token has_token=%s", bool(normalized))
|
|
|