|
| 1 | +import os |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from datacontract.model.run import Run |
| 7 | + |
| 8 | +# used to retrieve the HTML location of the published data contract or test results |
| 9 | +RESPONSE_HEADER_LOCATION_HTML = "location-html" |
| 10 | + |
| 11 | + |
| 12 | +def publish_test_results_to_entropy_data(run: Run, publish_url: str, ssl_verification: bool): |
| 13 | + try: |
| 14 | + host = publish_url |
| 15 | + if publish_url is None: |
| 16 | + # this url supports Data Mesh Manager and Data Contract Manager |
| 17 | + host = _get_host() |
| 18 | + url = "%s/api/test-results" % host |
| 19 | + else: |
| 20 | + url = publish_url |
| 21 | + |
| 22 | + api_key = _get_api_key() |
| 23 | + |
| 24 | + if run.dataContractId is None: |
| 25 | + raise Exception("Cannot publish run results for unknown data contract ID") |
| 26 | + |
| 27 | + headers = {"Content-Type": "application/json", "x-api-key": api_key} |
| 28 | + request_body = run.model_dump_json() |
| 29 | + # print("Request Body:", request_body) |
| 30 | + response = requests.post( |
| 31 | + url, |
| 32 | + data=request_body, |
| 33 | + headers=headers, |
| 34 | + verify=ssl_verification, |
| 35 | + ) |
| 36 | + # print("Status Code:", response.status_code) |
| 37 | + # print("Response Body:", response.text) |
| 38 | + if response.status_code != 200: |
| 39 | + display_host = _extract_hostname(host) |
| 40 | + run.log_error(f"Error publishing test results to {display_host}: {response.text}") |
| 41 | + return |
| 42 | + run.log_info("Published test results successfully") |
| 43 | + |
| 44 | + location_html = response.headers.get(RESPONSE_HEADER_LOCATION_HTML) |
| 45 | + if location_html is not None and len(location_html) > 0: |
| 46 | + print(f"🚀 Open {location_html}") |
| 47 | + |
| 48 | + except Exception as e: |
| 49 | + run.log_error(f"Failed publishing test results. Error: {str(e)}") |
| 50 | + |
| 51 | + |
| 52 | +def publish_data_contract_to_entropy_data(data_contract_dict: dict, ssl_verification: bool): |
| 53 | + try: |
| 54 | + api_key = _get_api_key() |
| 55 | + host = _get_host() |
| 56 | + headers = {"Content-Type": "application/json", "x-api-key": api_key} |
| 57 | + id = data_contract_dict["id"] |
| 58 | + url = f"{host}/api/datacontracts/{id}" |
| 59 | + response = requests.put( |
| 60 | + url=url, |
| 61 | + json=data_contract_dict, |
| 62 | + headers=headers, |
| 63 | + verify=ssl_verification, |
| 64 | + ) |
| 65 | + if response.status_code != 200: |
| 66 | + display_host = _extract_hostname(host) |
| 67 | + print(f"Error publishing data contract to {display_host}: {response.text}") |
| 68 | + exit(1) |
| 69 | + |
| 70 | + print("✅ Published data contract successfully") |
| 71 | + |
| 72 | + location_html = response.headers.get(RESPONSE_HEADER_LOCATION_HTML) |
| 73 | + if location_html is not None and len(location_html) > 0: |
| 74 | + print(f"🚀 Open {location_html}") |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + print(f"Failed publishing data contract. Error: {str(e)}") |
| 78 | + |
| 79 | + |
| 80 | +def _get_api_key() -> str: |
| 81 | + """ |
| 82 | + Get API key from environment variables with fallback priority: |
| 83 | + 1. ENTROPY_DATA_API_KEY |
| 84 | + 2. DATAMESH_MANAGER_API_KEY |
| 85 | + 3. DATACONTRACT_MANAGER_API_KEY |
| 86 | + """ |
| 87 | + api_key = os.getenv("ENTROPY_DATA_API_KEY") |
| 88 | + if api_key is None: |
| 89 | + api_key = os.getenv("DATAMESH_MANAGER_API_KEY") |
| 90 | + if api_key is None: |
| 91 | + api_key = os.getenv("DATACONTRACT_MANAGER_API_KEY") |
| 92 | + if api_key is None: |
| 93 | + raise Exception( |
| 94 | + "Cannot publish, as neither ENTROPY_DATA_API_KEY, DATAMESH_MANAGER_API_KEY, nor DATACONTRACT_MANAGER_API_KEY is set" |
| 95 | + ) |
| 96 | + return api_key |
| 97 | + |
| 98 | + |
| 99 | +def _get_host() -> str: |
| 100 | + """ |
| 101 | + Get host from environment variables with fallback priority: |
| 102 | + 1. ENTROPY_DATA_HOST |
| 103 | + 2. DATAMESH_MANAGER_HOST |
| 104 | + 3. DATACONTRACT_MANAGER_HOST |
| 105 | + 4. Default: https://api.entropy-data.com |
| 106 | + """ |
| 107 | + host = os.getenv("ENTROPY_DATA_HOST") |
| 108 | + if host is None: |
| 109 | + host = os.getenv("DATAMESH_MANAGER_HOST") |
| 110 | + if host is None: |
| 111 | + host = os.getenv("DATACONTRACT_MANAGER_HOST") |
| 112 | + if host is None: |
| 113 | + host = "https://api.entropy-data.com" |
| 114 | + return host |
| 115 | + |
| 116 | + |
| 117 | +def _extract_hostname(url: str) -> str: |
| 118 | + """ |
| 119 | + Extract the hostname (including subdomains and top-level domain) from a URL. |
| 120 | +
|
| 121 | + Examples: |
| 122 | + - https://app.entropy-data.com/path -> app.entropy-data.com |
| 123 | + - http://api.example.com:8080/api -> api.example.com |
| 124 | + """ |
| 125 | + parsed = urlparse(url) |
| 126 | + return parsed.netloc.split(":")[0] if parsed.netloc else url |
0 commit comments