ApolloClient.py 722 B

123456789101112131415161718
  1. import requests
  2. class ApolloClient(object):
  3. def __init__(self, base_url):
  4. self.base_url = base_url
  5. def get_config(self, app_id: str, cluster: str = "default", namespace: str = "application"):
  6. url = f"{self.base_url}/configs/{app_id}/{cluster}/{namespace}"
  7. print(f"请求的Apollo地址为: {url}")
  8. response = requests.get(url)
  9. return response.json()
  10. def get_value(self, app_id: str, cluster: str = "default", namespace: str = "application", key: str = None):
  11. if not key:
  12. raise ValueError("Key not empty")
  13. config_json = self.get_config(app_id=app_id, cluster=cluster, namespace=namespace)
  14. return config_json['configurations'][key]