Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions kick/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import json
import logging
from typing import TYPE_CHECKING, Any, Coroutine, Optional, TypeVar, Union
from curl_cffi import requests as requests_ORI
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to change the name its imported under. Importing curl_cffi itself is better than changing the name of something from the library


from aiohttp import ClientConnectionError, ClientResponse, ClientSession

from . import __version__
from .errors import (
CloudflareBypassException,
# CloudflareBypassException,
Forbidden,
HTTPException,
InternalKickException,
Expand Down Expand Up @@ -58,7 +59,7 @@


async def json_or_text(response: ClientResponse, /) -> Union[dict[str, Any], str]:
text = await response.text()
text = response.text
try:
try:
return json.loads(text)
Expand Down Expand Up @@ -236,22 +237,21 @@ async def request(self, route: Route, **kwargs) -> Any:
f"Making request to {route.method} {url}. headers: {headers}, params: {kwargs.get('params', None)}, json: {kwargs.get('json', None)}"
)
try:
res = await self.__session.request(
res = await requests_ORI.AsyncSession().request(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like with aiohttp, the session should be reused and stored, not creating a new one

route.method,
url
if self.whitelisted is True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to remove generating the url like this, you should also remove HTTPClient.whitelisted, and the others. kick.Client's docstring would also need to change

else f"{self.bypass_host}:{self.bypass_port}/request?url={url}",
url,
headers=headers,
cookies=cookies,
impersonate="safari15_3",
**kwargs,
)
except ClientConnectionError:
if self.whitelisted is True:
raise InternalKickException("Could Not Connect To Kick") from None
else:
raise CloudflareBypassException(
"Could Not Connect To Bypass Script"
) from None
# else:
# raise CloudflareBypassException(
# "Could Not Connect To Bypass Script"
# ) from None

if res is not None:
self.xsrf_token = str(
Expand All @@ -260,7 +260,7 @@ async def request(self, route: Route, **kwargs) -> Any:

data = await json_or_text(res)

if res.status == 429:
if res.status_code == 429:
self.globally_locked = True
LOGGER.warning(
f"We have been ratelimited at {route.method} {route.url}. Waiting five seconds before trying again...",
Expand All @@ -271,13 +271,13 @@ async def request(self, route: Route, **kwargs) -> Any:
else:
self.globally_locked = False

if 300 > res.status >= 200:
if 300 > res.status_code >= 200:
return data

match res.status:
match res.status_code:
case 400:
error = await error_or_text(data)
raise HTTPException(error, res.status)
raise HTTPException(error, res.status_code)
case 403:
raise Forbidden(await error_or_nothing(data))
case 404:
Expand All @@ -300,10 +300,10 @@ async def request(self, route: Route, **kwargs) -> Any:
if res is not None and data is not None:
txt = await error_or_text(data)

if res.status >= 500:
if res.status_code >= 500:
raise InternalKickException(txt)

raise HTTPException(txt, res.status)
raise HTTPException(txt, res.status_code)

raise RuntimeError("Unreachable situation occured in http handling")

Expand Down Expand Up @@ -493,7 +493,7 @@ async def get_asset(self, url: str) -> bytes:
self.__session = ClientSession()

res = await self.__session.request("GET", url)
match res.status:
match res.status_code:
case 200:
return await res.read()
case 403:
Expand Down