forked from Xenomega/EchoRelay
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public HTTP API endpoints for centralized app (#22)
- Loading branch information
1 parent
a9f4d5d
commit 692c71c
Showing
15 changed files
with
509 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace EchoRelay.Core.Monitoring; | ||
|
||
//TO DO : Encrypt data | ||
public class ApiClient | ||
{ | ||
private readonly HttpClient _httpClient; | ||
|
||
public ApiClient(string baseUrl) | ||
{ | ||
_httpClient = new HttpClient | ||
{ | ||
BaseAddress = new Uri(baseUrl) | ||
}; | ||
} | ||
|
||
//TODO encrypt data to avoid people to send fake data | ||
public async Task<string> PostMonitoringData(string endpoint, string data) | ||
{ | ||
HttpContent content = new StringContent(data, Encoding.UTF8, "application/json"); | ||
HttpResponseMessage response = await _httpClient.PostAsync(endpoint, content); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
throw new Exception($"API request failed with status code: {response.StatusCode}"); | ||
} | ||
|
||
public async Task DeleteMonitoringData(string endpoint) | ||
{ | ||
HttpResponseMessage response = await _httpClient.DeleteAsync(endpoint); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
throw new Exception($"DELETE request failed with status code: {response.StatusCode}"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Security.Cryptography; | ||
|
||
namespace EchoRelay.Core.Monitoring; | ||
|
||
public class ApiManager | ||
{ | ||
|
||
/// <summary> | ||
/// Uri to the monitoring API | ||
/// </summary> | ||
public string URI { get; } = "http://localhost:3000/api/"; | ||
//public string URI { get; } = "http://51.75.140.182:3000/api/"; | ||
|
||
public PeerStatsObject peerStatsObject; | ||
/// <summary> | ||
/// Public key to encrypt data | ||
/// </summary> | ||
public ApiClient Monitoring { get; } | ||
|
||
public Server Server { get; } | ||
|
||
public GameServer GameServer { get; } | ||
public PeerStats PeerStats { get; } | ||
|
||
private static ApiManager instance = new ApiManager(); | ||
|
||
private ApiManager() | ||
{ | ||
Monitoring = new ApiClient(URI); | ||
GameServer = new GameServer(Monitoring); | ||
PeerStats = new PeerStats(Monitoring); | ||
Server = new Server(Monitoring); | ||
peerStatsObject = new PeerStatsObject(); | ||
} | ||
|
||
// Method to get the singleton instance | ||
public static ApiManager Instance | ||
{ | ||
get { return instance; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace EchoRelay.Core.Monitoring; | ||
|
||
public class GameServer | ||
{ | ||
private readonly ApiClient _apiClient; | ||
|
||
public GameServer(ApiClient apiClient) | ||
{ | ||
_apiClient = apiClient; | ||
} | ||
|
||
public async Task DeleteGameServerAsync(string sessionId) | ||
{ | ||
// Define the endpoint for deleting a game server with the sessionID | ||
string endpoint = $"deleteGameServer/{sessionId}"; | ||
|
||
try | ||
{ | ||
await _apiClient.DeleteMonitoringData(endpoint); | ||
Console.WriteLine($"Game server with session ID {sessionId} deleted successfully from monitoring."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error deleting the game server from monitoring: {ex.Message}"); | ||
} | ||
} | ||
|
||
public async Task AddGameServerAsync(GameServerObject jsonObject) | ||
{ | ||
// Create a StringContent with the JSON data and set the content type | ||
string jsonData = JsonConvert.SerializeObject(jsonObject); | ||
string endpoint = "addGameServer/"; | ||
|
||
try | ||
{ | ||
await _apiClient.PostMonitoringData(endpoint, jsonData); | ||
Console.WriteLine($"Game server successfully added to monitoring."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error adding the game server from monitoring: {ex.Message}"); | ||
} | ||
} | ||
|
||
public async Task EditGameServer(GameServerObject jsonObject, string gameServerID) | ||
{ | ||
// Create a StringContent with the JSON data and set the content type | ||
string jsonData = JsonConvert.SerializeObject(jsonObject); | ||
string endpoint = $"updateGameServer/{gameServerID}"; | ||
|
||
try | ||
{ | ||
await _apiClient.PostMonitoringData(endpoint, jsonData); | ||
Console.WriteLine($"Game server successfully edited in monitoring."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error editing the game server in monitoring: {ex.Message}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.