Skip to content

Commit ac2b623

Browse files
[youtube-data-api] fix: handle HttpError when calling API
If video or channel not found and cannot be access return empty response.
1 parent 6dfac39 commit ac2b623

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

libs/community/google/youtube/youtube-data-api/garf_youtube_data_api/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Defines simplified imports."""
15+
"""Interacts with YouTube Data API via garf."""
1616

1717
from __future__ import annotations
1818

@@ -24,4 +24,4 @@
2424
'YouTubeDataApiReportFetcher',
2525
]
2626

27-
__version__ = '0.0.10'
27+
__version__ = '0.0.11'

libs/community/google/youtube/youtube-data-api/garf_youtube_data_api/api_clients.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from garf_core import api_clients, query_editor
2121
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
2223
from typing_extensions import override
2324

2425
from garf_youtube_data_api import exceptions
@@ -91,8 +92,11 @@ def get_response(
9192
def _list(
9293
self, service, part: str, next_page_token: str | None = None, **kwargs
9394
) -> dict:
94-
if next_page_token:
95-
return service.list(
96-
part=part, pageToken=next_page_token, **kwargs
97-
).execute()
98-
return service.list(part=part, **kwargs).execute()
95+
try:
96+
if next_page_token:
97+
return service.list(
98+
part=part, pageToken=next_page_token, **kwargs
99+
).execute()
100+
return service.list(part=part, **kwargs).execute()
101+
except HttpError:
102+
return {'items': None}

libs/community/google/youtube/youtube-data-api/tests/e2e/test_lib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
fetcher = report_fetcher.YouTubeDataApiReportFetcher(api_client=api_client)
2323

2424

25+
def test_query_with_non_existing_id_returns_empty_report():
26+
query = 'SELECT id, snippet.title AS title FROM videos'
27+
test_youtube_id = 'no-existing-video-id'
28+
fetched_report = fetcher.fetch(query, id=[test_youtube_id])
29+
assert not fetched_report
30+
31+
2532
def test_query_with_ids_only():
2633
query = 'SELECT id, snippet.title AS title FROM videos'
2734
test_youtube_id = os.getenv('YOUTUBE_ID')

0 commit comments

Comments
 (0)