"""OWS Assets API client.""" from datetime import datetime from datetime import timezone import httpx from owsclient import OwsClient from src import config from src.config import logger client = OwsClient( config.ENVIRONMENT, config.APPLICATION_NAME, retries=config.OWS_RETRIES, timeout=httpx.Timeout(config.OWS_TIMEOUT) ) def post_asset_status(filename, status, description, message): """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 Exception(error_text) except Exception as e: logger.exception(str(e)) raise