@@ -230,31 +230,39 @@ async fn fetch_devforum_data(
230230 request : & reqwest:: Client ,
231231 roblox_username : & str ,
232232) -> anyhow:: Result < DevForumAPIResponse > {
233- // Construct the DevForum API endpoint using the Roblox username and make the request.
234- let mut res = request
235- . get ( construct_devforum_endpoint ( roblox_username) )
236- . send ( )
237- . await ?;
233+ let endpoint = construct_devforum_endpoint ( roblox_username) ;
238234
239- if res. status ( ) . is_client_error ( ) && DEVFORUM_COOKIE . is_some ( ) {
240- // Retry the request with the DevForum cookie if the first attempt fails
241- res = request
242- . get ( construct_devforum_endpoint ( roblox_username) )
243- . header ( COOKIE , format ! ( "_t={}" , * DEVFORUM_COOKIE . as_ref( ) . unwrap( ) ) )
244- . send ( )
245- . await ?;
235+ // Attempt request without the cookie first
236+ let res = request. get ( & endpoint) . send ( ) . await ?;
237+ if res. status ( ) . is_success ( ) {
238+ if let Ok ( data) = res. json :: < DevForumAPIResponse > ( ) . await {
239+ return Ok ( data) ;
240+ }
246241 }
247242
248- // Check if the response was successful and parse the JSON data.
243+ // Return early if the cookie is not set
244+ if DEVFORUM_COOKIE . is_none ( ) {
245+ anyhow:: bail!(
246+ "Failed to fetch DevForum data for roblox_username={roblox_username} without cookie"
247+ )
248+ }
249+
250+ // If the request fails, try again with the cookie
251+ let res = request
252+ . get ( endpoint)
253+ . header ( COOKIE , format ! ( "_t={}" , * DEVFORUM_COOKIE . as_ref( ) . unwrap( ) ) )
254+ . send ( )
255+ . await ?;
256+
249257 if res. status ( ) . is_success ( ) {
250258 res. json :: < DevForumAPIResponse > ( )
251259 . await
252- . context ( "parse devforum data" )
260+ . context ( "parse devforum data with cookie " )
253261 } else {
254- Err ( anyhow:: anyhow !(
255- "Failed to fetch DevForum data for roblox_username={roblox_username}, received status: {}" ,
262+ anyhow:: bail !(
263+ "Failed to fetch DevForum data for roblox_username={roblox_username} with cookie , received status: {}" ,
256264 res. status( )
257- ) )
265+ )
258266 }
259267}
260268
0 commit comments