Skip to content

Commit 4ea7fbd

Browse files
committed
Add enhydris_api_client.put_timeseries() (fixes DEV-130)
1 parent e2bd1b9 commit 4ea7fbd

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

docs/enhydris_api_client.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ Reference
6767
.. method:: list_timeseries(self, station_id, timeseries_group_id)
6868
get_timeseries(self, station_id, timeseries_group_id, timeseries_id)
6969
post_timeseries(self, station_id, timeseries_group_id, data)
70+
put_timeseries(self, station_id, timeseries_group_id, timeseries_id, data)
7071
delete_timeseries(self, station_id, timeseries_group_id, timeseries_id)
7172

72-
Methods that create, retrieve or delete time series. Similar to
73-
the ones for station. :meth:`~EnhydrisApiClient.list_timeseries`
74-
returns a list of dictionaries.
73+
Methods that create, retrieve, update or delete time series.
74+
Similar to the ones for station.
75+
:meth:`~EnhydrisApiClient.list_timeseries` returns a list of
76+
dictionaries.
7577

7678
.. method:: read_tsdata(self, station_id, timeseries_group_id, timeseries_id, start_date=None, end_date=None, timezone=None)
7779
.post_tsdata(self, station_id, timeseries_group_id, timeseries_id, ts)

src/enhydris_api_client/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ def post_timeseries(
244244
self.check_response()
245245
return self.response.json()["id"]
246246

247+
def put_timeseries(
248+
self,
249+
station_id: int,
250+
timeseries_group_id: int,
251+
timeseries_id: int,
252+
data: JSONDict,
253+
) -> None:
254+
self.response = self.session.put(
255+
urljoin(
256+
self.base_url,
257+
f"api/stations/{station_id}/timeseriesgroups/{timeseries_group_id}/"
258+
f"timeseries/{timeseries_id}/",
259+
),
260+
data=data,
261+
)
262+
self.check_response()
263+
247264
def delete_timeseries(
248265
self, station_id: int, timeseries_group_id: int, timeseries_id: int
249266
) -> None:

tests/enhydris_api_client/test_timeseries.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ def test_returns_id(self) -> None:
8787
self.assertEqual(self.data, 43)
8888

8989

90+
class PutTimeseriesTestCase(TestCase):
91+
def setUp(self) -> None:
92+
self.session_patcher = mock_session(
93+
**{"post.return_value.json.return_value": {"id": 43}}
94+
)
95+
self.mock_requests_session = self.session_patcher.start()
96+
self.client = EnhydrisApiClient("https://mydomain.com")
97+
self.data = self.client.put_timeseries(41, 42, 43, data={"location": "Syria"})
98+
99+
def tearDown(self) -> None:
100+
self.session_patcher.stop()
101+
102+
def test_makes_request(self) -> None:
103+
self.mock_requests_session.return_value.put.assert_called_once_with(
104+
"https://mydomain.com/api/stations/41/timeseriesgroups/42/timeseries/43/",
105+
data={"location": "Syria"},
106+
)
107+
108+
90109
class FailedPostTimeseriesTestCase(TestCase):
91110
@mock_session(**{"post.return_value.status_code": 404})
92111
def test_raises_exception_on_error(self, m: mock.MagicMock) -> None:

0 commit comments

Comments
 (0)