Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 4e9f69f

Browse files
committed
download_linkedin_media, download_profile_picture: add error handling
1 parent 2e467f4 commit 4e9f69f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

linkedin_messaging/linkedin.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,12 @@ async def send_message(
320320
return SendMessageResponse.from_json(await res.text())
321321

322322
async def download_linkedin_media(self, url: str) -> bytes:
323-
return await (await self.session.get(url)).content.read()
323+
async with self.session.get(url) as media_resp:
324+
if not media_resp.ok:
325+
raise Exception(
326+
f"Failed downloading media. Response code {media_resp.status}"
327+
)
328+
return await media_resp.content.read()
324329

325330
# endregion
326331

@@ -331,11 +336,16 @@ async def get_user_profile(self) -> UserProfileResponse:
331336
return UserProfileResponse.from_json(await response.text())
332337

333338
async def download_profile_picture(self, picture: Picture) -> bytes:
334-
resp = await self.session.get(
339+
url = (
335340
picture.vector_image.root_url
336341
+ picture.vector_image.artifacts[-1].file_identifying_url_path_segment
337342
)
338-
return await resp.content.read()
343+
async with await self.session.get(url) as profile_resp:
344+
if not profile_resp.ok:
345+
raise Exception(
346+
f"Failed downloading media. Response code {profile_resp.status}"
347+
)
348+
return await profile_resp.content.read()
339349

340350
# endregion
341351

0 commit comments

Comments
 (0)