Browse Source

Update configs/__init__.py: add apollo

StrayWarrior 1 day ago
parent
commit
14dddf2f45
1 changed files with 40 additions and 3 deletions
  1. 40 3
      configs/__init__.py

+ 40 - 3
configs/__init__.py

@@ -1,10 +1,12 @@
 #! /usr/bin/env python
 # -*- coding: utf-8 -*-
 # vim:fenc=utf-8
-
-
+import json
 import os
 import yaml
+import pyapollos
+
+APOLLO_PROJECT_NAME = 'ai-agent'
 
 def get():
     dirname = os.path.dirname(os.path.abspath(__file__))
@@ -15,4 +17,39 @@ def get():
 
 def get_env():
     env = os.environ.get('AI_AGENT_ENV', 'dev')
-    return env
+    return env
+
+class ApolloConfig(object):
+    def __init__(self, env):
+        match env:
+            case "dev":
+                self.apollo_connection = pyapollos.ApolloClient(
+                    app_id=APOLLO_PROJECT_NAME,
+                    config_server_url="https://apolloconfig-internal.piaoquantv.com/",
+                    timeout=10
+                )
+            case "pre":
+                self.apollo_connection = pyapollos.ApolloClient(
+                    app_id=APOLLO_PROJECT_NAME,
+                    config_server_url="http://preapolloconfig-internal.piaoquantv.com/",
+                    timeout=10
+                )
+            case "prod":
+                self.apollo_connection = pyapollos.ApolloClient(
+                    app_id=APOLLO_PROJECT_NAME,
+                    config_server_url="https://apolloconfig-internal.piaoquantv.com/",
+                    timeout=10
+                )
+
+    def get_value(self, key, default=None):
+        response = self.apollo_connection.get_value(key, default)
+        return response
+
+    def get_json_value(self, key, default=None):
+        value = self.get_value(key)
+        if not value:
+            return default
+        else:
+            return json.loads(value)
+
+apollo_config = ApolloConfig(get_env())