__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. import json
  5. import os
  6. import yaml
  7. import pyapollos
  8. APOLLO_PROJECT_NAME = 'ai-agent'
  9. def get():
  10. dirname = os.path.dirname(os.path.abspath(__file__))
  11. env = os.environ.get('AI_AGENT_ENV', 'dev')
  12. if env not in ('dev', 'pre', 'prod'):
  13. raise ValueError(f"Invalid environment: {env}. Expected one of ('dev', 'pre', 'prod').")
  14. return yaml.safe_load(open(f'{dirname}/{env}.yaml').read())
  15. def get_env():
  16. env = os.environ.get('AI_AGENT_ENV', 'dev')
  17. return env
  18. class ApolloConfig(object):
  19. def __init__(self, env):
  20. match env:
  21. case "dev":
  22. self.apollo_connection = pyapollos.ApolloClient(
  23. app_id=APOLLO_PROJECT_NAME,
  24. config_server_url="http://devapolloconfig-internal.piaoquantv.com/",
  25. timeout=10
  26. )
  27. case "pre":
  28. self.apollo_connection = pyapollos.ApolloClient(
  29. app_id=APOLLO_PROJECT_NAME,
  30. config_server_url="http://preapolloconfig-internal.piaoquantv.com/",
  31. timeout=10
  32. )
  33. case "prod":
  34. self.apollo_connection = pyapollos.ApolloClient(
  35. app_id=APOLLO_PROJECT_NAME,
  36. config_server_url="https://apolloconfig-internal.piaoquantv.com/",
  37. timeout=10
  38. )
  39. def get_value(self, key, default=None):
  40. response = self.apollo_connection.get_value(key, default)
  41. return response
  42. def get_json_value(self, key, default=None):
  43. value = self.get_value(key)
  44. if not value:
  45. return default
  46. else:
  47. return json.loads(value)
  48. apollo_config = ApolloConfig(get_env())