|
| 1 | +import json |
| 2 | +import uuid |
| 3 | +from enum import Enum, auto |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +class DataTypes(Enum): |
| 9 | + static_mesh = "static_mesh" |
| 10 | + collars = "wells/collars" |
| 11 | + cylinder = "wells/cylinders" |
| 12 | + volumes = "volumes" |
| 13 | + |
| 14 | + |
| 15 | +class LiquidEarthClient(): |
| 16 | + token: str |
| 17 | + user_id: str |
| 18 | + |
| 19 | + # Move to local settings |
| 20 | + host = "https://apim-liquidearth.azure-api.net/" |
| 21 | + |
| 22 | + @property |
| 23 | + def header(self): |
| 24 | + header_dict = { |
| 25 | + "Authorization": f"Bearer {self.token}" |
| 26 | + } |
| 27 | + return header_dict |
| 28 | + |
| 29 | + def login(self, username: str, password: str): |
| 30 | + end_point = "user/login_b2c" |
| 31 | + |
| 32 | + data = { |
| 33 | + "username": username, |
| 34 | + "password": password, |
| 35 | + "grant_type": "password", |
| 36 | + "client_id": "685e08c0-0aac-42f6-80a9-c57440cd2962", |
| 37 | + "scope": "openid 685e08c0-0aac-42f6-80a9-c57440cd2962 offline_access" |
| 38 | + } |
| 39 | + |
| 40 | + response = requests.post(self.host + end_point, data=data) |
| 41 | + if response.status_code == 400: |
| 42 | + raise Exception(f"Request failed: {response.text}") |
| 43 | + elif response.status_code == 200: |
| 44 | + _json = response.json() |
| 45 | + self.token = _json["access_token"] |
| 46 | + self.user_id = username |
| 47 | + |
| 48 | + def get_available_projects(self): |
| 49 | + end_point = "cosmos-le/v1/available_projects/" |
| 50 | + response = requests.get(self.host + end_point, headers=self.header) |
| 51 | + |
| 52 | + if response.status_code >= 400: |
| 53 | + raise Exception(f"Request failed: {response.text}") |
| 54 | + elif response.status_code >= 200: |
| 55 | + print(response.json()) |
| 56 | + return response.json() |
| 57 | + |
| 58 | + def add_new_project(self, project_name, extent, project_id=None): |
| 59 | + if project_id is None: |
| 60 | + project_id = str(uuid.uuid4()) |
| 61 | + |
| 62 | + available_projects: list = self.get_available_projects() |
| 63 | + |
| 64 | + for project in available_projects: |
| 65 | + if project["project_id"] == project_id: |
| 66 | + raise ValueError("This Project already exists") |
| 67 | + |
| 68 | + new_project = { |
| 69 | + "project_name": project_name, |
| 70 | + "project_id": project_id |
| 71 | + } |
| 72 | + available_projects.append(new_project) |
| 73 | + self._post_available_projects(available_projects) |
| 74 | + |
| 75 | + new_project_meta = { |
| 76 | + "remote_name": project_name, |
| 77 | + "id": project_id, |
| 78 | + "owner": self.user_id, |
| 79 | + "extent": extent, |
| 80 | + "static_data_address": [] |
| 81 | + } |
| 82 | + self._post_project_meta(new_project_meta) |
| 83 | + return project_id |
| 84 | + |
| 85 | + def add_data_to_project(self, project_id: str, data_name: str, data_type: DataTypes, |
| 86 | + header: json, body: bytearray): |
| 87 | + |
| 88 | + self._post_update_meta_data(project_id, data_name, data_type) |
| 89 | + self._put_file_in_project(project_id, data_name + ".le", data_type, body) |
| 90 | + self._put_file_in_project(project_id, data_name + ".json", data_type, json.dumps(header)) |
| 91 | + |
| 92 | + def _put_file_in_project(self, project_id: str, data_name, data_type: DataTypes, file): |
| 93 | + blob_path = data_type.value + "/" + data_name |
| 94 | + |
| 95 | + end_point = f"container/{project_id}/{blob_path}" |
| 96 | + response = requests.put(self.host + end_point, data=file, headers=self.header) |
| 97 | + |
| 98 | + if response.status_code >= 400: |
| 99 | + raise Exception(f"Request failed: {response.text}") |
| 100 | + elif response.status_code >= 200: |
| 101 | + print(response.text) |
| 102 | + |
| 103 | + def _post_update_meta_data(self, project_id: str, data_name: str, data_type: DataTypes): |
| 104 | + |
| 105 | + query_param = f"?project_id={project_id}&data_id={data_name}&data_type={data_type.value}" |
| 106 | + end_point = "http://localhost:7071/api/update_project_meta" + query_param |
| 107 | + |
| 108 | + response = requests.post(end_point) |
| 109 | + |
| 110 | + if response.status_code >= 400: |
| 111 | + raise Exception(f"Request failed: {response.text}") |
| 112 | + elif response.status_code >= 200: |
| 113 | + print(response.text) |
| 114 | + |
| 115 | + def _post_available_projects(self, available_projects: list): |
| 116 | + end_point = "cosmos-le/v1/available_projects/" |
| 117 | + response = requests.post(self.host + end_point, json=available_projects, |
| 118 | + headers=self.header) |
| 119 | + |
| 120 | + if response.status_code >= 400: |
| 121 | + raise Exception(f"Request failed: {response.text}") |
| 122 | + elif response.status_code >= 200: |
| 123 | + print("Available Projects Posted") |
| 124 | + |
| 125 | + def _post_project_meta(self, meta_data: dict): |
| 126 | + project_id = meta_data["id"] |
| 127 | + |
| 128 | + end_point = f"cosmos-le/v1/project_meta/?project_id={project_id}" |
| 129 | + response = requests.post(self.host + end_point, json=meta_data, headers=self.header) |
| 130 | + |
| 131 | + if response.status_code >= 400: |
| 132 | + raise Exception(f"Request failed: {response.text}") |
| 133 | + elif response.status_code >= 200: |
| 134 | + print("Project Metadata Posted") |
0 commit comments