"""OWS Assets API client.""" from datetime import datetime, timezone from typing import Any import httpx from owsclient import OwsClient from src import config, lambda_exceptions from src.config import logger client = OwsClient( config.ENVIRONMENT, config.APPLICATION_NAME, retries=config.OWS_RETRIES, timeout=httpx.Timeout(config.OWS_TIMEOUT_SECONDS), ) def post_asset_status(filename: str | None, status: str, description: str, message: dict[str, Any]) -> None: """Call ows-asset post asset status handler. Args: filename (str): Unique filename with extension. status (str): Status. description (str): Description. message (dict): Status message. """ try: data = { "filename": filename, "status": status, "description": description, "message": message, "timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ"), } response = client.post( service_name=config.OWS_ASSETS_SERVICE_NAME, path=config.POST_ASSET_STATUS_PATH, json=data, ) if response.status_code == 404: # This happens when an asset has been replaced by a new one while it is still being processed. # The replaced asset has been marked as deleted, so we get a 404 when trying to update status for it. pass elif response.status_code != 200: error_text = "Failed to post to ows-assets from {source}. Status: {status}; text: {text}".format( source=config.APPLICATION_NAME, status=response.status_code, text=response.text, ) raise lambda_exceptions.OwsAssetsError(error_text) except Exception as error: logger.exception(str(error)) raise