import logging import httpx from app.connectors.ows import ows_client logger = logging.getLogger(__name__) OWS_DMP_CLIENT_TIMEOUT = 10 class OwsDmpError(Exception): pass class StoreNotFoundError(Exception): pass class MetaAdReportingConnectionNotFoundError(Exception): pass class TikTokAdReportingConnectionNotFoundError(Exception): pass class GoogleAdReportingConnectionNotFoundError(Exception): pass def refresh_shopify_store(connector_id: str) -> None: path = "/shopify/stores/refresh" logger.info("Refreshing store", extra={"connector_id": connector_id}) try: response = ows_client.put( service_name="ows-dmp", path=path, json={ "fivetranConnectorId": connector_id, }, timeout=OWS_DMP_CLIENT_TIMEOUT, ) response.raise_for_status() except httpx.HTTPStatusError as exc: if exc.response.status_code == 404: raise StoreNotFoundError() from None raise OwsDmpError(str(exc)) from exc except Exception as exc: raise OwsDmpError(str(exc)) from exc def handle_transformation_success() -> None: path = "/shopify/handle-transformation-success" logger.info("Handling transformation success...") try: response = ows_client.post( service_name="ows-dmp", path=path, timeout=OWS_DMP_CLIENT_TIMEOUT, ) response.raise_for_status() except Exception as exc: raise OwsDmpError(str(exc)) from exc def refresh_meta_ad_reporting_connection(connector_id: str) -> None: path = "/meta/ad-reporting/connection/refresh" logger.info( "Refreshing meta ad reporting connection", extra={"connector_id": connector_id} ) try: response = ows_client.put( service_name="ows-dmp", path=path, json={ "fivetranConnectorId": connector_id, }, timeout=OWS_DMP_CLIENT_TIMEOUT, ) response.raise_for_status() except httpx.HTTPStatusError as exc: if exc.response.status_code == 404: raise MetaAdReportingConnectionNotFoundError() from None raise OwsDmpError(str(exc)) from exc except Exception as exc: raise OwsDmpError(str(exc)) from exc def refresh_tiktok_ad_reporting_connection(connector_id: str) -> None: path = "/tiktok/ad-reporting/connection/refresh" logger.info( "Refreshing tiktok ad reporting connection", extra={"connector_id": connector_id}, ) try: response = ows_client.put( service_name="ows-dmp", path=path, json={ "fivetranConnectorId": connector_id, }, timeout=OWS_DMP_CLIENT_TIMEOUT, ) response.raise_for_status() except httpx.HTTPStatusError as exc: if exc.response.status_code == 404: raise TikTokAdReportingConnectionNotFoundError() from None raise OwsDmpError(str(exc)) from exc except Exception as exc: raise OwsDmpError(str(exc)) from exc def refresh_google_ad_reporting_connection(connector_id: str) -> None: path = "/google/ad-reporting/connection/refresh" logger.info( "Refreshing google ad reporting connection", extra={"connector_id": connector_id}, ) try: response = ows_client.put( service_name="ows-dmp", path=path, json={ "fivetranConnectorId": connector_id, }, timeout=OWS_DMP_CLIENT_TIMEOUT, ) response.raise_for_status() except httpx.HTTPStatusError as exc: if exc.response.status_code == 404: raise GoogleAdReportingConnectionNotFoundError() from None raise OwsDmpError(str(exc)) from exc except Exception as exc: raise OwsDmpError(str(exc)) from exc