Skip to content

Commit

Permalink
refactor: replace aiohttp with httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 19, 2024
1 parent 624800e commit af871bd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 412 deletions.
334 changes: 2 additions & 332 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ packages = [
[tool.poetry.dependencies]
python = "^3.10"
#
aiohttp = "^3.10.3"
asyncpg = "^0.29.0"
httpx = { extras = ["http2"], version = "^0.27.0" }
jinja2 = "^3.1.4"
Expand Down
30 changes: 15 additions & 15 deletions server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ def __user_from_session(session: dict[str, Any]) -> User:


async def refresh(refresh_token: str) -> dict[str, Any]:
async with http_client.post(
res = await http_client.post(
"https://bgm.tv/oauth/access_token",
data={
"refresh_token": refresh_token,
"client_id": BGM_TV_APP_ID,
"grant_type": "refresh_token",
"client_secret": BGM_TV_APP_SECRET,
},
) as res:
if res.status >= 300:
raise InternalServerException("api request error")
return orjson.loads(await res.read())
)
if res.status_code >= 300:
raise InternalServerException("api request error")
return orjson.loads(res.content)


class MyAuthenticationMiddleware(SessionAuthMiddleware):
Expand Down Expand Up @@ -95,7 +95,7 @@ def login() -> Redirect:

@litestar.get("/oauth_callback")
async def callback(code: str, request: Request) -> Redirect:
async with http_client.post(
res = await http_client.post(
"https://bgm.tv/oauth/access_token",
data={
"code": code,
Expand All @@ -104,20 +104,20 @@ async def callback(code: str, request: Request) -> Redirect:
"redirect_uri": CALLBACK_URL,
"client_secret": BGM_TV_APP_SECRET,
},
) as res:
if res.status >= 300:
raise InternalServerException("api request error")
data = await res.json()
)
if res.status_code >= 300:
raise InternalServerException("api request error")
data = res.json()

user_id = data["user_id"]
access_token = data["access_token"]

async with http_client.get(
res = await http_client.get(
"https://api.bgm.tv/v0/me", headers={"Authorization": f"Bearer {access_token}"}
) as res:
if res.status >= 300:
raise InternalServerException("api request error")
user = await res.json()
)
if res.status_code >= 300:
raise InternalServerException("api request error")
user = res.json()

group_id = user["user_group"]

Expand Down
31 changes: 2 additions & 29 deletions server/base.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
from collections.abc import Iterator
from http.cookies import BaseCookie
from typing import Any

import aiohttp
import asyncpg
import httpx
import litestar
from aiohttp.abc import AbstractCookieJar, ClearCookiePredicate
from aiohttp.typedefs import LooseCookies
from litestar.exceptions import ClientException
from litestar.status_codes import HTTP_400_BAD_REQUEST
from loguru import logger
from typing_extensions import Never
from yarl import URL

from config import PG_DSN
from server.model import User


class DisableCookiesJar(AbstractCookieJar):
"""disable cookies on aiohttp client"""

def clear(self, predicate: ClearCookiePredicate | None = None) -> None:
return

def clear_domain(self, domain: str) -> None:
return

def update_cookies(self, cookies: LooseCookies, response_url: URL | None = None) -> None:
return

def filter_cookies(self, request_url: URL) -> BaseCookie[str]:
return BaseCookie()

def __len__(self) -> int:
return 0

def __iter__(self) -> Iterator[Any]:
yield from ()


http_client = aiohttp.ClientSession(cookie_jar=DisableCookiesJar())
http_client = httpx.AsyncClient(follow_redirects=False)
pg = asyncpg.create_pool(dsn=PG_DSN)


Expand Down
33 changes: 15 additions & 18 deletions server/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Annotated, Any

import litestar
import orjson
from litestar import Response
from litestar.enums import RequestEncodingType
from litestar.exceptions import (
Expand All @@ -30,12 +29,10 @@ async def suggest_ui(request: Request, subject_id: int = 0) -> Response[Any]:
request.set_session({"backTo": request.url.path + f"?subject_id={subject_id}"})
return Redirect("/login")

async with http_client.get(
f"https://next.bgm.tv/p1/wiki/subjects/{subject_id}", allow_redirects=False
) as res:
if res.status >= 300:
raise NotFoundException()
data = await res.json()
res = await http_client.get(f"https://next.bgm.tv/p1/wiki/subjects/{subject_id}")
if res.status_code >= 300:
raise NotFoundException()
data = res.json()
return Template(
"suggest.html.jinja2",
context={"data": data, "subject_id": subject_id, "CAPTCHA_SITE_KEY": TURNSTILE_SITE_KEY},
Expand Down Expand Up @@ -68,22 +65,22 @@ async def suggest_api(
if not data.desc:
raise ValidationException("missing suggestion description")

async with http_client.post(
res = await http_client.post(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
data={
"secret": TURNSTILE_SECRET_KEY,
"response": data.cf_turnstile_response,
},
) as res:
if res.status > 300:
raise BadRequestException("验证码无效")
captcha_data = orjson.loads(await res.read())
if captcha_data.get("success") is not True:
raise BadRequestException("验证码无效")

async with http_client.get(f"https://next.bgm.tv/p1/wiki/subjects/{subject_id}") as res:
res.raise_for_status()
original_wiki = await res.json()
)
if res.status_code > 300:
raise BadRequestException("验证码无效")
captcha_data = res.json()
if captcha_data.get("success") is not True:
raise BadRequestException("验证码无效")

res = await http_client.get(f"https://next.bgm.tv/p1/wiki/subjects/{subject_id}")
res.raise_for_status()
original_wiki = res.json()

original = Wiki(
name=original_wiki["name"],
Expand Down
33 changes: 16 additions & 17 deletions server/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Annotated, Any

import litestar
import orjson
from asyncpg import Record
from asyncpg.pool import PoolConnectionProxy
from litestar import Response
Expand Down Expand Up @@ -90,7 +89,7 @@ async def __accept_patch(patch: Patch, conn: PoolConnectionProxy[Record], auth:
if not auth.is_access_token_fresh():
return Redirect("/login")

async with http_client.patch(
res = await http_client.patch(
f"https://next.bgm.tv/p1/wiki/subjects/{patch.subject_id}",
headers={"Authorization": f"Bearer {auth.access_token}"},
json={
Expand All @@ -111,27 +110,27 @@ async def __accept_patch(patch: Patch, conn: PoolConnectionProxy[Record], auth:
}
),
},
) as res:
if res.status >= 300:
data = orjson.loads(await res.read())
if data.get("code") == "SUBJECT_CHANGED":
await conn.execute(
"""
)
if res.status_code >= 300:
data = res.json()
if data.get("code") == "SUBJECT_CHANGED":
await conn.execute(
"""
update patch set
state = $1,
wiki_user_id = $2,
updated_at = $3
where id = $4 and deleted_at is NULL
""",
PatchState.Outdated,
auth.user_id,
datetime.now(tz=UTC),
patch.id,
)
return Redirect(f"/patch/{patch.id}")

logger.error("failed to apply patch {!r}", data)
raise InternalServerException()
PatchState.Outdated,
auth.user_id,
datetime.now(tz=UTC),
patch.id,
)
return Redirect(f"/patch/{patch.id}")

logger.error("failed to apply patch {!r}", data)
raise InternalServerException()

await conn.execute(
"""
Expand Down

0 comments on commit af871bd

Please sign in to comment.