- 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>
34 lines
968 B
Python
34 lines
968 B
Python
"""Dependencies for FastAPI"""
|
|
import os
|
|
from fastapi import HTTPException, Header
|
|
from typing import Optional
|
|
|
|
from vultr_api import VultrClient
|
|
|
|
VULTR_API_KEY = os.environ.get("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. Use 'Authorization: Bearer YOUR_API_KEY' header or set VULTR_API_KEY env var"
|
|
)
|
|
return VultrClient(api_key=key)
|