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

request in async way #121

Merged
merged 3 commits into from
Sep 8, 2024
Merged
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
28 changes: 14 additions & 14 deletions showcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib.error
import urllib.request

import aiohttp
import server
from aiohttp import web

Expand All @@ -12,26 +13,25 @@
SHOW_CASES = {}


def get_bizyair_news(base_url="https://bizyair.siliconflow.cn"):
async def get_bizyair_news(base_url="https://bizyair.siliconflow.cn"):
url = f"{base_url}/bznews.json"
try:
response = urllib.request.urlopen(url, timeout=5)
if response.getcode() == 200:
data = response.read()
return json.loads(data)
else:
print(f"Failed to fetch news.json: HTTP Status {response.getcode()}")
return {}
except urllib.error.URLError as e:
print(f"Error fetching news.json: {e.reason}")
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=5) as response:
if response.status == 200:
data = await response.text()
return json.loads(data)
else:
print(f"Failed to fetch bznews.json: HTTP Status {response.status}")
return {}
except aiohttp.ClientError as e:
print(f"Error fetching news.json: {e}")
return {}
except Exception as e:
print(f"Error fetching BizyAir news.json: {str(e)}")
print(f"Error fetching BizyAir bznews.json: {str(e)}")
return {}


SHOW_CASES.update()

with open(os.path.join(CURRENT_DIR, "bizyair_example_menu.json"), "r") as file:
SHOW_CASES.update(json.load(file))

Expand Down Expand Up @@ -61,7 +61,7 @@ async def set_api_key_page(request):
@PromptServer.instance.routes.get("/bizyair/news")
async def list_news(request):
return web.Response(
text=json.dumps(get_bizyair_news(), ensure_ascii=False),
text=json.dumps(await get_bizyair_news(), ensure_ascii=False),
content_type="application/json",
)

Expand Down
Loading