Add role-based filtering and imporve code
This commit is contained in:
@@ -14,6 +14,7 @@ from urllib.parse import urljoin
|
||||
|
||||
from shared.auth.jwt_handler import JWTHandler
|
||||
from shared.config.base import BaseServiceSettings
|
||||
from shared.clients.circuit_breaker import CircuitBreaker, CircuitBreakerOpenException
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
@@ -91,11 +92,19 @@ class BaseServiceClient(ABC):
|
||||
self.config = config
|
||||
self.gateway_url = config.GATEWAY_URL
|
||||
self.authenticator = ServiceAuthenticator(service_name, config)
|
||||
|
||||
|
||||
# HTTP client configuration
|
||||
self.timeout = config.HTTP_TIMEOUT
|
||||
self.retries = config.HTTP_RETRIES
|
||||
self.retry_delay = config.HTTP_RETRY_DELAY
|
||||
|
||||
# Circuit breaker for fault tolerance
|
||||
self.circuit_breaker = CircuitBreaker(
|
||||
service_name=f"{service_name}-client",
|
||||
failure_threshold=5,
|
||||
timeout=60,
|
||||
success_threshold=2
|
||||
)
|
||||
|
||||
@abstractmethod
|
||||
def get_service_base_path(self) -> str:
|
||||
@@ -113,8 +122,8 @@ class BaseServiceClient(ABC):
|
||||
timeout: Optional[Union[int, httpx.Timeout]] = None
|
||||
) -> Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]:
|
||||
"""
|
||||
Make an authenticated request to another service via gateway
|
||||
|
||||
Make an authenticated request to another service via gateway with circuit breaker protection.
|
||||
|
||||
Args:
|
||||
method: HTTP method (GET, POST, PUT, DELETE)
|
||||
endpoint: API endpoint (will be prefixed with service base path)
|
||||
@@ -123,10 +132,53 @@ class BaseServiceClient(ABC):
|
||||
params: Query parameters
|
||||
headers: Additional headers
|
||||
timeout: Request timeout override
|
||||
|
||||
|
||||
Returns:
|
||||
Response data or None if request failed
|
||||
"""
|
||||
try:
|
||||
# Wrap request in circuit breaker
|
||||
return await self.circuit_breaker.call(
|
||||
self._do_request,
|
||||
method,
|
||||
endpoint,
|
||||
tenant_id,
|
||||
data,
|
||||
params,
|
||||
headers,
|
||||
timeout
|
||||
)
|
||||
except CircuitBreakerOpenException as e:
|
||||
logger.error(
|
||||
"Circuit breaker open - request rejected",
|
||||
service=self.service_name,
|
||||
endpoint=endpoint,
|
||||
error=str(e)
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Unexpected error in request",
|
||||
service=self.service_name,
|
||||
endpoint=endpoint,
|
||||
error=str(e)
|
||||
)
|
||||
return None
|
||||
|
||||
async def _do_request(
|
||||
self,
|
||||
method: str,
|
||||
endpoint: str,
|
||||
tenant_id: Optional[str] = None,
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
params: Optional[Dict[str, Any]] = None,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
timeout: Optional[Union[int, httpx.Timeout]] = None
|
||||
) -> Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]:
|
||||
"""
|
||||
Internal method to execute HTTP request with retries.
|
||||
Called by _make_request through circuit breaker.
|
||||
"""
|
||||
try:
|
||||
# Get service token
|
||||
token = await self.authenticator.get_service_token()
|
||||
@@ -135,7 +187,11 @@ class BaseServiceClient(ABC):
|
||||
request_headers = self.authenticator.get_request_headers(tenant_id)
|
||||
request_headers["Authorization"] = f"Bearer {token}"
|
||||
request_headers["Content-Type"] = "application/json"
|
||||
|
||||
|
||||
# Propagate request ID for distributed tracing if provided
|
||||
if headers and "X-Request-ID" in headers:
|
||||
request_headers["X-Request-ID"] = headers["X-Request-ID"]
|
||||
|
||||
if headers:
|
||||
request_headers.update(headers)
|
||||
|
||||
|
||||
215
shared/clients/circuit_breaker.py
Normal file
215
shared/clients/circuit_breaker.py
Normal file
@@ -0,0 +1,215 @@
|
||||
"""
|
||||
Circuit Breaker implementation for inter-service communication
|
||||
Prevents cascading failures by failing fast when a service is unhealthy
|
||||
"""
|
||||
|
||||
import time
|
||||
import structlog
|
||||
from enum import Enum
|
||||
from typing import Callable, Any, Optional
|
||||
import asyncio
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
class CircuitState(Enum):
|
||||
"""Circuit breaker states"""
|
||||
CLOSED = "closed" # Normal operation, requests pass through
|
||||
OPEN = "open" # Service is failing, reject requests immediately
|
||||
HALF_OPEN = "half_open" # Testing if service has recovered
|
||||
|
||||
|
||||
class CircuitBreakerOpenException(Exception):
|
||||
"""Raised when circuit breaker is open and rejects a request"""
|
||||
pass
|
||||
|
||||
|
||||
class CircuitBreaker:
|
||||
"""
|
||||
Circuit breaker pattern implementation for preventing cascading failures.
|
||||
|
||||
States:
|
||||
- CLOSED: Normal operation, all requests pass through
|
||||
- OPEN: Service is failing, reject all requests immediately
|
||||
- HALF_OPEN: Testing recovery, allow one request through
|
||||
|
||||
Transitions:
|
||||
- CLOSED -> OPEN: After failure_threshold consecutive failures
|
||||
- OPEN -> HALF_OPEN: After timeout seconds have passed
|
||||
- HALF_OPEN -> CLOSED: If test request succeeds
|
||||
- HALF_OPEN -> OPEN: If test request fails
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
service_name: str,
|
||||
failure_threshold: int = 5,
|
||||
timeout: int = 60,
|
||||
success_threshold: int = 2
|
||||
):
|
||||
"""
|
||||
Initialize circuit breaker.
|
||||
|
||||
Args:
|
||||
service_name: Name of the service being protected
|
||||
failure_threshold: Number of consecutive failures before opening circuit
|
||||
timeout: Seconds to wait before attempting recovery (half-open state)
|
||||
success_threshold: Consecutive successes needed to close from half-open
|
||||
"""
|
||||
self.service_name = service_name
|
||||
self.failure_threshold = failure_threshold
|
||||
self.timeout = timeout
|
||||
self.success_threshold = success_threshold
|
||||
|
||||
self.state = CircuitState.CLOSED
|
||||
self.failure_count = 0
|
||||
self.success_count = 0
|
||||
self.last_failure_time: Optional[float] = None
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
logger.info(
|
||||
"Circuit breaker initialized",
|
||||
service=service_name,
|
||||
failure_threshold=failure_threshold,
|
||||
timeout=timeout
|
||||
)
|
||||
|
||||
async def call(self, func: Callable, *args, **kwargs) -> Any:
|
||||
"""
|
||||
Execute function with circuit breaker protection.
|
||||
|
||||
Args:
|
||||
func: Async function to execute
|
||||
*args, **kwargs: Arguments to pass to func
|
||||
|
||||
Returns:
|
||||
Result from func
|
||||
|
||||
Raises:
|
||||
CircuitBreakerOpenException: If circuit is open
|
||||
Exception: Any exception raised by func
|
||||
"""
|
||||
async with self._lock:
|
||||
# Check if circuit should transition to half-open
|
||||
if self.state == CircuitState.OPEN:
|
||||
if self._should_attempt_reset():
|
||||
logger.info(
|
||||
"Circuit breaker transitioning to half-open",
|
||||
service=self.service_name
|
||||
)
|
||||
self.state = CircuitState.HALF_OPEN
|
||||
self.success_count = 0
|
||||
else:
|
||||
# Circuit is open, reject request
|
||||
raise CircuitBreakerOpenException(
|
||||
f"Circuit breaker is OPEN for {self.service_name}. "
|
||||
f"Service will be retried in {self._time_until_retry():.0f} seconds."
|
||||
)
|
||||
|
||||
# Execute function
|
||||
try:
|
||||
result = await func(*args, **kwargs)
|
||||
await self._on_success()
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
await self._on_failure(e)
|
||||
raise
|
||||
|
||||
def _should_attempt_reset(self) -> bool:
|
||||
"""Check if enough time has passed to attempt recovery"""
|
||||
if self.last_failure_time is None:
|
||||
return True
|
||||
|
||||
return time.time() - self.last_failure_time >= self.timeout
|
||||
|
||||
def _time_until_retry(self) -> float:
|
||||
"""Calculate seconds until next retry attempt"""
|
||||
if self.last_failure_time is None:
|
||||
return 0.0
|
||||
|
||||
elapsed = time.time() - self.last_failure_time
|
||||
return max(0.0, self.timeout - elapsed)
|
||||
|
||||
async def _on_success(self):
|
||||
"""Handle successful request"""
|
||||
async with self._lock:
|
||||
self.failure_count = 0
|
||||
|
||||
if self.state == CircuitState.HALF_OPEN:
|
||||
self.success_count += 1
|
||||
logger.debug(
|
||||
"Circuit breaker success in half-open state",
|
||||
service=self.service_name,
|
||||
success_count=self.success_count,
|
||||
success_threshold=self.success_threshold
|
||||
)
|
||||
|
||||
if self.success_count >= self.success_threshold:
|
||||
logger.info(
|
||||
"Circuit breaker closing - service recovered",
|
||||
service=self.service_name
|
||||
)
|
||||
self.state = CircuitState.CLOSED
|
||||
self.success_count = 0
|
||||
|
||||
async def _on_failure(self, exception: Exception):
|
||||
"""Handle failed request"""
|
||||
async with self._lock:
|
||||
self.failure_count += 1
|
||||
self.last_failure_time = time.time()
|
||||
|
||||
if self.state == CircuitState.HALF_OPEN:
|
||||
logger.warning(
|
||||
"Circuit breaker opening - recovery attempt failed",
|
||||
service=self.service_name,
|
||||
error=str(exception)
|
||||
)
|
||||
self.state = CircuitState.OPEN
|
||||
self.success_count = 0
|
||||
|
||||
elif self.state == CircuitState.CLOSED:
|
||||
logger.warning(
|
||||
"Circuit breaker failure recorded",
|
||||
service=self.service_name,
|
||||
failure_count=self.failure_count,
|
||||
threshold=self.failure_threshold,
|
||||
error=str(exception)
|
||||
)
|
||||
|
||||
if self.failure_count >= self.failure_threshold:
|
||||
logger.error(
|
||||
"Circuit breaker opening - failure threshold reached",
|
||||
service=self.service_name,
|
||||
failure_count=self.failure_count
|
||||
)
|
||||
self.state = CircuitState.OPEN
|
||||
|
||||
def get_state(self) -> str:
|
||||
"""Get current circuit breaker state"""
|
||||
return self.state.value
|
||||
|
||||
def is_closed(self) -> bool:
|
||||
"""Check if circuit is closed (normal operation)"""
|
||||
return self.state == CircuitState.CLOSED
|
||||
|
||||
def is_open(self) -> bool:
|
||||
"""Check if circuit is open (failing fast)"""
|
||||
return self.state == CircuitState.OPEN
|
||||
|
||||
def is_half_open(self) -> bool:
|
||||
"""Check if circuit is half-open (testing recovery)"""
|
||||
return self.state == CircuitState.HALF_OPEN
|
||||
|
||||
async def reset(self):
|
||||
"""Manually reset circuit breaker to closed state"""
|
||||
async with self._lock:
|
||||
logger.info(
|
||||
"Circuit breaker manually reset",
|
||||
service=self.service_name,
|
||||
previous_state=self.state.value
|
||||
)
|
||||
self.state = CircuitState.CLOSED
|
||||
self.failure_count = 0
|
||||
self.success_count = 0
|
||||
self.last_failure_time = None
|
||||
205
shared/clients/nominatim_client.py
Normal file
205
shared/clients/nominatim_client.py
Normal file
@@ -0,0 +1,205 @@
|
||||
"""
|
||||
Nominatim Client for geocoding and address search
|
||||
"""
|
||||
|
||||
import structlog
|
||||
import httpx
|
||||
from typing import Optional, List, Dict, Any
|
||||
from shared.config.base import BaseServiceSettings
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
class NominatimClient:
|
||||
"""
|
||||
Client for Nominatim geocoding service.
|
||||
|
||||
Provides address search and geocoding capabilities for the bakery onboarding flow.
|
||||
"""
|
||||
|
||||
def __init__(self, config: BaseServiceSettings):
|
||||
self.config = config
|
||||
self.nominatim_url = getattr(
|
||||
config,
|
||||
"NOMINATIM_SERVICE_URL",
|
||||
"http://nominatim-service:8080"
|
||||
)
|
||||
self.timeout = 30
|
||||
|
||||
async def search_address(
|
||||
self,
|
||||
query: str,
|
||||
country_codes: str = "es",
|
||||
limit: int = 5,
|
||||
addressdetails: bool = True
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Search for addresses matching a query.
|
||||
|
||||
Args:
|
||||
query: Address search query (e.g., "Calle Mayor 1, Madrid")
|
||||
country_codes: Limit search to country codes (default: "es" for Spain)
|
||||
limit: Maximum number of results (default: 5)
|
||||
addressdetails: Include detailed address breakdown (default: True)
|
||||
|
||||
Returns:
|
||||
List of geocoded results with lat, lon, and address details
|
||||
|
||||
Example:
|
||||
results = await nominatim.search_address("Calle Mayor 1, Madrid")
|
||||
if results:
|
||||
lat = results[0]["lat"]
|
||||
lon = results[0]["lon"]
|
||||
display_name = results[0]["display_name"]
|
||||
"""
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
||||
response = await client.get(
|
||||
f"{self.nominatim_url}/search",
|
||||
params={
|
||||
"q": query,
|
||||
"format": "json",
|
||||
"countrycodes": country_codes,
|
||||
"addressdetails": 1 if addressdetails else 0,
|
||||
"limit": limit
|
||||
}
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
results = response.json()
|
||||
logger.info(
|
||||
"Address search completed",
|
||||
query=query,
|
||||
results_count=len(results)
|
||||
)
|
||||
return results
|
||||
else:
|
||||
logger.error(
|
||||
"Nominatim search failed",
|
||||
query=query,
|
||||
status_code=response.status_code,
|
||||
response=response.text
|
||||
)
|
||||
return []
|
||||
|
||||
except httpx.TimeoutException:
|
||||
logger.error("Nominatim search timeout", query=query)
|
||||
return []
|
||||
except Exception as e:
|
||||
logger.error("Nominatim search error", query=query, error=str(e))
|
||||
return []
|
||||
|
||||
async def geocode_address(
|
||||
self,
|
||||
street: str,
|
||||
city: str,
|
||||
postal_code: Optional[str] = None,
|
||||
country: str = "Spain"
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Geocode a structured address to coordinates.
|
||||
|
||||
Args:
|
||||
street: Street name and number
|
||||
city: City name
|
||||
postal_code: Optional postal code
|
||||
country: Country name (default: "Spain")
|
||||
|
||||
Returns:
|
||||
Dict with lat, lon, and display_name, or None if not found
|
||||
|
||||
Example:
|
||||
location = await nominatim.geocode_address(
|
||||
street="Calle Mayor 1",
|
||||
city="Madrid",
|
||||
postal_code="28013"
|
||||
)
|
||||
if location:
|
||||
lat, lon = location["lat"], location["lon"]
|
||||
"""
|
||||
# Build structured query
|
||||
query_parts = [street, city]
|
||||
if postal_code:
|
||||
query_parts.append(postal_code)
|
||||
query_parts.append(country)
|
||||
|
||||
query = ", ".join(query_parts)
|
||||
|
||||
results = await self.search_address(query, limit=1)
|
||||
if results:
|
||||
return results[0]
|
||||
return None
|
||||
|
||||
async def reverse_geocode(
|
||||
self,
|
||||
latitude: float,
|
||||
longitude: float
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Reverse geocode coordinates to an address.
|
||||
|
||||
Args:
|
||||
latitude: Latitude coordinate
|
||||
longitude: Longitude coordinate
|
||||
|
||||
Returns:
|
||||
Dict with address details, or None if not found
|
||||
|
||||
Example:
|
||||
address = await nominatim.reverse_geocode(40.4168, -3.7038)
|
||||
if address:
|
||||
city = address["address"]["city"]
|
||||
street = address["address"]["road"]
|
||||
"""
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
||||
response = await client.get(
|
||||
f"{self.nominatim_url}/reverse",
|
||||
params={
|
||||
"lat": latitude,
|
||||
"lon": longitude,
|
||||
"format": "json",
|
||||
"addressdetails": 1
|
||||
}
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
logger.info(
|
||||
"Reverse geocoding completed",
|
||||
lat=latitude,
|
||||
lon=longitude
|
||||
)
|
||||
return result
|
||||
else:
|
||||
logger.error(
|
||||
"Nominatim reverse geocoding failed",
|
||||
lat=latitude,
|
||||
lon=longitude,
|
||||
status_code=response.status_code
|
||||
)
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Reverse geocoding error",
|
||||
lat=latitude,
|
||||
lon=longitude,
|
||||
error=str(e)
|
||||
)
|
||||
return None
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
"""
|
||||
Check if Nominatim service is healthy.
|
||||
|
||||
Returns:
|
||||
True if service is responding, False otherwise
|
||||
"""
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=5) as client:
|
||||
response = await client.get(f"{self.nominatim_url}/status")
|
||||
return response.status_code == 200
|
||||
except Exception as e:
|
||||
logger.warning("Nominatim health check failed", error=str(e))
|
||||
return False
|
||||
Reference in New Issue
Block a user