Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
565 lines (488 loc) · 17 KB
/
Copy pathmain.py
File metadata and controls
565 lines (488 loc) · 17 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
"""
MBTQ Auto-API - FastAPI Backend
================================
Main FastAPI application for the MBTQ API Autopilot system.
Integrates with DeafAUTH, Fibonrose, and PinkSync services.
"""
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from typing import Optional, List, Dict, Any
from datetime import datetime, timedelta
import httpx
import logging
from models import (
APIEntry,
APIFilter,
CodeGenerationRequest,
CodeGenerationResponse,
DeploymentRequest,
DeploymentResponse,
AuthRequest,
AuthResponse,
FibonroseLog
)
from services.deafauth import DeafAuthService
from services.fibonrose import FibonroseService
from services.pinksync import PinkSyncService
from services.code_generator import CodeGeneratorService
from services.curated_apis import CuratedAPIsService
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(
title="MBTQ Auto-API",
description="Visual-first API integration with auto-deployment to mbtq.dev",
version="1.0.0",
docs_url="/api/docs",
redoc_url="/api/redoc"
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify actual origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize services
deafauth_service = DeafAuthService()
fibonrose_service = FibonroseService()
pinksync_service = PinkSyncService()
code_generator_service = CodeGeneratorService()
curated_apis_service = CuratedAPIsService()
# Dependency for authentication
async def verify_auth(x_mbtq_token: Optional[str] = Header(None)) -> dict:
"""Verify DeafAUTH token and return user info"""
if not x_mbtq_token:
raise HTTPException(status_code=401, detail="DeafAUTH token required")
user = await deafauth_service.verify_token(x_mbtq_token)
if not user:
raise HTTPException(status_code=401, detail="Invalid DeafAUTH token")
return user
@app.get("/")
async def root():
"""Root endpoint with API information"""
return {
"name": "MBTQ Auto-API",
"version": "1.0.0",
"status": "operational",
"services": {
"deafauth": "🔐 Active",
"fibonrose": "🌹 Active",
"pinksync": "⚡ Active"
},
"docs": "/api/docs",
"timestamp": datetime.utcnow().isoformat()
}
@app.get("/api/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"timestamp": datetime.utcnow().isoformat(),
"services": {
"deafauth": await deafauth_service.health_check(),
"fibonrose": await fibonrose_service.health_check(),
"pinksync": await pinksync_service.health_check(),
"curated_apis": await curated_apis_service.health_check()
}
}
@app.post("/api/auth/login", response_model=AuthResponse)
async def login(auth_request: AuthRequest):
"""
Authenticate user with DeafAUTH
"""
try:
# Log authentication attempt
await fibonrose_service.log_event(
action="auth_attempt",
metadata={"username": auth_request.username},
user="system"
)
# Authenticate with DeafAUTH
result = await deafauth_service.authenticate(
auth_request.username,
auth_request.password
)
if result["success"]:
# Log successful authentication
await fibonrose_service.log_event(
action="auth_success",
metadata={"username": auth_request.username},
user=auth_request.username
)
return AuthResponse(
success=True,
token=result["token"],
username=auth_request.username,
message="DeafAUTH authentication successful"
)
else:
raise HTTPException(status_code=401, detail="Authentication failed")
except Exception as e:
logger.error(f"Authentication error: {str(e)}")
await fibonrose_service.log_event(
action="auth_error",
metadata={"username": auth_request.username, "error": str(e)},
user="system"
)
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/entries", response_model=List[APIEntry])
async def get_api_entries(
category: Optional[str] = None,
search: Optional[str] = None,
auth: Optional[str] = None,
https: Optional[bool] = None,
limit: int = 100
):
"""
Fetch and filter API entries from public-apis.org
"""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get("https://api.publicapis.org/entries")
response.raise_for_status()
data = response.json()
entries = data.get("entries", [])
# Apply filters
if category and category.lower() != "all":
entries = [e for e in entries if e.get("Category") == category]
if search:
search_lower = search.lower()
entries = [
e for e in entries
if search_lower in e.get("API", "").lower()
or search_lower in e.get("Description", "").lower()
]
if auth:
entries = [e for e in entries if e.get("Auth") == auth]
if https is not None:
entries = [e for e in entries if e.get("HTTPS") == https]
# Limit results
entries = entries[:limit]
# Log the request
await fibonrose_service.log_event(
action="api_entries_fetched",
metadata={
"count": len(entries),
"filters": {
"category": category,
"search": search,
"auth": auth,
"https": https
}
},
user="system"
)
return entries
except httpx.HTTPError as e:
logger.error(f"Error fetching API entries: {str(e)}")
raise HTTPException(status_code=502, detail="Failed to fetch API entries")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/categories")
async def get_categories():
"""
Get list of all available API categories
"""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get("https://api.publicapis.org/entries")
response.raise_for_status()
data = response.json()
entries = data.get("entries", [])
categories = sorted(list(set(e.get("Category", "Unknown") for e in entries)))
return {
"categories": ["All"] + categories,
"count": len(categories)
}
except Exception as e:
logger.error(f"Error fetching categories: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/github", response_model=List[Dict[str, Any]])
async def get_github_api_endpoints(
search: Optional[str] = None
):
"""
Get comprehensive GitHub REST API endpoints
Returns all GitHub API endpoints with detailed information including
available endpoints for each category (repositories, issues, pulls, etc.)
"""
try:
# Get GitHub API endpoints from curated service
endpoints = await curated_apis_service.get_github_endpoints(search=search)
# Log the request
await fibonrose_service.log_event(
action="github_api_endpoints_fetched",
metadata={
"count": len(endpoints),
"search": search
},
user="system"
)
return endpoints
except Exception as e:
logger.error(f"Error fetching GitHub API endpoints: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/enriched", response_model=List[Dict[str, Any]])
async def get_enriched_apis(
search: Optional[str] = None,
auth: Optional[str] = None,
limit: int = 100
):
"""
Get enriched collection of high-quality development APIs
Returns curated collection of open-source and free development APIs including:
- Version control (GitLab, Bitbucket)
- Package registries (npm, PyPI, Maven)
- CI/CD platforms (CircleCI, Travis CI)
- Code quality tools (SonarQube, Codacy)
- Documentation (Stack Exchange, DevDocs)
- And many more...
"""
try:
# Get enriched APIs from curated service
apis = await curated_apis_service.get_enriched_apis(
search=search,
auth=auth,
limit=limit
)
# Log the request
await fibonrose_service.log_event(
action="enriched_apis_fetched",
metadata={
"count": len(apis),
"filters": {
"search": search,
"auth": auth
}
},
user="system"
)
return apis
except Exception as e:
logger.error(f"Error fetching enriched APIs: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/curated", response_model=List[Dict[str, Any]])
async def get_all_curated_apis(
include_github: bool = True,
include_enriched: bool = True,
search: Optional[str] = None,
limit: int = 200
):
"""
Get all curated APIs (GitHub + enriched development APIs)
Combines comprehensive GitHub REST API endpoints with a rich collection
of open-source and free development APIs to provide a one-stop catalog
for developers.
"""
try:
# Get all curated APIs
apis = await curated_apis_service.get_all_curated_apis(
include_github=include_github,
include_enriched=include_enriched,
search=search,
limit=limit
)
# Log the request
await fibonrose_service.log_event(
action="curated_apis_fetched",
metadata={
"count": len(apis),
"include_github": include_github,
"include_enriched": include_enriched,
"search": search
},
user="system"
)
return apis
except Exception as e:
logger.error(f"Error fetching curated APIs: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/generate", response_model=CodeGenerationResponse)
async def generate_code(
request: CodeGenerationRequest,
user: dict = Depends(verify_auth)
):
"""
Generate full-stack code for selected API
Requires DeafAUTH authentication
"""
try:
# Log code generation request
await fibonrose_service.log_event(
action="code_generation_started",
metadata={
"api_name": request.api_name,
"category": request.category
},
user=user["username"]
)
# Generate code using the code generator service
code = await code_generator_service.generate_fullstack_code(
api_name=request.api_name,
api_description=request.description,
api_link=request.link,
api_category=request.category,
api_auth=request.auth,
username=user["username"]
)
# Log successful generation
await fibonrose_service.log_event(
action="code_generation_completed",
metadata={
"api_name": request.api_name,
"code_length": len(code)
},
user=user["username"]
)
return CodeGenerationResponse(
success=True,
code=code,
api_name=request.api_name,
generated_at=datetime.utcnow().isoformat(),
mbtq_metadata={
"deafauth": "✅ Validated",
"fibonrose": "🌹 Logged",
"pinksync": "⚡ Ready"
}
)
except Exception as e:
logger.error(f"Code generation error: {str(e)}")
await fibonrose_service.log_event(
action="code_generation_error",
metadata={"error": str(e)},
user=user["username"]
)
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/deploy", response_model=DeploymentResponse)
async def deploy_api(
request: DeploymentRequest,
user: dict = Depends(verify_auth)
):
"""
Deploy generated API to mbtq.dev via PinkSync
Requires DeafAUTH authentication
"""
try:
# Log deployment initiation
await fibonrose_service.log_event(
action="deployment_started",
metadata={
"api_name": request.api_name,
"target": "mbtq.dev"
},
user=user["username"]
)
# Execute deployment via PinkSync
deployment_result = await pinksync_service.deploy(
api_name=request.api_name,
code=request.code,
config=request.config,
user=user["username"]
)
# Log deployment completion
await fibonrose_service.log_event(
action="deployment_completed",
metadata={
"api_name": request.api_name,
"deployment_url": deployment_result["url"],
"status": deployment_result["status"]
},
user=user["username"]
)
return DeploymentResponse(
success=True,
deployment_id=deployment_result["deployment_id"],
url=deployment_result["url"],
status=deployment_result["status"],
logs=deployment_result["logs"],
deployed_at=datetime.utcnow().isoformat()
)
except Exception as e:
logger.error(f"Deployment error: {str(e)}")
await fibonrose_service.log_event(
action="deployment_error",
metadata={
"api_name": request.api_name,
"error": str(e)
},
user=user["username"]
)
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/deployments/{deployment_id}")
async def get_deployment_status(
deployment_id: str,
user: dict = Depends(verify_auth)
):
"""
Get deployment status by ID
"""
try:
status = await pinksync_service.get_deployment_status(deployment_id)
return status
except Exception as e:
logger.error(f"Error fetching deployment status: {str(e)}")
raise HTTPException(status_code=404, detail="Deployment not found")
@app.get("/api/fibonrose/logs")
async def get_fibonrose_logs(
user: dict = Depends(verify_auth),
limit: int = 50
):
"""
Get Fibonrose activity logs for authenticated user
"""
try:
logs = await fibonrose_service.get_user_logs(
username=user["username"],
limit=limit
)
return {
"logs": logs,
"count": len(logs),
"user": user["username"]
}
except Exception as e:
logger.error(f"Error fetching logs: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/fibonrose/reputation")
async def get_reputation(user: dict = Depends(verify_auth)):
"""
Get user's Fibonrose reputation score
"""
try:
reputation = await fibonrose_service.get_reputation(user["username"])
return reputation
except Exception as e:
logger.error(f"Error fetching reputation: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
# Error handlers
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
"""Custom HTTP exception handler"""
return JSONResponse(
status_code=exc.status_code,
content={
"error": exc.detail,
"timestamp": datetime.utcnow().isoformat(),
"path": str(request.url)
}
)
@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
"""General exception handler"""
logger.error(f"Unhandled exception: {str(exc)}")
return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"timestamp": datetime.utcnow().isoformat(),
"path": str(request.url)
}
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)