-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_server.py
More file actions
326 lines (259 loc) · 8.61 KB
/
Copy pathapi_server.py
File metadata and controls
326 lines (259 loc) · 8.61 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
REST API server for PresentaPulse
Provides API endpoints for programmatic access to animation generation
"""
from flask import Flask, request, jsonify, send_file
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from functools import wraps
import os
import logging
import hashlib
import secrets
from datetime import datetime, timedelta
from pathlib import Path
import json
from typing import Dict, Optional
import threading
from queue import Queue
try:
from werkzeug.security import check_password_hash, generate_password_hash
SECURITY_AVAILABLE = True
except ImportError:
SECURITY_AVAILABLE = False
logging.warning("werkzeug not available, using basic API key storage")
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('API_SECRET_KEY', secrets.token_hex(32))
# Rate limiting
limiter = Limiter(
app=app,
key_func=get_remote_address,
default_limits=["1000 per hour"],
storage_uri="memory://"
)
# API keys storage (in production, use a database)
API_KEYS_FILE = Path('api_keys.json')
api_keys: Dict[str, Dict] = {}
usage_stats: Dict[str, Dict] = {}
def load_api_keys():
"""Load API keys from file."""
global api_keys
if API_KEYS_FILE.exists():
try:
with open(API_KEYS_FILE, 'r') as f:
api_keys = json.load(f)
except Exception as e:
logging.error(f"Failed to load API keys: {e}")
api_keys = {}
def save_api_keys():
"""Save API keys to file."""
try:
with open(API_KEYS_FILE, 'w') as f:
json.dump(api_keys, f, indent=2)
except Exception as e:
logging.error(f"Failed to save API keys: {e}")
def load_usage_stats():
"""Load usage statistics."""
global usage_stats
stats_file = Path('api_usage_stats.json')
if stats_file.exists():
try:
with open(stats_file, 'r') as f:
usage_stats = json.load(f)
except Exception as e:
logging.error(f"Failed to load usage stats: {e}")
usage_stats = {}
def save_usage_stats():
"""Save usage statistics."""
stats_file = Path('api_usage_stats.json')
try:
with open(stats_file, 'w') as f:
json.dump(usage_stats, f, indent=2)
except Exception as e:
logging.error(f"Failed to save usage stats: {e}")
def track_usage(api_key: str, endpoint: str):
"""Track API usage."""
if api_key not in usage_stats:
usage_stats[api_key] = {
'total_requests': 0,
'endpoints': {},
'last_used': None
}
usage_stats[api_key]['total_requests'] += 1
usage_stats[api_key]['last_used'] = datetime.now().isoformat()
if endpoint not in usage_stats[api_key]['endpoints']:
usage_stats[api_key]['endpoints'][endpoint] = 0
usage_stats[api_key]['endpoints'][endpoint] += 1
save_usage_stats()
def require_api_key(f):
"""Decorator to require API key authentication."""
@wraps(f)
def decorated_function(*args, **kwargs):
api_key = request.headers.get('X-API-Key') or request.args.get('api_key')
if not api_key:
return jsonify({'error': 'API key required'}), 401
if api_key not in api_keys:
return jsonify({'error': 'Invalid API key'}), 401
# Check if key is active
if not api_keys[api_key].get('active', True):
return jsonify({'error': 'API key is inactive'}), 403
# Track usage
track_usage(api_key, request.endpoint)
return f(*args, **kwargs)
return decorated_function
# Initialize on startup
load_api_keys()
load_usage_stats()
@app.route('/api/health', methods=['GET'])
def health_check():
"""Health check endpoint."""
return jsonify({
'status': 'healthy',
'timestamp': datetime.now().isoformat(),
'version': '1.0.1'
})
@app.route('/api/v1/generate', methods=['POST'])
@limiter.limit("10 per minute")
@require_api_key
def generate_animation():
"""
Generate animation from image and video.
Request body:
{
"image_path": "path/to/image.jpg",
"video_path": "path/to/video.mp4",
"parameters": {
"relative_motion": true,
"do_crop": true,
"remap": true,
"crop_driving_video": false,
"smoothing_strength": 0.0,
"denoise_strength": 0.0,
"stabilize": false
}
}
Response:
{
"job_id": "abc123",
"status": "processing",
"estimated_time": 60
}
"""
try:
data = request.get_json()
if not data or 'image_path' not in data or 'video_path' not in data:
return jsonify({'error': 'Missing required fields: image_path, video_path'}), 400
# Generate job ID
job_id = secrets.token_hex(8)
# Queue job for processing (implement actual processing queue)
# For now, return job ID
return jsonify({
'job_id': job_id,
'status': 'queued',
'message': 'Job queued for processing',
'estimated_time': 60 # seconds
}), 202
except Exception as e:
logging.error(f"Error in generate_animation: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/api/v1/job/<job_id>', methods=['GET'])
@require_api_key
def get_job_status(job_id: str):
"""Get job status."""
# Implement job status checking
return jsonify({
'job_id': job_id,
'status': 'completed',
'progress': 100,
'output_path': f'/api/v1/download/{job_id}'
})
@app.route('/api/v1/download/<job_id>', methods=['GET'])
@require_api_key
def download_result(job_id: str):
"""Download generated video."""
# Implement file download
return jsonify({'error': 'Not implemented'}), 501
@app.route('/api/v1/keys', methods=['POST'])
@limiter.limit("5 per hour")
def create_api_key():
"""
Create a new API key.
Request body:
{
"name": "My Application",
"rate_limit": 100
}
"""
try:
data = request.get_json() or {}
name = data.get('name', 'Unnamed')
rate_limit = data.get('rate_limit', 100)
# Generate API key
api_key = secrets.token_urlsafe(32)
# Store API key
api_keys[api_key] = {
'name': name,
'created_at': datetime.now().isoformat(),
'active': True,
'rate_limit': rate_limit
}
save_api_keys()
return jsonify({
'api_key': api_key,
'name': name,
'created_at': api_keys[api_key]['created_at'],
'rate_limit': rate_limit
}), 201
except Exception as e:
logging.error(f"Error creating API key: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/api/v1/keys/<api_key>', methods=['DELETE'])
@require_api_key
def revoke_api_key(api_key: str):
"""Revoke an API key."""
if api_key in api_keys:
api_keys[api_key]['active'] = False
save_api_keys()
return jsonify({'message': 'API key revoked'}), 200
return jsonify({'error': 'API key not found'}), 404
@app.route('/api/v1/stats', methods=['GET'])
@require_api_key
def get_usage_stats():
"""Get usage statistics for current API key."""
api_key = request.headers.get('X-API-Key') or request.args.get('api_key')
if api_key in usage_stats:
return jsonify(usage_stats[api_key]), 200
return jsonify({
'total_requests': 0,
'endpoints': {},
'last_used': None
}), 200
@app.route('/api/v1/webhook', methods=['POST'])
@limiter.limit("20 per minute")
@require_api_key
def register_webhook():
"""
Register a webhook for async processing.
Request body:
{
"url": "https://example.com/webhook",
"events": ["job.completed", "job.failed"]
}
"""
try:
data = request.get_json()
if not data or 'url' not in data:
return jsonify({'error': 'Missing required field: url'}), 400
webhook_id = secrets.token_hex(8)
# Store webhook (implement webhook storage)
return jsonify({
'webhook_id': webhook_id,
'url': data['url'],
'events': data.get('events', ['job.completed']),
'status': 'active'
}), 201
except Exception as e:
logging.error(f"Error registering webhook: {e}")
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)