Skip to content

Commit

Permalink
Add update_entry() method
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Oct 8, 2023
1 parent 2f55ce2 commit 90b414c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
17 changes: 17 additions & 0 deletions miniflux.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ def get_entries(self, **kwargs) -> Dict:
return response.json()
raise ClientError(response)

def update_entry(self, entry_id: int, title: str = None, content: str = None) -> Dict:
endpoint = self._get_endpoint(f"/entries/{entry_id}")
data = self._get_modification_params(**{
"title": title,
"content": content,
})
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
raise ClientError(response)

def update_entries(self, entry_ids: List[int], status: str) -> bool:
endpoint = self._get_endpoint("/entries")
data = {"entry_ids": entry_ids, "status": status}
Expand Down
80 changes: 80 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,86 @@ def test_get_category_entries(self):

assert result == expected_result

def test_update_entry_title(self):
requests = _get_request_mock()
expected_result = {"id": 123, "title": "New title"}

response = mock.Mock()
response.status_code = 201
response.json.return_value = expected_result

requests.put.return_value = response

client = miniflux.Client("http://localhost", "username", "password")
result = client.update_entry(entry_id=123, title="New title")

requests.put.assert_called_once_with(
"http://localhost/v1/entries/123",
headers=None,
auth=("username", "password"),
data=mock.ANY,
timeout=30.0,
)

_, kwargs = requests.put.call_args
payload = json.loads(kwargs.get("data"))

self.assertEqual(payload.get("title"), "New title")
self.assertEqual(result, expected_result)

def test_update_entry_content(self):
requests = _get_request_mock()
expected_result = {"id": 123, "content": "New content"}

response = mock.Mock()
response.status_code = 201
response.json.return_value = expected_result

requests.put.return_value = response

client = miniflux.Client("http://localhost", "username", "password")
result = client.update_entry(entry_id=123, content="New content")

requests.put.assert_called_once_with(
"http://localhost/v1/entries/123",
headers=None,
auth=("username", "password"),
data=mock.ANY,
timeout=30.0,
)

_, kwargs = requests.put.call_args
payload = json.loads(kwargs.get("data"))

self.assertEqual(payload.get("content"), "New content")
self.assertEqual(result, expected_result)

def test_update_entries_status(self):
requests = _get_request_mock()

response = mock.Mock()
response.status_code = 204

requests.put.return_value = response

client = miniflux.Client("http://localhost", "username", "password")
result = client.update_entries(entry_ids=[123, 456], status="read")

requests.put.assert_called_once_with(
"http://localhost/v1/entries",
headers=None,
auth=("username", "password"),
data=mock.ANY,
timeout=30.0,
)

_, kwargs = requests.put.call_args
payload = json.loads(kwargs.get("data"))

self.assertEqual(payload.get("entry_ids"), [123, 456])
self.assertEqual(payload.get("status"), "read")
self.assertTrue(result)


def _get_request_mock():
patcher = mock.patch("miniflux.requests")
Expand Down

0 comments on commit 90b414c

Please sign in to comment.