consul.py 802 Bytes
Newer Older
Weizhi Cui's avatar
Weizhi Cui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/10/28 15:06
# @File        : consul.py
# @Description :
"""

from typing import Any

import consul
import yaml


class ConsulConfig:
    def __init__(self, host='localhost', port=8500, token=None, dc='dc1'):
        self.token = token
        self.dc = dc
        self.client = consul.Consul(host=host, port=port, token=token, dc=dc)

    def get(self, key: str) -> Any:
        _, data = self.client.kv.get(key=key, token=self.token, dc=self.dc)
        if data is None:
            raise KeyError(f'Key {key} not found in Consul.')
        return yaml.load(data['Value'], Loader=yaml.FullLoader)