11from logging import getLogger , Logger
22
33import json
4+ import httpx
45import random
56import textwrap
6- import requests
77
88from fastapi import APIRouter , Request , Form
99from 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
0 commit comments