taolib.github_app.hooks#

令牌事件回调的参考实现。

本模块提供两个开箱即用的 TokenEventHook 实现:

Classes#

LoggingTokenEventHook

将令牌生命周期事件写入 Python logging。

MetricsTokenEventHook

收集令牌事件计数指标。

Module Contents#

class taolib.github_app.hooks.LoggingTokenEventHook(logger: logging.Logger | None = None)#

将令牌生命周期事件写入 Python logging。

适用于开发调试与生产环境的审计日志。

初始化日志 hook。

参数:

logger -- 自定义 Logger 实例,为 None 时使用默认 logger。

async on_token_refresh_failed(cache_key: str, error: Exception) None#

记录令牌刷新失败事件。

参数:
  • cache_key -- 本次刷新对应的缓存键。

  • error -- 导致刷新失败的异常。

async on_token_refreshed(cache_key: str, result: taolib.github_app.models.InstallationTokenResult) None#

记录令牌刷新成功事件。

参数:
  • cache_key -- 本次刷新对应的缓存键。

  • result -- 刷新后的令牌结果。

class taolib.github_app.hooks.MetricsTokenEventHook#

收集令牌事件计数指标。

适用于 Prometheus 导出、测试断言或简易内存监控。

变量:
  • refresh_count -- 刷新成功的累计次数。

  • failure_count -- 刷新失败的累计次数。

  • last_refresh_key -- 最近一次成功刷新的缓存键。

初始化计数器为零。

async on_token_refresh_failed(cache_key: str, error: Exception) None#

递增刷新失败计数。

参数:
  • cache_key -- 本次刷新对应的缓存键。

  • error -- 导致刷新失败的异常。

async on_token_refreshed(cache_key: str, result: taolib.github_app.models.InstallationTokenResult) None#

递增刷新成功计数并记录缓存键。

参数:
  • cache_key -- 本次刷新对应的缓存键。

  • result -- 刷新后的令牌结果。

failure_count: int = 0#
last_refresh_key: str | None = None#
refresh_count: int = 0#