Skip to content

Commit

Permalink
Refactor post method in SoupPage class to include JSON data serializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
mohamedmamdouh22 committed Jan 7, 2025
1 parent 1340d31 commit 6d26f53
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sdk/harambe/contrib/soup/impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Optional

from bs4 import BeautifulSoup, Tag
import json

# noinspection PyProtectedMember
from curl_cffi.requests import AsyncSession, HeaderTypes
Expand Down Expand Up @@ -88,16 +89,23 @@ class SoupResponseWithStatus:

async def post(self, url: str, data: dict[str, Any], **kwargs: Any) -> Any:
res = await self._session.post(
url, headers=self._extra_headers, data=data, **kwargs, impersonate="chrome"
url,
headers=self._extra_headers,
data=json.dumps(data),
**kwargs,
impersonate="chrome",
)
if self._tracer:
self._tracer.log_request(res)

self._url = res.url
self._soup = BeautifulSoup(await res.text, "html.parser")
content_type = res.headers.get("Content-Type", "")
if "application/json" in content_type:
return res.json()
self._soup = BeautifulSoup(res.text, "html.parser")

class SoupResponseWithStatus:
status: int = res.status
status: int = res.status_code

return SoupResponseWithStatus()

Expand Down

0 comments on commit 6d26f53

Please sign in to comment.