From fa465c888237a58462de7c758ddf5bde26dde03f Mon Sep 17 00:00:00 2001 From: GustaafL Date: Wed, 5 Jul 2023 14:47:51 +0200 Subject: [PATCH 1/4] feat(sensors): adds fetch_one sensor endpoint to API Signed-off-by: GustaafL --- flexmeasures/api/v3_0/sensors.py | 37 ++++++++++++++++++ .../api/v3_0/tests/test_sensors_api.py | 39 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 flexmeasures/api/v3_0/tests/test_sensors_api.py diff --git a/flexmeasures/api/v3_0/sensors.py b/flexmeasures/api/v3_0/sensors.py index 9f61a67ed..ff110203e 100644 --- a/flexmeasures/api/v3_0/sensors.py +++ b/flexmeasures/api/v3_0/sensors.py @@ -46,6 +46,7 @@ get_sensor_schema = GetSensorDataSchema() post_sensor_schema = PostSensorDataSchema() sensors_schema = SensorSchema(many=True) +sensor_schema = SensorSchema() class SensorAPI(FlaskView): @@ -494,3 +495,39 @@ def get_schedule(self, sensor: Sensor, job_id: str, duration: timedelta, **kwarg d, s = request_processed() return dict(**response, **d), s + + @route("/", methods=["GET"]) + @use_kwargs({"sensor": SensorIdField(data_key="id")}, location="path") + @permission_required_for_context("read", arg_name="sensor") + @as_json + def fetch_one(self, id, sensor): + """Fetch a given sensor. + + .. :quickref: Sensor; Get a sensor + + This endpoint gets a sensor. + + **Example response** + + .. sourcecode:: json + + { + "name": "some gas sensor", + "unit": "m³/h", + "entity_address": "ea1.2023-08.localhost:fm1.1", + "event_resolution": 10, + "generic_asset_id": 4, + "timezone": "UTC", + } + + :reqheader Authorization: The authentication token + :reqheader Content-Type: application/json + :resheader Content-Type: application/json + :status 200: PROCESSED + :status 400: INVALID_REQUEST, REQUIRED_INFO_MISSING, UNEXPECTED_PARAMS + :status 401: UNAUTHORIZED + :status 403: INVALID_SENDER + :status 422: UNPROCESSABLE_ENTITY + """ + sensor = Sensor.query.filter(Sensor.id == 1).one_or_none() + return sensor_schema.dump(sensor), 200 diff --git a/flexmeasures/api/v3_0/tests/test_sensors_api.py b/flexmeasures/api/v3_0/tests/test_sensors_api.py new file mode 100644 index 000000000..68c836542 --- /dev/null +++ b/flexmeasures/api/v3_0/tests/test_sensors_api.py @@ -0,0 +1,39 @@ +from __future__ import annotations + + +from flask import url_for + + +from flexmeasures import Sensor +from flexmeasures.api.tests.utils import get_auth_token + + +def test_fetch_one_sensor( + client, + setup_api_test_data: dict[str, Sensor], +): + sensor_id = 1 + assert_response = { + "name": "some gas sensor", + "unit": "m³/h", + "entity_address": "ea1.2023-08.localhost:fm1.1", + "event_resolution": 10, + "generic_asset_id": 4, + "timezone": "UTC", + "status": 200, + } + headers = make_headers_for("test_supplier_user_4@seita.nl", client) + response = client.get( + url_for("SensorAPI:fetch_one", id=sensor_id), + headers=headers, + ) + print("Server responded with:\n%s" % response.json) + assert response.status_code == 200 + assert response.json == assert_response + + +def make_headers_for(user_email: str | None, client) -> dict: + headers = {"content-type": "application/json"} + if user_email: + headers["Authorization"] = get_auth_token(client, user_email, "testtest") + return headers From cf83385e6aba16b61dcc563253b349c2a27c732c Mon Sep 17 00:00:00 2001 From: GustaafL Date: Wed, 19 Jul 2023 10:30:10 +0200 Subject: [PATCH 2/4] refactor: review suggestions, changed test asserts Signed-off-by: GustaafL --- flexmeasures/api/v3_0/sensors.py | 1 - flexmeasures/api/v3_0/tests/test_sensors_api.py | 14 ++++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/flexmeasures/api/v3_0/sensors.py b/flexmeasures/api/v3_0/sensors.py index ff110203e..be2164fb8 100644 --- a/flexmeasures/api/v3_0/sensors.py +++ b/flexmeasures/api/v3_0/sensors.py @@ -529,5 +529,4 @@ def fetch_one(self, id, sensor): :status 403: INVALID_SENDER :status 422: UNPROCESSABLE_ENTITY """ - sensor = Sensor.query.filter(Sensor.id == 1).one_or_none() return sensor_schema.dump(sensor), 200 diff --git a/flexmeasures/api/v3_0/tests/test_sensors_api.py b/flexmeasures/api/v3_0/tests/test_sensors_api.py index 68c836542..b57fcfd83 100644 --- a/flexmeasures/api/v3_0/tests/test_sensors_api.py +++ b/flexmeasures/api/v3_0/tests/test_sensors_api.py @@ -13,15 +13,6 @@ def test_fetch_one_sensor( setup_api_test_data: dict[str, Sensor], ): sensor_id = 1 - assert_response = { - "name": "some gas sensor", - "unit": "m³/h", - "entity_address": "ea1.2023-08.localhost:fm1.1", - "event_resolution": 10, - "generic_asset_id": 4, - "timezone": "UTC", - "status": 200, - } headers = make_headers_for("test_supplier_user_4@seita.nl", client) response = client.get( url_for("SensorAPI:fetch_one", id=sensor_id), @@ -29,7 +20,10 @@ def test_fetch_one_sensor( ) print("Server responded with:\n%s" % response.json) assert response.status_code == 200 - assert response.json == assert_response + assert response.json["name"] == "some gas sensor" + assert response.json["unit"] == "m³/h" + assert response.json["generic_asset_id"] == 4 + assert response.json["timezone"] == "UTC" def make_headers_for(user_email: str | None, client) -> dict: From 8ef791af51281c9211edd9b972dfb44190d9b485 Mon Sep 17 00:00:00 2001 From: GustaafL Date: Thu, 20 Jul 2023 14:33:52 +0200 Subject: [PATCH 3/4] docs(sensor): API changelog and FM changelog update Signed-off-by: GustaafL --- documentation/api/change_log.rst | 1 + documentation/changelog.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/documentation/api/change_log.rst b/documentation/api/change_log.rst index ad1dd350f..29e474348 100644 --- a/documentation/api/change_log.rst +++ b/documentation/api/change_log.rst @@ -10,6 +10,7 @@ v3.0-10 | 2023-06-12 - Introduced the ``storage-efficiency`` field to the ``flex-model``field for `/sensors//schedules/trigger` (POST). - Introduced the ``database_redis`` optional field to the response of the endpoint `/health/ready` (GET). +- Added REST endpoint for fetching one sensor: `/sensors/` (GET) v3.0-9 | 2023-04-26 """"""""""""""""""" diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 167a8fb8c..db4a8c76b 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -15,6 +15,7 @@ New features * Make it a lot easier to read off the color legend on the asset page, especially when showing many sensors, as they will now be ordered from top to bottom in the same order as they appear in the chart (as defined in the ``sensors_to_show`` attribute), rather than alphabetically [see `PR #742 `_] * Having percentages within the [0, 100] domain is such a common use case that we now always include it in sensor charts with % units, making it easier to read off individual charts and also to compare across charts [see `PR #739 `_] * DataSource table now allows storing arbitrary attributes as a JSON (without content validation), similar to the Sensor and GenericAsset tables [see `PR #750 `_] +* Added API endpoint `/sensor/` for fetching a single sensor. [see `PR #433 `_] Bugfixes ----------- From 9ea8e4c68c2a219440cfa3201efd72c4f33791bd Mon Sep 17 00:00:00 2001 From: GustaafL Date: Thu, 20 Jul 2023 17:56:42 +0200 Subject: [PATCH 4/4] docs(sensor): API changelog correct date Signed-off-by: GustaafL --- documentation/api/change_log.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/documentation/api/change_log.rst b/documentation/api/change_log.rst index 29e474348..0941ab77a 100644 --- a/documentation/api/change_log.rst +++ b/documentation/api/change_log.rst @@ -5,12 +5,16 @@ API change log .. note:: The FlexMeasures API follows its own versioning scheme. This is also reflected in the URL, allowing developers to upgrade at their own pace. +v3.0-11 | 2023-07-20 +"""""""""""""""""""" + +- Added REST endpoint for fetching one sensor: `/sensors/` (GET) + v3.0-10 | 2023-06-12 """""""""""""""""""" - Introduced the ``storage-efficiency`` field to the ``flex-model``field for `/sensors//schedules/trigger` (POST). - Introduced the ``database_redis`` optional field to the response of the endpoint `/health/ready` (GET). -- Added REST endpoint for fetching one sensor: `/sensors/` (GET) v3.0-9 | 2023-04-26 """""""""""""""""""