| 1 | from typing import Any |
| 2 | from typing import Dict |
| 3 | from typing import Optional |
| 4 | |
| 5 | import requests |
| 6 | from loguru import logger |
| 7 | |
| 8 | from app.connectors.utils import get_connector_info_from_db |
| 9 | from app.db.db_session import get_db_session |
| 10 | |
| 11 | |
| 12 | async def verify_sublime_credentials(attributes: Dict[str, Any]) -> Dict[str, Any]: |
| 13 | """ |
| 14 | Verifies the connection to Sublime service. |
| 15 | |
| 16 | Returns: |
| 17 | dict: A dictionary containing 'connectionSuccessful' status and 'authToken' if the connection is successful. |
| 18 | """ |
| 19 | logger.info( |
| 20 | f"Verifying the Sublime connection to {attributes['connector_url']}", |
| 21 | ) |
| 22 | try: |
| 23 | headers = { |
| 24 | "Authorization": f"Bearer {attributes['connector_api_key']}", |
| 25 | "Content-Type": "application/json", |
| 26 | } |
| 27 | params = { |
| 28 | "limit": 1, |
| 29 | } |
| 30 | sublime = requests.get( |
| 31 | f"{attributes['connector_url']}/v0/rules", |
| 32 | headers=headers, |
| 33 | params=params, |
| 34 | verify=False, |
| 35 | ) |
| 36 | if sublime.status_code == 200: |
| 37 | logger.info( |
| 38 | f"Connection to {attributes['connector_url']} successful", |
| 39 | ) |
| 40 | return { |
| 41 | "connectionSuccessful": True, |
| 42 | "message": "Sublime connection successful", |
| 43 | } |
| 44 | else: |
| 45 | logger.error( |
| 46 | f"Connection to {attributes['connector_url']} failed with error: {sublime.text}", |
| 47 | ) |
| 48 | return { |
| 49 | "connectionSuccessful": False, |
| 50 | "message": f"Connection to {attributes['connector_url']} failed with error: {sublime.text}", |
| 51 | } |
| 52 | except Exception as e: |
| 53 | logger.error( |
| 54 | f"Connection to {attributes['connector_url']} failed with error: {e}", |
| 55 | ) |
| 56 | return { |
| 57 | "connectionSuccessful": False, |
| 58 | "message": f"Connection to {attributes['connector_url']} failed with error: {e}", |
| 59 | } |
| 60 | |
| 61 | |
| 62 | async def verify_sublime_connection(connector_name: str) -> str: |
| 63 | """ |
| 64 | Returns if connection to Sublime service is successful. |
| 65 | """ |
| 66 | logger.info("Getting Sublime authentication token") |
| 67 | async with get_db_session() as session: # This will correctly enter the context manager |
| 68 | attributes = await get_connector_info_from_db(connector_name, session) |
| 69 | if attributes is None: |
| 70 | logger.error("No Sublime connector found in the database") |
| 71 | return None |
| 72 | return await verify_sublime_credentials(attributes) |
| 73 | |
| 74 | |
| 75 | async def send_get_request( |
| 76 | endpoint: str, |
| 77 | params: Optional[Dict[str, Any]] = None, |
| 78 | connector_name: str = "Sublime", |
| 79 | ) -> Dict[str, Any]: |
| 80 | """ |
| 81 | Sends a GET request to the Sublime service. |
| 82 | |
| 83 | Args: |
| 84 | endpoint (str): The endpoint to send the GET request to. |
| 85 | params (Optional[Dict[str, Any]], optional): The parameters to send with the GET request. Defaults to None. |
| 86 | connector_name (str, optional): The name of the connector to use. Defaults to "Shuffle". |
| 87 | |
| 88 | Returns: |
| 89 | Dict[str, Any]: The response from the GET request. |
| 90 | """ |
| 91 | logger.info(f"Sending GET request to {endpoint}") |
| 92 | async with get_db_session() as session: # This will correctly enter the context manager |
| 93 | attributes = await get_connector_info_from_db(connector_name, session) |
| 94 | if attributes is None: |
| 95 | logger.error("No Sublime connector found in the database") |
| 96 | return None |
| 97 | try: |
| 98 | HEADERS = { |
| 99 | "Authorization": f"Bearer {attributes['connector_api_key']}", |
| 100 | "Content-Type": "application/json", |
| 101 | } |
| 102 | response = requests.get( |
| 103 | f"{attributes['connector_url']}{endpoint}", |
| 104 | headers=HEADERS, |
| 105 | params=params, |
| 106 | verify=False, |
| 107 | ) |
| 108 | return { |
| 109 | "data": response.json(), |
| 110 | "success": True, |
| 111 | "message": "Successfully retrieved data", |
| 112 | } |
| 113 | except Exception as e: |
| 114 | logger.error(f"Failed to send GET request to {endpoint} with error: {e}") |
| 115 | return { |
| 116 | "success": False, |
| 117 | "message": f"Failed to send GET request to {endpoint} with error: {e}", |
| 118 | } |