Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🦍Refactor: Enhance Unified Post Method: Intelligent Content-Type Handling #110

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "harambe-core"
version = "0.59.5"
version = "0.59.6"
description = "Core types for harambe SDK 🐒🍌"
authors = [
{ name = "Adam Watkins", email = "[email protected]" }
Expand Down
2 changes: 1 addition & 1 deletion core/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions sdk/harambe/contrib/soup/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json

# noinspection PyProtectedMember
from curl_cffi.requests import AsyncSession, HeaderTypes
from curl_cffi.requests import AsyncSession, HeaderTypes, Response
from harambe.contrib.soup.tracing import Tracer
from harambe.contrib.types import AbstractElementHandle, AbstractPage, Selectable
from harambe.contrib.types import ResponseWithStatus
Expand Down Expand Up @@ -92,37 +92,38 @@ async def post(
self,
url: str,
data: dict[str, Any],
params: Optional[dict[str, Any]] = None,
headers: Optional[HeaderTypes] = None,
**kwargs: Any,
) -> Any:
) -> Response:
content_type = (
(headers or self._extra_headers or {}).get("Content-Type", "").lower()
)

processed_data = (
json.dumps(data) if "application/json" in content_type else data
)
res = await self._session.post(
url,
headers=headers or self._extra_headers,
data=json.dumps(data),
data=processed_data,
params=params or {},
impersonate="chrome",
**kwargs,
)
res.raise_for_status()

if self._tracer:
self._tracer.log_request(res)

self._url = res.url
content_type = res.headers.get("Content-Type", "")

if "application/json" in content_type:

class SoupResponseWithStatus:
status: int = res.status_code
body: dict[str, Any] = res.json()

return SoupResponseWithStatus()
return res

self._soup = BeautifulSoup(res.text, "html.parser")

class SoupResponseWithStatus:
status: int = res.status_code
body: str = res.text

return SoupResponseWithStatus()
return res

async def query_selector_all(self, selector: str) -> list[SoupElementHandle]:
return SoupElementHandle.from_tags(self._soup.select(selector))
Expand Down
4 changes: 2 additions & 2 deletions sdk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[project]
name = "harambe-sdk"
version = "0.59.5"
version = "0.59.6"
description = "Data extraction SDK for Playwright 🐒🍌"
authors = [
{ name = "Adam Watkins", email = "[email protected]" }
]
requires-python = ">=3.11,<4.0"
readme = "README.md"
dependencies = [
"harambe_core==0.59.5",
"harambe_core==0.59.6",
"playwright==1.47.0",
"beautifulsoup4==4.12.3",
"requests==2.32.3",
Expand Down
4 changes: 2 additions & 2 deletions sdk/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.