async def generate_direct_api_code(
language: str = "javascript",
framework: Optional[str] = None,
include_examples: bool = True
) -> Dict[str, Any]:
"""
Generate code that directly calls BaaS API by fetching templates from CDN
Perfect for: Production deployments, custom integrations, framework-specific implementations
Token-optimized: Fetches maintained templates from CDN instead of generating locally
Args:
language: Programming language (javascript, python, php, java, go, csharp)
framework: Optional framework (react, vue, django, laravel, fastapi, spring, etc.)
include_examples: Include usage examples and configuration templates
Returns:
Dictionary with code fetched from CDN, filename, and integration instructions
Code directly calls https://api.aiapp.link/api/message/ with X-API-KEY header authentication
If MCP server has BAAS_API_KEY set, it will be automatically injected into code
"""
try:
language = language.lower()
framework = framework.lower() if framework else None
# CDN base URL for templates
base_url = "https://cdn.mbaas.kr/templates/sms-mms"
# Construct template path
template_path = language
if framework:
template_path += f"/{framework}"
else:
template_path += "/vanilla"
template_url = f"{base_url}/{template_path}.md"
# Fetch template from CDN
try:
response = await client.get(template_url)
if response.status_code == 200:
template_content = response.text
# Extract code from markdown (assuming code is in ```language blocks)
import re
code_blocks = re.findall(f'```{language}(.*?)```', template_content, re.DOTALL)
if code_blocks:
code = code_blocks[0].strip()
else:
# Fallback: use entire content if no code blocks found
code = template_content
else:
return {
"success": False,
"error": f"CDN에서 템플릿을 가져올 수 없습니다 (HTTP {response.status_code})",
"cdn_url": template_url,
"error_code": "CDN_UNAVAILABLE"
}
except Exception as cdn_error:
return {
"success": False,
"error": f"CDN 연결 오류: {str(cdn_error)}",
"cdn_url": template_url,
"error_code": "CDN_CONNECTION_ERROR"
}
# Apply environment variable injection if API key is available
if BAAS_API_KEY:
code = code.replace('your-api-key', BAAS_API_KEY)
code = code.replace('process.env.BAAS_API_KEY', f"'{BAAS_API_KEY}'")
code = code.replace('os.getenv(\'BAAS_API_KEY\')', f"'{BAAS_API_KEY}'")
code = code.replace('$_ENV[\'BAAS_API_KEY\']', f"'{BAAS_API_KEY}'")
# File naming
extensions = {
"javascript": "js",
"js": "js",
"python": "py",
"py": "py",
"php": "php"
}
extension = extensions.get(language, language)
filename = f"baas-sms-service.{extension}"
# Configuration instructions
config_instructions = {
"javascript": {
"env_vars": ["BAAS_API_KEY"],
"install": "npm install (dependencies included in template)",
"usage": "Import and instantiate BaaSMessageService class",
"api_key_injected": bool(BAAS_API_KEY)
},
"python": {
"env_vars": ["BAAS_API_KEY"],
"install": "pip install requests",
"usage": "Import and instantiate BaaSMessageService class",
"api_key_injected": bool(BAAS_API_KEY)
},
"php": {
"env_vars": ["BAAS_API_KEY"],
"install": "cURL extension required (usually included)",
"usage": "Include file and instantiate BaaSMessageService class",
"api_key_injected": bool(BAAS_API_KEY)
}
}
return {
"success": True,
"language": language,
"framework": framework,
"code": code,
"filename": filename,
"description": f"{language.title()} BaaS SMS service for direct /api/message/ API calls",
"source": "CDN template",
"template_url": template_url,
"configuration": config_instructions.get(language, {}),
"api_endpoint": "https://api.aiapp.link/api/message/",
"message": f"{language.title()} 코드가 성공적으로 생성되었습니다 (CDN 소스, API Key {'주입됨' if BAAS_API_KEY else '미설정'})"
}
except Exception as e:
return {
"success": False,
"error": f"코드 생성에 실패했습니다: {str(e)}",
"error_code": "CODE_GENERATION_ERROR"
}