Skip to content

Commit

Permalink
fix: Make sure API responses indicate success
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Mar 24, 2023
1 parent d6fcfdc commit a1118fd
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Battlefield rich presence/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static ServerInfo GetServerInfo(string gameName, string serverName)
string stringPayload = JsonConvert.SerializeObject(payload);
StringContent httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
HttpResponseMessage httpResponse = new HttpClient().PostAsync($"https://api.gametools.network/seedergame/{gameName}", httpContent).Result;
httpResponse.EnsureSuccessStatusCode();
string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<ServerInfo>(responseContent);

Expand All @@ -26,6 +27,11 @@ public static ServerInfo OldTitleServerInfo(string unescapedPlayerName, string g
{
string playerName = Uri.EscapeDataString(unescapedPlayerName);
HttpResponseMessage httpResponse = new HttpClient().GetAsync($"https://api.bflist.io/{gameName}/v1/players/{playerName}/server").Result;

// bflist returns a 404 response if the player is not currently playing on a (known) server
// Throw an exception in that case to avoid returning empty ServerInfo
httpResponse.EnsureSuccessStatusCode();

string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<ServerInfo> (responseContent);
}
Expand All @@ -42,6 +48,7 @@ public static ServerInfo GetCurrentServer(string playerName, Resources.Statics.G
StringContent httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var url = $"https://api.gametools.network/currentserver/{Resources.Statics.ShortGameName[game_name].ToLower()}";
HttpResponseMessage httpResponse = new HttpClient().PostAsync(url, httpContent).Result;
httpResponse.EnsureSuccessStatusCode();
string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
if (responseContent == "{}")
{
Expand Down

0 comments on commit a1118fd

Please sign in to comment.