|
| 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_observations(server_addr: HttpUrl, api_root: str = APITerms.API.value): |
| 9 | + """ |
| 10 | + Lists all observations |
| 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.OBSERVATIONS.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_observations_from_datastream(server_addr: HttpUrl, datastream_id: str, api_root: str = APITerms.API.value): |
| 24 | + """ |
| 25 | + Lists all observations of a datastream |
| 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.DATASTREAMS.value) |
| 32 | + .with_resource_id(datastream_id) |
| 33 | + .for_resource_type(APITerms.OBSERVATIONS.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 add_observations_to_datastream(server_addr: HttpUrl, datastream_id: str, request_body: dict, |
| 41 | + api_root: str = APITerms.API.value): |
| 42 | + """ |
| 43 | + Adds an observation to a datastream 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.DATASTREAMS.value) |
| 50 | + .with_resource_id(datastream_id) |
| 51 | + .for_resource_type(APITerms.OBSERVATIONS.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_observation_by_id(server_addr: HttpUrl, observation_id: str, api_root: str = APITerms.API.value): |
| 60 | + """ |
| 61 | + Retrieves an observation 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.OBSERVATIONS.value) |
| 68 | + .with_resource_id(observation_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_observation_by_id(server_addr: HttpUrl, observation_id: str, request_body: dict, |
| 76 | + api_root: str = APITerms.API.value): |
| 77 | + """ |
| 78 | + Updates an observation 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.OBSERVATIONS.value) |
| 85 | + .with_resource_id(observation_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_observation_by_id(server_addr: HttpUrl, observation_id: str, api_root: str = APITerms.API.value): |
| 94 | + """ |
| 95 | + Deletes an observation 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.OBSERVATIONS.value) |
| 102 | + .with_resource_id(observation_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() |
0 commit comments