Skip to content

Commit

Permalink
[media] allowing to pass pathlib.Path as a media file path
Browse files Browse the repository at this point in the history
  • Loading branch information
david-lev committed Nov 1, 2023
1 parent 31c0c99 commit 4593518
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 185 deletions.
10 changes: 7 additions & 3 deletions pywa/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""The internal API for the WhatsApp client."""

from typing import Any, BinaryIO
from typing import Any

import requests
import requests_toolbelt

import pywa
from pywa.errors import WhatsAppError


Expand All @@ -29,6 +30,7 @@ def __init__(
self._session.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
"User-Agent": f"PyWa/{pywa.__version__}",
}
self._common_keys = {
"messaging_product": "whatsapp",
Expand Down Expand Up @@ -176,7 +178,7 @@ def send_text_message(

def upload_media(
self,
media: bytes | BinaryIO,
media: bytes,
mime_type: str,
filename: str,
) -> dict[str, str]:
Expand Down Expand Up @@ -240,19 +242,21 @@ def get_media_url(self, media_id: str) -> dict:
def get_media_bytes(
self,
media_url: str,
**kwargs,
) -> tuple[bytes, str | None]:
"""
Get the bytes of a media file from WhatsApp servers.
Args:
media_url: The URL of the media file (from ``get_media_url``).
**kwargs: Additional arguments to pass to the request.
Returns:
The media file bytes and the MIME type (if available).
"""
headers = self._session.headers.copy()
del headers["Content-Type"]
res = self._session.get(media_url, headers=headers)
res = self._session.get(media_url, headers=headers, **kwargs)
res.raise_for_status()
return res.content, res.headers.get("Content-Type")

Expand Down
Loading

0 comments on commit 4593518

Please sign in to comment.