Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_entry() method #22

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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