EDNA/Edsm/LogsApi.cs

173 lines
6.4 KiB
C#
Raw Permalink Normal View History

2022-02-24 16:16:03 +01:00
// <copyright file="LogsApi.cs" company="alterNERDtive">
// Copyright 20212022 alterNERDtive.
//
// This file is part of EDNA.
//
// EDNA is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EDNA is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
using System;
using System.Threading.Tasks;
using RestSharp;
2022-05-29 14:22:15 +02:00
namespace alterNERDtive.Edna.Edsm
2022-02-24 16:16:03 +01:00
{
/// <summary>
/// A Commander in the galaxy of Elite Dangerous, as returned from EDSMs
/// “Logs” API.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "Either this or wrong class/struct order 🤷")]
public struct ApiCmdr
{
/// <summary>
/// Gets or sets EDSMs internal message code for the request; see
/// https://www.edsm.net/en/api-logs-v1 for details.
/// </summary>
public int MsgNum { get; set; }
/// <summary>
/// Gets or sets the APIs status message; see
/// https://www.edsm.net/en/api-logs-v1 for details.
/// </summary>
2022-05-29 17:33:16 +02:00
public string Msg { get; set; }
2022-02-24 16:16:03 +01:00
/// <summary>
/// Gets or sets the commanders current system. Will be “null” if the
/// commanders flight log is hidden.
/// </summary>
2022-05-29 13:34:13 +02:00
public string? System { get; set; }
2022-02-24 16:16:03 +01:00
/// <summary>
/// Gets or sets whether the commander has first discovered their
/// current system.
/// </summary>
public bool? FirstDiscover { get; set; }
/// <summary>
/// Gets or sets the date and time of the commanders jump into the
/// current system. Will be null if the commanders flight log
/// timestamps are hidden.
/// </summary>
2022-05-29 17:33:16 +02:00
public string? Date { get; set; }
2022-02-24 16:16:03 +01:00
/// <summary>
/// Gets or sets EDSMs internal ID of the commanders current system.
/// </summary>
public ulong? SystemId { get; set; }
/// <summary>
/// Gets or sets the ID64 of the commanders current system.
/// </summary>
public ulong? SystemId64 { get; set; }
/// <summary>
/// Gets or sets the coordinates of the commanders current system. Will
/// be null if the commanders flight log is hidden.
/// </summary>
public Coordinates? Coordinates { get; set; }
/// <summary>
/// Gets or sets whether the commander is currently docked to a station.
/// </summary>
public bool? IsDocked { get; set; }
/// <summary>
/// Gets or sets the station the commander is currently docked at.
/// </summary>
public string Station { get; set; }
/// <summary>
/// Gets or sets EDSMs internal ID of the station the commander is
/// currently docked at.
/// </summary>
public int? StationId { get; set; }
/// <summary>
/// Gets or sets the date time of docking at the commanders currently
/// docked station.
/// </summary>
2022-05-29 17:33:16 +02:00
public string? DateDocked { get; set; }
2022-02-24 16:16:03 +01:00
/// <summary>
/// Gets or sets the ship ID of the commanders current ship. That is
/// the slot ID of the ship, not the “ID” chosen in livery.
/// </summary>
public int? ShipId { get; set; }
/// <summary>
/// Gets or sets the ship type of the commanders current ship.
/// </summary>
public string ShipType { get; set; }
/// <summary>
/// Gets or sets the fuel status of the commanders current ship. Seems
/// to always be “null”.
/// </summary>
public object ShipFuel { get; set; }
/// <summary>
/// Gets or sets the date and time of the commanders last recorded
/// activity. Will be “null” if the commanders flight log is hidden.
/// </summary>
2022-05-29 17:33:16 +02:00
public string? DateLastActivity { get; set; }
2022-02-24 16:16:03 +01:00
/// <summary>
/// Gets or sets the commanders EDSM profile URL. Will be “null” if the
/// commanders profile page is hidden.
/// </summary>
public string Url { get; set; }
}
2022-05-29 14:22:15 +02:00
/// <summary>
2022-05-29 17:33:16 +02:00
/// Pulls data about CMDRs from the EDSM Logs API.
2022-05-29 14:22:15 +02:00
/// </summary>
2022-02-24 16:16:03 +01:00
public class LogsApi
{
private static readonly Uri ApiUrl = new Uri("https://www.edsm.net/api-logs-v1");
private static readonly RestClient ApiClient = new RestClient(ApiUrl);
2022-05-29 14:22:15 +02:00
/// <summary>
2022-05-29 17:33:16 +02:00
/// Pulls data about a single CMDR from the EDSM Logs API.
2022-05-29 14:22:15 +02:00
/// </summary>
2022-05-29 17:33:16 +02:00
/// <param name="name">The CMDRs name.</param>
/// <param name="apiKey">The CMDRs EDSM API key.</param>
2022-05-29 14:22:15 +02:00
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
2022-02-24 16:16:03 +01:00
public static async Task<ApiCmdr> FindCmdr(string name, string? apiKey = null)
{
RestRequest request = new RestRequest("get-position")
.AddQueryParameter("commanderName", name)
2022-05-29 17:33:16 +02:00
.AddQueryParameter("showId", 1)
2022-02-24 16:16:03 +01:00
.AddQueryParameter("showCoordinates", 1);
if (apiKey != null)
{
request.AddQueryParameter("apiKey", apiKey);
}
ApiCmdr response = await ApiClient.GetAsync<ApiCmdr>(request);
2022-05-29 14:22:15 +02:00
if (response.MsgNum == 203)
{
2022-05-29 17:33:16 +02:00
throw new ArgumentException($"Cmdr not found{(apiKey == null ? string.Empty : " and/or invalid API key")}.", name);
2022-05-29 14:22:15 +02:00
}
else if (response.MsgNum == 100 && response.System == null && response.FirstDiscover == null && response.Date == null)
{
throw new AccessViolationException($"Cmdr “{name}” has not made their profile and/or flight log public.");
}
2022-02-24 16:16:03 +01:00
return response;
}
}
}