|
| 1 | +# Copyright © 2025 Bentley Systems, Incorporated |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import datetime |
| 14 | +import json |
| 15 | +import tempfile |
| 16 | +import time |
| 17 | +import uuid |
| 18 | +import zipfile |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import requests |
| 22 | + |
| 23 | +from evo.aio import AioTransport |
| 24 | +from evo.common import APIConnector, Environment |
| 25 | +from evo.files import FileAPIClient |
| 26 | +from evo.oauth import ClientCredentialsAuthorizer, EvoScopes, OAuthConnector |
| 27 | + |
| 28 | +# Configuration |
| 29 | +CONFIG = { |
| 30 | + "mx": { |
| 31 | + "url": "https://app.mxdeposit.net/api/v3/collars/export/", |
| 32 | + "project_id": "<project_id>", |
| 33 | + "template_code": "<template_code>", |
| 34 | + "auth_token": "<api_key>", |
| 35 | + "client_id": "<client_id>", |
| 36 | + }, |
| 37 | + "evo": { |
| 38 | + "USER_AGENT": "MXDepositToEvoScript", |
| 39 | + "CLIENT_ID": "<client_id>", |
| 40 | + "CLIENT_SECRET": "<client_secret>", |
| 41 | + "service_host": "<hub_url>", |
| 42 | + "org_id": "<org_id>", |
| 43 | + "workspace_id": "<workspace_id>", |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +def export_collars(config): |
| 49 | + payload = json.dumps( |
| 50 | + { |
| 51 | + "project": config["project_id"], |
| 52 | + "template_code": config["template_code"], |
| 53 | + } |
| 54 | + ) |
| 55 | + headers = { |
| 56 | + "Content-Type": "application/json", |
| 57 | + "Authorization": config["auth_token"], |
| 58 | + "Client-ID": config["client_id"], |
| 59 | + } |
| 60 | + print("Request made at (UTC):", datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")) |
| 61 | + response = requests.post(config["url"], headers=headers, data=payload) |
| 62 | + if response.status_code != 200: |
| 63 | + print(f"Error: Received status code {response.status_code}") |
| 64 | + print(f"Response content: {response.text}") |
| 65 | + print(response.json()) |
| 66 | + try: |
| 67 | + data = response.json() |
| 68 | + operation_uid = data["jobs"][0]["parameters"]["body"]["operation_uid"] |
| 69 | + print("operation_uid:", operation_uid) |
| 70 | + return operation_uid |
| 71 | + except Exception as e: |
| 72 | + print("Could not extract operation_uid:", e) |
| 73 | + return None |
| 74 | + |
| 75 | + |
| 76 | +def poll_export_status(operation_uid, config, interval=30, timeout=8 * 60 * 60): |
| 77 | + headers = { |
| 78 | + "Content-Type": "application/json", |
| 79 | + "Authorization": config["auth_token"], |
| 80 | + "Client-ID": config["client_id"], |
| 81 | + } |
| 82 | + status_url = f"https://app.mxdeposit.net/export-status/{operation_uid}" |
| 83 | + start_time = time.time() |
| 84 | + while True: |
| 85 | + response = requests.get(status_url, headers=headers) |
| 86 | + try: |
| 87 | + data = response.json() |
| 88 | + print(f"Polling: {data}") |
| 89 | + if data.get("state") == "done": |
| 90 | + return data.get("url") |
| 91 | + except Exception as e: |
| 92 | + print("Error parsing response:", e) |
| 93 | + if time.time() - start_time > timeout: |
| 94 | + print("Polling timed out.") |
| 95 | + return None |
| 96 | + time.sleep(interval) |
| 97 | + |
| 98 | + |
| 99 | +def download_and_extract_zip(download_url, temp_dir): |
| 100 | + export_file = temp_dir / "export.zip" |
| 101 | + response = requests.get(download_url) |
| 102 | + export_file.write_bytes(response.content) |
| 103 | + with zipfile.ZipFile(export_file, "r") as zip_ref: |
| 104 | + zip_ref.extractall(temp_dir) |
| 105 | + print(f"Files extracted to: {temp_dir}") |
| 106 | + |
| 107 | + |
| 108 | +async def upload_csv_files(temp_dir, file_client, connector): |
| 109 | + success = True |
| 110 | + for file_path in temp_dir.glob("*.csv"): |
| 111 | + try: |
| 112 | + ctx = await file_client.prepare_upload_by_path(file_path.name) |
| 113 | + await ctx.upload_from_path(str(file_path), connector.transport) |
| 114 | + except Exception as e: |
| 115 | + print(f"Error uploading {file_path.name}: {e}") |
| 116 | + success = False |
| 117 | + if success: |
| 118 | + print("All CSV files uploaded successfully.") |
| 119 | + return success |
| 120 | + |
| 121 | + |
| 122 | +def main(): |
| 123 | + mx_cfg = CONFIG["mx"] |
| 124 | + evo_cfg = CONFIG["evo"] |
| 125 | + |
| 126 | + operation_uid = export_collars(mx_cfg) |
| 127 | + if not operation_uid: |
| 128 | + return |
| 129 | + |
| 130 | + download_url = poll_export_status(operation_uid, mx_cfg) |
| 131 | + if not download_url: |
| 132 | + return |
| 133 | + |
| 134 | + environment = Environment( |
| 135 | + hub_url=evo_cfg["service_host"], |
| 136 | + org_id=uuid.UUID(evo_cfg["org_id"]), |
| 137 | + workspace_id=uuid.UUID(evo_cfg["workspace_id"]), |
| 138 | + ) |
| 139 | + transport = AioTransport(user_agent=evo_cfg["USER_AGENT"]) |
| 140 | + authorizer = ClientCredentialsAuthorizer( |
| 141 | + oauth_connector=OAuthConnector( |
| 142 | + transport=transport, |
| 143 | + client_id=evo_cfg["CLIENT_ID"], |
| 144 | + client_secret=evo_cfg["CLIENT_SECRET"], |
| 145 | + ), |
| 146 | + scopes=EvoScopes.all_evo, |
| 147 | + ) |
| 148 | + connector = APIConnector(environment.hub_url, transport, authorizer) |
| 149 | + file_client = FileAPIClient(connector=connector, environment=environment) |
| 150 | + |
| 151 | + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 152 | + |
| 153 | + script_dir = Path(__file__).parent |
| 154 | + with tempfile.TemporaryDirectory(dir=script_dir) as temp_dir: |
| 155 | + temp_path = Path(temp_dir) |
| 156 | + download_and_extract_zip(download_url, temp_path) |
| 157 | + asyncio.run(upload_csv_files(temp_path, file_client, connector)) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main() |
0 commit comments