Skip to content

Commit ae565a0

Browse files
Fix sonarqube issues and make showerthoughts async
1 parent ffd882f commit ae565a0

7 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎docker-compose.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ services:
1313
- CALENDAR_TIMEZONE=${CALENDAR_TIMEZONE}
1414
- WATCHED_CHANNELS=${WATCHED_CHANNELS}
1515
- SLACK_API_TOKEN=${SLACK_API_TOKEN}
16-
- SLACK_JUMPSTART_MESSAGE=${SLACK_JUMPSTART_MESSAGE}
16+
- SLACK_JUMPSTART_MESSAGE=${SLACK_JUMPSTART_MESSAGE}

‎mkdocs.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ plugins:
5858
show_root_full_path: false
5959
show_signature: false
6060
filters:
61-
- ".*"
61+
- ".*"

‎src/api/endpoints.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from logging import getLogger, Logger
22

33
import json
4+
import httpx
45
import random
56
import textwrap
6-
import requests
77

88
from fastapi import APIRouter, Request, Form
99
from fastapi.responses import JSONResponse
@@ -110,13 +110,13 @@ async def message_actions(payload: str = Form(...)) -> JSONResponse:
110110
slack.add_announcement(form_json.get("text", None))
111111

112112
if response_url:
113-
requests.post(
113+
await httpx.post(
114114
response_url,
115115
json={"text": "Posting right now :^)", "replace_original": True},
116116
)
117117
else:
118118
if response_url:
119-
requests.post(
119+
await httpx.post(
120120
response_url,
121121
json={"text": "Okay :( maybe next time", "replace_original": True},
122122
)
@@ -129,7 +129,7 @@ async def message_actions(payload: str = Form(...)) -> JSONResponse:
129129

130130

131131
@router.get("/showerthoughts")
132-
def showerthoughts() -> JSONResponse:
132+
async def showerthoughts() -> JSONResponse:
133133
"""
134134
Returns a random shower thought from the Reddit API.
135135
@@ -142,17 +142,20 @@ def showerthoughts() -> JSONResponse:
142142
try:
143143
logger.info("Fetching shower thoughts from Reddit API...")
144144

145-
reddit_data: requests.Response = requests.get(
146-
"https://www.reddit.com/r/showerthoughts/top.json",
147-
headers={"User-agent": "Showerthoughtbot 0.1"},
148-
)
145+
async with httpx.AsyncClient() as client:
146+
reddit_data: httpx.Response = await client.get(
147+
"https://www.reddit.com/r/showerthoughts/top.json",
148+
headers={"User-agent": "Showerthoughtbot 0.1"},
149+
)
150+
151+
reddit_json = reddit_data.json()
149152

150-
if len(reddit_data.json()["data"]["children"]) == 0:
153+
if len(reddit_json["data"]["children"]) == 0:
151154
logger.warning("No shower thoughts found in Reddit API response.")
152155
return JSONResponse(response)
153156

154157
shower_thought: str = textwrap.fill(
155-
(random.choice(reddit_data.json()["data"]["children"])["data"]["title"]), 50
158+
(random.choice(reddit_json["data"]["children"])["data"]["title"]), 50
156159
)
157160

158161
response["data"] = shower_thought

‎src/config.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
CALENDAR_API_KEY: str = os.getenv("CALENDAR_API_KEY", None)
2020

2121
if SLACK_API_TOKEN in (None, ""):
22-
Exception("Missing SLACK_API_TOKEN")
22+
raise Exception("Missing SLACK_API_TOKEN")
2323

2424
if CALENDAR_API_KEY in (None, "") and CALENDAR_URL in (None, ""):
25-
Exception("Missing CALENDAR_API_KEY or CALENDAR_URL")
25+
raise Exception("Missing CALENDAR_API_KEY or CALENDAR_URL")
2626

2727
with open(os.path.join(BASE_DIR, "static", "slack", "dm_request_template.json")) as f:
2828
SLACK_DM_TEMPLATE = json.load(f)

‎src/core/slack.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
import json
32

43
from logging import getLogger, Logger
54

@@ -11,7 +10,12 @@
1110

1211
logger: Logger = getLogger(__name__)
1312

14-
client: AsyncWebClient = AsyncWebClient(token=SLACK_API_TOKEN)
13+
client: AsyncWebClient | None = None
14+
15+
try:
16+
client = AsyncWebClient(token=SLACK_API_TOKEN)
17+
except Exception as e:
18+
logger.error(f"Failed to initialize Slack client: {e}")
1519

1620
announcements: list[str] = ["Welcome to Jumpstart!"]
1721

‎src/requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ jinja2==3.1.6
44
python-multipart==0.0.22
55
logging==0.4.9.6
66
requests==2.32.5
7+
httpx==0.28.1
78

89
# Config
910
dotenv==0.9.9

‎src/templates/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@
5151
</div>
5252
</div>
5353
</div>
54-
<script src="https://weatherwidget.io/js/widget.min.js"></script>
54+
<script src="https://weatherwidget.io/js/widget.min.js" integrity="sha384-z1SrZstAkfpo40+eVsXy73T2tzbGOviy6svjVLsoiZvGhs2mgIMzFdWh1YSuoH9D" crossorigin="anonymous"></script>
5555
<script src="{{ url_for('static', path='js/main.js') }}"></script>
5656
{% endblock %}

0 commit comments

Comments
 (0)