EDTS: now using RestSharp like a good boi.
This commit is contained in:
parent
50c5098b66
commit
1baa1136ac
2 changed files with 51 additions and 28 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
||||||
|
<PackageReference Include="RestSharp" Version="107.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -19,9 +19,10 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using RestSharp;
|
||||||
|
|
||||||
namespace alterNERDtive.Edna.Edts
|
namespace alterNERDtive.Edna.Edts
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -98,18 +99,8 @@ namespace alterNERDtive.Edna.Edts
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class EdtsApi
|
public class EdtsApi
|
||||||
{
|
{
|
||||||
private static readonly string ApiUrl = "http://edts.thargoid.space/api/v1/";
|
private static readonly string ApiUrl = "http://edts.thargoid.space/api/v1";
|
||||||
private static readonly HttpClient ApiClient;
|
private static readonly RestClient ApiClient = new RestClient(ApiUrl);
|
||||||
|
|
||||||
static EdtsApi()
|
|
||||||
{
|
|
||||||
ApiClient = new HttpClient
|
|
||||||
{
|
|
||||||
BaseAddress = new Uri(ApiUrl),
|
|
||||||
};
|
|
||||||
ApiClient.DefaultRequestHeaders.Accept.Clear();
|
|
||||||
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pulls system data for a given procedurally generated system name.
|
/// Pulls system data for a given procedurally generated system name.
|
||||||
|
@ -118,25 +109,56 @@ namespace alterNERDtive.Edna.Edts
|
||||||
/// <returns>The system with calculated coordinates.</returns>
|
/// <returns>The system with calculated coordinates.</returns>
|
||||||
public static async Task<StarSystem> FindSystem(string name)
|
public static async Task<StarSystem> FindSystem(string name)
|
||||||
{
|
{
|
||||||
HttpResponseMessage response = await ApiClient.GetAsync($"system_position/{name}");
|
try
|
||||||
|
|
||||||
// EDTS API gives a 400 status code (and an empty result) if the
|
|
||||||
// system name is not valid.
|
|
||||||
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
|
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"“{name}” is not a valid proc gen system name.", paramName: "name");
|
ApiResult response = await ApiClient.GetAsync<ApiResult>(new RestRequest($"system_position/{name}"));
|
||||||
|
|
||||||
|
ApiResult.ApiSystem result = response.Result!.Value;
|
||||||
|
|
||||||
|
return new StarSystem(
|
||||||
|
name: name,
|
||||||
|
coordinates: new Location(
|
||||||
|
x: (int)result.Position.X,
|
||||||
|
y: (int)result.Position.Y,
|
||||||
|
z: (int)result.Position.Z,
|
||||||
|
precision: (int)result.Uncertainty));
|
||||||
}
|
}
|
||||||
|
catch (HttpRequestException e)
|
||||||
|
{
|
||||||
|
// EDTS API gives a 400 status code (and an empty result) if the
|
||||||
|
// system name is not valid.
|
||||||
|
if (e.Message.Equals("Request failed with status code BadRequest"))
|
||||||
|
{
|
||||||
|
throw new ArgumentException(message: $"“{name}” is not a valid proc gen system name.", paramName: nameof(name));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
private struct ApiResult
|
||||||
dynamic json = response.Content.ReadAsAsync<dynamic>().Result["result"];
|
{
|
||||||
|
public ApiSystem? Result { get; set; }
|
||||||
|
|
||||||
return new StarSystem(
|
public struct ApiSystem
|
||||||
name: name,
|
{
|
||||||
coordinates: new Location(
|
public string Name { get; set; }
|
||||||
x: (int)json["position"]["x"],
|
|
||||||
y: (int)json["position"]["y"],
|
public ApiLocation Position { get; set; }
|
||||||
z: (int)json["position"]["z"],
|
|
||||||
precision: (int)json["uncertainty"]));
|
public decimal Uncertainty { get; set; }
|
||||||
|
|
||||||
|
public struct ApiLocation
|
||||||
|
{
|
||||||
|
public decimal X { get; set; }
|
||||||
|
|
||||||
|
public decimal Y { get; set; }
|
||||||
|
|
||||||
|
public decimal Z { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue