Make API endpoints compatible with Vultr API v2 format
- Change auth from X-API-Key header to Authorization: Bearer format - Add /v2 prefix to all endpoints to match Vultr API URL structure - Fix router paths (dns, firewall) to avoid duplicate path segments - Split VPC 2.0 into separate router at /v2/vpc2 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,20 +1,33 @@
|
||||
"""Dependencies for FastAPI"""
|
||||
import os
|
||||
from fastapi import HTTPException, Security
|
||||
from fastapi.security import APIKeyHeader
|
||||
from fastapi import HTTPException, Header
|
||||
from typing import Optional
|
||||
|
||||
from vultr_api import VultrClient
|
||||
|
||||
API_KEY_HEADER = APIKeyHeader(name="X-API-Key", auto_error=False)
|
||||
VULTR_API_KEY = os.environ.get("VULTR_API_KEY")
|
||||
|
||||
|
||||
def get_client(api_key: str = Security(API_KEY_HEADER)) -> VultrClient:
|
||||
"""Get VultrClient instance with API key from header or environment"""
|
||||
key = api_key or VULTR_API_KEY
|
||||
def get_client(authorization: Optional[str] = Header(None)) -> VultrClient:
|
||||
"""
|
||||
Get VultrClient instance with API key from Authorization header or environment.
|
||||
|
||||
Supports Vultr API compatible format:
|
||||
Authorization: Bearer YOUR_API_KEY
|
||||
"""
|
||||
key = None
|
||||
|
||||
# Parse "Authorization: Bearer API_KEY" header
|
||||
if authorization and authorization.startswith("Bearer "):
|
||||
key = authorization[7:] # Remove "Bearer " prefix
|
||||
|
||||
# Fallback to environment variable
|
||||
if not key:
|
||||
key = VULTR_API_KEY
|
||||
|
||||
if not key:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="API key required. Set X-API-Key header or VULTR_API_KEY env var"
|
||||
detail="API key required. Use 'Authorization: Bearer YOUR_API_KEY' header or set VULTR_API_KEY env var"
|
||||
)
|
||||
return VultrClient(api_key=key)
|
||||
|
||||
Reference in New Issue
Block a user