Skip to content

Commit c8e5c9f

Browse files
committed
[tags] (batching) Add batching and error handling in tagging proccess
1 parent 46fbb1f commit c8e5c9f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

feedly/api_client/data.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
handy getter methods, but otherwise you can just use a .json property to access the
44
raw json passed back by the client.
55
"""
6-
from typing import Any, Callable, Dict, List, Optional, Union
6+
import logging
7+
from typing import Any, Callable, Dict, List, Optional, Union, Set
78
from urllib.parse import quote_plus
89

10+
from requests import HTTPError, Response
11+
912
from feedly.api_client.protocol import APIClient
10-
from feedly.api_client.stream import EnterpriseStreamId, STREAM_SOURCE_ENTERPRISE, STREAM_SOURCE_USER, StreamBase, StreamIdBase, StreamOptions, UserStreamId
13+
from feedly.api_client.stream import EnterpriseStreamId, STREAM_SOURCE_ENTERPRISE, STREAM_SOURCE_USER, StreamBase, \
14+
StreamIdBase, StreamOptions, UserStreamId
15+
16+
logger = logging.getLogger(__name__)
1117

1218

1319
class FeedlyData:
@@ -72,9 +78,25 @@ class TagBase(Streamable):
7278
def tag_entry(self, entry_id:str):
7379
self._client.do_api_request(f'/v3/tags/{quote_plus(self["id"])}', method='put', data={'entryId': entry_id})
7480

75-
def tag_entries(self, entry_ids: List[str]):
76-
self._client.do_api_request(f'/v3/tags/{quote_plus(self["id"])}', method='put',
77-
data={'entryIds': [entry_id for entry_id in entry_ids]})
81+
def tag_entries(self, entry_ids: List[str], errors_to_ignore: Set[int] = None):
82+
"""
83+
Tag the given entries.
84+
Will ignore given error_codes (default is only ignore 409, that happens when a duplicate is already tagged)
85+
"""
86+
if errors_to_ignore is None:
87+
errors_to_ignore = {409}
88+
for i in range(0, len(entry_ids), 50):
89+
self._tag_entries_batch(entry_ids[i: i + 50], errors_to_ignore)
90+
91+
def _tag_entries_batch(self, entry_ids: List[str], errors_to_ignore: Set[int]):
92+
try:
93+
self._client.do_api_request(f'/v3/tags/{quote_plus(self["id"])}', method='put', data={'entryIds': entry_ids})
94+
except HTTPError as e:
95+
resp: Response = e.response
96+
if resp.status_code in errors_to_ignore:
97+
logger.warning(f"Got 409 Client Error while tagging batch of entries: {resp.text}")
98+
else:
99+
raise e
78100

79101
def untag_entry(self, entry_id: str):
80102
self.untag_entries([entry_id])

0 commit comments

Comments
 (0)