|
| 1 | +import requests |
| 2 | +from pydantic import HttpUrl |
| 3 | + |
| 4 | +from conSys.con_sys_api import ConnectedSystemsRequestBuilder |
| 5 | +from conSys.constants import APITerms |
| 6 | + |
| 7 | + |
| 8 | +def list_all_commands(server_addr: HttpUrl, api_root: str = APITerms.API.value): |
| 9 | + """ |
| 10 | + Lists all commands |
| 11 | + :return: |
| 12 | + """ |
| 13 | + builder = ConnectedSystemsRequestBuilder() |
| 14 | + api_request = (builder.with_server_url(server_addr) |
| 15 | + .with_api_root(api_root) |
| 16 | + .for_resource_type(APITerms.COMMANDS.value) |
| 17 | + .build_url_from_base() |
| 18 | + .build()) |
| 19 | + resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers) |
| 20 | + return resp.json() |
| 21 | + |
| 22 | + |
| 23 | +def list_commands_of_control_channel(server_addr: HttpUrl, control_channel_id: str, api_root: str = APITerms.API.value): |
| 24 | + """ |
| 25 | + Lists all commands of a control channel |
| 26 | + :return: |
| 27 | + """ |
| 28 | + builder = ConnectedSystemsRequestBuilder() |
| 29 | + api_request = (builder.with_server_url(server_addr) |
| 30 | + .with_api_root(api_root) |
| 31 | + .for_resource_type(APITerms.CONTROL_STREAMS.value) |
| 32 | + .with_resource_id(control_channel_id) |
| 33 | + .for_resource_type(APITerms.COMMANDS.value) |
| 34 | + .build_url_from_base() |
| 35 | + .build()) |
| 36 | + resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers) |
| 37 | + return resp.json() |
| 38 | + |
| 39 | + |
| 40 | +def send_commands_to_specific_control_stream(server_addr: HttpUrl, control_stream_id: str, request_body: dict, |
| 41 | + api_root: str = APITerms.API.value): |
| 42 | + """ |
| 43 | + Sends a command to a control stream by its id |
| 44 | + :return: |
| 45 | + """ |
| 46 | + builder = ConnectedSystemsRequestBuilder() |
| 47 | + api_request = (builder.with_server_url(server_addr) |
| 48 | + .with_api_root(api_root) |
| 49 | + .for_resource_type(APITerms.CONTROL_STREAMS.value) |
| 50 | + .with_resource_id(control_stream_id) |
| 51 | + .for_resource_type(APITerms.COMMANDS.value) |
| 52 | + .with_request_body(request_body) |
| 53 | + .build_url_from_base() |
| 54 | + .build()) |
| 55 | + resp = requests.post(api_request.url, params=api_request.body, headers=api_request.headers) |
| 56 | + return resp.json() |
| 57 | + |
| 58 | + |
| 59 | +def retrieve_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value): |
| 60 | + """ |
| 61 | + Retrieves a command by its id |
| 62 | + :return: |
| 63 | + """ |
| 64 | + builder = ConnectedSystemsRequestBuilder() |
| 65 | + api_request = (builder.with_server_url(server_addr) |
| 66 | + .with_api_root(api_root) |
| 67 | + .for_resource_type(APITerms.COMMANDS.value) |
| 68 | + .with_resource_id(command_id) |
| 69 | + .build_url_from_base() |
| 70 | + .build()) |
| 71 | + resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers) |
| 72 | + return resp.json() |
| 73 | + |
| 74 | + |
| 75 | +def update_command_description(server_addr: HttpUrl, command_id: str, request_body: dict, |
| 76 | + api_root: str = APITerms.API.value): |
| 77 | + """ |
| 78 | + Updates a command's description by its id |
| 79 | + :return: |
| 80 | + """ |
| 81 | + builder = ConnectedSystemsRequestBuilder() |
| 82 | + api_request = (builder.with_server_url(server_addr) |
| 83 | + .with_api_root(api_root) |
| 84 | + .for_resource_type(APITerms.COMMANDS.value) |
| 85 | + .with_resource_id(command_id) |
| 86 | + .with_request_body(request_body) |
| 87 | + .build_url_from_base() |
| 88 | + .build()) |
| 89 | + resp = requests.put(api_request.url, params=api_request.body, headers=api_request.headers) |
| 90 | + return resp.json() |
| 91 | + |
| 92 | + |
| 93 | +def delete_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value): |
| 94 | + """ |
| 95 | + Deletes a command by its id |
| 96 | + :return: |
| 97 | + """ |
| 98 | + builder = ConnectedSystemsRequestBuilder() |
| 99 | + api_request = (builder.with_server_url(server_addr) |
| 100 | + .with_api_root(api_root) |
| 101 | + .for_resource_type(APITerms.COMMANDS.value) |
| 102 | + .with_resource_id(command_id) |
| 103 | + .build_url_from_base() |
| 104 | + .build()) |
| 105 | + resp = requests.delete(api_request.url, params=api_request.body, headers=api_request.headers) |
| 106 | + return resp.json() |
| 107 | + |
| 108 | + |
| 109 | +def list_command_status_reports(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value): |
| 110 | + """ |
| 111 | + Lists all status reports of a command by its id |
| 112 | + :return: |
| 113 | + """ |
| 114 | + builder = ConnectedSystemsRequestBuilder() |
| 115 | + api_request = (builder.with_server_url(server_addr) |
| 116 | + .with_api_root(api_root) |
| 117 | + .for_resource_type(APITerms.COMMANDS.value) |
| 118 | + .with_resource_id(command_id) |
| 119 | + .for_resource_type(APITerms.STATUS.value) |
| 120 | + .build_url_from_base() |
| 121 | + .build()) |
| 122 | + resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers) |
| 123 | + return resp.json() |
| 124 | + |
| 125 | + |
| 126 | +def add_command_status_reports(server_addr: HttpUrl, command_id: str, request_body: dict, |
| 127 | + api_root: str = APITerms.API.value): |
| 128 | + """ |
| 129 | + Adds a status report to a command by its id |
| 130 | + :return: |
| 131 | + """ |
| 132 | + builder = ConnectedSystemsRequestBuilder() |
| 133 | + api_request = (builder.with_server_url(server_addr) |
| 134 | + .with_api_root(api_root) |
| 135 | + .for_resource_type(APITerms.COMMANDS.value) |
| 136 | + .with_resource_id(command_id) |
| 137 | + .for_resource_type(APITerms.STATUS.value) |
| 138 | + .with_request_body(request_body) |
| 139 | + .build_url_from_base() |
| 140 | + .build()) |
| 141 | + resp = requests.post(api_request.url, params=api_request.body, headers=api_request.headers) |
| 142 | + return resp.json() |
| 143 | + |
| 144 | + |
| 145 | +def retrieve_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str, |
| 146 | + api_root: str = APITerms.API.value): |
| 147 | + """ |
| 148 | + Retrieves a status report of a command by its id and status report id |
| 149 | + :return: |
| 150 | + """ |
| 151 | + builder = ConnectedSystemsRequestBuilder() |
| 152 | + api_request = (builder.with_server_url(server_addr) |
| 153 | + .with_api_root(api_root) |
| 154 | + .for_resource_type(APITerms.COMMANDS.value) |
| 155 | + .with_resource_id(command_id) |
| 156 | + .for_resource_type(APITerms.STATUS.value) |
| 157 | + .with_secondary_resource_id(status_report_id) |
| 158 | + .build_url_from_base() |
| 159 | + .build()) |
| 160 | + resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers) |
| 161 | + return resp.json() |
| 162 | + |
| 163 | + |
| 164 | +def update_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str, |
| 165 | + request_body: dict, api_root: str = APITerms.API.value): |
| 166 | + """ |
| 167 | + Updates a status report of a command by its id and status report id |
| 168 | + :return: |
| 169 | + """ |
| 170 | + builder = ConnectedSystemsRequestBuilder() |
| 171 | + api_request = (builder.with_server_url(server_addr) |
| 172 | + .with_api_root(api_root) |
| 173 | + .for_resource_type(APITerms.COMMANDS.value) |
| 174 | + .with_resource_id(command_id) |
| 175 | + .for_resource_type(APITerms.STATUS.value) |
| 176 | + .with_secondary_resource_id(status_report_id) |
| 177 | + .with_request_body(request_body) |
| 178 | + .build_url_from_base() |
| 179 | + .build()) |
| 180 | + resp = requests.put(api_request.url, params=api_request.body, headers=api_request.headers) |
| 181 | + return resp.json() |
| 182 | + |
| 183 | + |
| 184 | +def delete_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str, |
| 185 | + api_root: str = APITerms.API.value): |
| 186 | + """ |
| 187 | + Deletes a status report of a command by its id and status report id |
| 188 | + :return: |
| 189 | + """ |
| 190 | + builder = ConnectedSystemsRequestBuilder() |
| 191 | + api_request = (builder.with_server_url(server_addr) |
| 192 | + .with_api_root(api_root) |
| 193 | + .for_resource_type(APITerms.COMMANDS.value) |
| 194 | + .with_resource_id(command_id) |
| 195 | + .for_resource_type(APITerms.STATUS.value) |
| 196 | + .with_secondary_resource_id(status_report_id) |
| 197 | + .build_url_from_base() |
| 198 | + .build()) |
| 199 | + resp = requests.delete(api_request.url, params=api_request.body, headers=api_request.headers) |
| 200 | + return resp.json() |
0 commit comments