-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add controllers to SDK and switch app to use them
- Loading branch information
Showing
29 changed files
with
400 additions
and
283 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using MCTools.SDK.Enums.Controllers; | ||
|
||
namespace MCTools.SDK.Controllers | ||
{ | ||
public class ApiClient : IApiClient | ||
{ | ||
private readonly HttpClient _client; | ||
private readonly string _baseAddress; | ||
|
||
public ApiClient(HttpClient client, string userAgent, ApiRelease apiRelease = ApiRelease.Release, string overrideBaseAddress = "") | ||
{ | ||
_client = client; | ||
_baseAddress = apiRelease switch | ||
{ | ||
ApiRelease.Release => "https://mctools-api.mullak99.co.uk", | ||
ApiRelease.Beta => "https://mctools-api-beta.mullak99.co.uk", | ||
_ => overrideBaseAddress.TrimEnd('/') | ||
}; | ||
_client.BaseAddress = new Uri(_baseAddress); | ||
_client.DefaultRequestHeaders.Add("User-Agent", userAgent); | ||
_client.DefaultRequestHeaders.Add("Accept", "application/json"); | ||
} | ||
|
||
public string BuildRequestUri(string requestUri, string apiVersion = "1.0") | ||
=> BuildRequestUriRaw($"api/v{apiVersion}/{requestUri.TrimStart('/')}"); | ||
|
||
public string BuildRequestUriRaw(string requestUri) | ||
=> $"{_baseAddress.TrimEnd('/')}/{requestUri.TrimStart('/')}"; | ||
|
||
public HttpClient GetClient() | ||
=> _client; | ||
|
||
public string GetBaseAddress() | ||
=> _baseAddress; | ||
} | ||
|
||
public interface IApiClient | ||
{ | ||
string BuildRequestUri(string requestUri, string apiVersion); | ||
string BuildRequestUriRaw(string requestUri); | ||
HttpClient GetClient(); | ||
string GetBaseAddress(); | ||
} | ||
} |
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 MCTools.SDK.Interfaces.Controllers; | ||
using MCTools.SDK.Models; | ||
using Newtonsoft.Json; | ||
|
||
namespace MCTools.SDK.Controllers | ||
{ | ||
public class BedrockController : IEditionController | ||
{ | ||
private IApiClient _client { get; } | ||
private string _apiVersion { get; } | ||
|
||
public BedrockController(IApiClient client, string apiVersion = "1.0") | ||
{ | ||
_client = client; | ||
_apiVersion = apiVersion; | ||
} | ||
|
||
public async Task<List<MCVersion>> GetVersions() | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri("bedrock/versions", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return new(); | ||
|
||
string rawJson = await res.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<List<MCVersion>>(rawJson) ?? new(); | ||
} | ||
|
||
public async Task<MCAssets> GetAssets(string version) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri($"bedrock/version/{version}", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return new(); | ||
|
||
string rawJson = await res.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<MCAssets>(rawJson) ?? new(); | ||
} | ||
} | ||
} |
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,32 @@ | ||
using MCTools.SDK.Interfaces.Controllers; | ||
using System.Net; | ||
|
||
namespace MCTools.SDK.Controllers | ||
{ | ||
public class HealthController : IController | ||
{ | ||
private IApiClient _client { get; } | ||
|
||
public HealthController(IApiClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
public async Task<HttpStatusCode> GetApiStatus(uint timeoutMs = 2000) | ||
{ | ||
using CancellationTokenSource cancellationTokenSource = new(TimeSpan.FromMilliseconds(timeoutMs)); | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUriRaw("health")); | ||
|
||
try | ||
{ | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req, cancellationTokenSource.Token); | ||
return res.StatusCode; | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// The task was canceled due to timeout | ||
return HttpStatusCode.BadGateway; | ||
} | ||
} | ||
} | ||
} |
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,66 @@ | ||
using MCTools.SDK.Interfaces.Controllers; | ||
using MCTools.SDK.Models; | ||
using Newtonsoft.Json; | ||
|
||
namespace MCTools.SDK.Controllers | ||
{ | ||
public class JavaController : IEditionController | ||
{ | ||
private IApiClient _client { get; } | ||
private string _apiVersion { get; } | ||
|
||
public JavaController(IApiClient client, string apiVersion = "1.0") | ||
{ | ||
_client = client; | ||
_apiVersion = apiVersion; | ||
} | ||
|
||
public async Task<List<MCVersion>> GetVersions() | ||
=> await GetVersions(false); | ||
|
||
public async Task<List<MCVersion>> GetVersions(bool bypassVersionLimit) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri($"java/versions?bypassVersionLimit={bypassVersionLimit}", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return new(); | ||
|
||
string rawJson = await res.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<List<MCVersion>>(rawJson) ?? new(); | ||
} | ||
|
||
public async Task<MCAssets> GetAssets(string version) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri($"java/version/{version}", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return new(); | ||
|
||
string rawJson = await res.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<MCAssets>(rawJson) ?? new(); | ||
} | ||
|
||
public async Task<string> GetJar(string version) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri($"java/version/{version}/jar", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode == System.Net.HttpStatusCode.OK) | ||
return await res.Content.ReadAsStringAsync(); | ||
return string.Empty; | ||
} | ||
|
||
public async Task<bool> GetOverlaySupport(string version) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri($"java/version/{version}/supports-overlays", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return true; | ||
|
||
return !bool.TryParse(await res.Content.ReadAsStringAsync(), out bool result) || result; | ||
} | ||
} | ||
} |
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.Text; | ||
using MCTools.SDK.Interfaces.Controllers; | ||
using MCTools.SDK.Models.Telemetry; | ||
using Newtonsoft.Json; | ||
|
||
namespace MCTools.SDK.Controllers | ||
{ | ||
public class TelemetryController : IController | ||
{ | ||
private IApiClient _client { get; } | ||
private string _apiVersion { get; } | ||
|
||
public TelemetryController(IApiClient client, string apiVersion = "1.0") | ||
{ | ||
_client = client; | ||
_apiVersion = apiVersion; | ||
} | ||
|
||
public async Task AddAppLaunch(AppInfo appInfo) | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Post, _client.BuildRequestUri("telemetry/launch", _apiVersion)); | ||
req.Content = new StringContent(JsonConvert.SerializeObject(appInfo), Encoding.UTF8, "application/json"); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
Console.WriteLine($"Unable to communicate with the API! Response: {await res.Content.ReadAsStringAsync()}"); | ||
} | ||
|
||
public async Task<List<ApiMessage>> GetStatusMessages() | ||
{ | ||
HttpRequestMessage req = new(HttpMethod.Get, _client.BuildRequestUri("telemetry/launch", _apiVersion)); | ||
HttpResponseMessage res = await _client.GetClient().SendAsync(req); | ||
|
||
if (res.StatusCode != System.Net.HttpStatusCode.OK) | ||
return new(); | ||
|
||
string rawJson = await res.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<List<ApiMessage>>(rawJson) ?? new(); | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace MCTools.SDK.Enums.Controllers | ||
{ | ||
public enum ApiRelease | ||
{ | ||
None = 0, | ||
Release = 1, | ||
Beta = 2 | ||
} | ||
} |
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,6 @@ | ||
namespace MCTools.SDK.Interfaces.Controllers | ||
{ | ||
public interface IController | ||
{ | ||
} | ||
} |
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,11 @@ | ||
using MCTools.SDK.Models; | ||
|
||
namespace MCTools.SDK.Interfaces.Controllers | ||
{ | ||
public interface IEditionController : IController | ||
{ | ||
Task<List<MCVersion>> GetVersions(); | ||
|
||
Task<MCAssets> GetAssets(string version); | ||
} | ||
} |
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
Oops, something went wrong.