-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
279 lines (239 loc) · 10.3 KB
/
api_client.py
File metadata and controls
279 lines (239 loc) · 10.3 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
import os
import json
import yaml
import random
import time
import threading
from collections import defaultdict
from datetime import datetime
class ConfigManager:
def __init__(self):
self.configs = {}
self.environments = ['development', 'staging', 'production']
self.services = ['api', 'web', 'worker', 'scheduler', 'cache', 'database']
self.regions = ['us-east-1', 'us-west-2', 'eu-west-1', 'ap-southeast-1']
self.versions = defaultdict(int)
self.history = defaultdict(list)
self.lock = threading.Lock()
self.watchers = defaultdict(list)
def generate_config(self, service, environment):
config = {
'service': service,
'environment': environment,
'version': self.versions[f"{service}_{environment}"] + 1,
'created_at': datetime.now().isoformat(),
'updated_at': datetime.now().isoformat(),
'settings': self.generate_settings(service),
'features': self.generate_features(),
'limits': self.generate_limits(),
'endpoints': self.generate_endpoints(service),
'credentials': self.generate_credentials(),
'logging': self.generate_logging_config(),
'monitoring': self.generate_monitoring_config()
}
return config
def generate_settings(self, service):
settings = {
'api': {
'host': '0.0.0.0',
'port': random.choice([3000, 8080, 8000, 5000]),
'workers': random.randint(2, 8),
'timeout': random.randint(30, 120),
'rate_limit': random.randint(100, 1000),
'cors_enabled': random.choice([True, False])
},
'web': {
'host': '0.0.0.0',
'port': random.choice([80, 443, 3000]),
'static_path': '/var/www/static',
'session_timeout': random.randint(3600, 86400),
'max_upload_size': random.choice([10485760, 104857600, 1073741824])
},
'worker': {
'concurrency': random.randint(5, 20),
'max_retries': random.randint(3, 10),
'queue': f"queue_{service}",
'prefetch_count': random.randint(5, 50)
},
'cache': {
'max_memory': random.choice(['256mb', '512mb', '1gb', '2gb']),
'max_keys': random.randint(10000, 100000),
'eviction_policy': random.choice(['lru', 'lfu', 'ttl']),
'ttl': random.randint(300, 3600)
},
'database': {
'pool_size': random.randint(10, 50),
'max_connections': random.randint(50, 200),
'statement_timeout': random.randint(30000, 60000),
'idle_timeout': random.randint(10000, 30000)
}
}
return settings.get(service, settings['api'])
def generate_features(self):
features = {}
feature_names = [
'new_dashboard', 'beta_api', 'advanced_search', 'realtime_updates',
'batch_processing', 'export_data', 'import_data', 'webhooks',
'sso_integration', 'audit_logs', 'data_retention', 'custom_domains'
]
for feature in feature_names:
features[feature] = {
'enabled': random.choice([True, False]),
'rollout_percentage': random.randint(0, 100),
'description': f"Feature flag for {feature}"
}
return features
def generate_limits(self):
return {
'rate_limit': random.randint(1000, 10000),
'burst_limit': random.randint(100, 1000),
'concurrent_requests': random.randint(50, 500),
'max_file_size': random.choice([10485760, 104857600, 5368709120]),
'max_batch_size': random.randint(100, 1000),
'timeout_seconds': random.randint(30, 300)
}
def generate_endpoints(self, service):
endpoints = []
endpoint_templates = {
'api': ['/v1/users', '/v1/products', '/v1/orders', '/v1/search', '/health'],
'web': ['/', '/dashboard', '/settings', '/profile', '/login'],
'worker': ['/jobs', '/queue', '/status', '/metrics'],
'cache': ['/cache', '/stats', '/flush'],
'database': ['/query', '/migrate', '/backup', '/restore']
}
for path in endpoint_templates.get(service, endpoint_templates['api']):
endpoints.append({
'path': path,
'methods': random.sample(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
random.randint(1, 3)),
'auth_required': random.choice([True, False]),
'rate_limited': random.choice([True, False])
})
return endpoints
def generate_credentials(self):
return {
'api_key': f"key_{random.randint(1000000000, 9999999999):x}",
'secret_key': f"sec_{random.randint(1000000000, 9999999999):x}",
'jwt_secret': f"jwt_{random.randint(1000000000, 9999999999):x}",
'encryption_key': f"enc_{random.randint(1000000000, 9999999999):x}"
}
def generate_logging_config(self):
return {
'level': random.choice(['DEBUG', 'INFO', 'WARNING', 'ERROR']),
'format': random.choice(['json', 'text', 'structured']),
'output': random.choice(['stdout', 'file', 'syslog', 'elasticsearch']),
'sample_rate': random.random(),
'include_headers': random.choice([True, False]),
'include_body': random.choice([True, False])
}
def generate_monitoring_config(self):
return {
'metrics_enabled': True,
'tracing_enabled': random.choice([True, False]),
'sampling_rate': random.uniform(0.1, 1.0),
'exporters': random.sample(['prometheus', 'datadog', 'newrelic', 'jaeger'],
random.randint(1, 3))
}
def set_config(self, service, environment, config=None):
key = f"{service}_{environment}"
with self.lock:
if config is None:
config = self.generate_config(service, environment)
self.versions[key] = config['version']
self.configs[key] = config
history_entry = {
'version': config['version'],
'timestamp': time.time(),
'config': config.copy()
}
self.history[key].append(history_entry)
if len(self.history[key]) > 20:
self.history[key] = self.history[key][-20:]
self.notify_watchers(key, config)
return config
def get_config(self, service, environment, version=None):
key = f"{service}_{environment}"
if version is not None:
for entry in self.history[key]:
if entry['version'] == version:
return entry['config']
return None
return self.configs.get(key)
def watch_config(self, key, callback):
self.watchers[key].append(callback)
def notify_watchers(self, key, config):
for callback in self.watchers[key]:
try:
callback(config)
except:
pass
def delete_config(self, service, environment):
key = f"{service}_{environment}"
with self.lock:
if key in self.configs:
del self.configs[key]
return True
return False
def list_configs(self):
configs = []
for key, config in self.configs.items():
configs.append({
'key': key,
'service': config['service'],
'environment': config['environment'],
'version': config['version'],
'updated_at': config['updated_at']
})
return configs
def validate_config(self, config):
required_fields = ['service', 'environment', 'version', 'settings']
for field in required_fields:
if field not in config:
return False, f"Missing required field: {field}"
return True, "Valid"
def export_config(self, service, environment, format='json'):
config = self.get_config(service, environment)
if config is None:
return None
if format == 'json':
return json.dumps(config, indent=2)
elif format == 'yaml':
return yaml.dump(config)
else:
return config
def import_config(self, data, format='json'):
try:
if format == 'json':
config = json.loads(data)
elif format == 'yaml':
config = yaml.safe_load(data)
else:
return None
valid, message = self.validate_config(config)
if not valid:
return {'error': message}
result = self.set_config(
config['service'],
config['environment'],
config
)
return result
except Exception as e:
return {'error': str(e)}
def run_simulation(self):
for _ in range(20):
service = random.choice(self.services)
env = random.choice(self.environments)
region = random.choice(self.regions)
config = self.generate_config(service, env)
config['region'] = region
self.set_config(service, env, config)
time.sleep(random.uniform(0.01, 0.05))
return self.list_configs()
def main():
cm = ConfigManager()
configs = cm.run_simulation()
print(f"Config manager: {len(configs)} configurations managed")
if __name__ == "__main__":
main()