using System; using System.Net.Http; using System.Threading.Tasks; using PepperDash.Core; namespace PepperDash.Essentials.Services { /// /// Service for interacting with a Mobile Control Edge server instance /// public class MobileControlApiService { private readonly HttpClient _client; /// /// Create an instance of the class. /// /// Mobile Control Edge API URL public MobileControlApiService(string apiUrl) { var handler = new HttpClientHandler { AllowAutoRedirect = false, ServerCertificateCustomValidationCallback = (req, cert, certChain, errors) => true }; _client = new HttpClient(handler); } /// /// Send authorization request to Mobile Control Edge Server /// /// Mobile Control Edge API URL /// Grant code for authorization /// System UUID for authorization /// Authorization response public async Task SendAuthorizationRequest(string apiUrl, string grantCode, string systemUuid) { try { var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}"); Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Sending authorization request to {host}", null, request.RequestUri); var response = await _client.SendAsync(request); var authResponse = new AuthorizationResponse { Authorized = response.StatusCode == System.Net.HttpStatusCode.OK }; if (authResponse.Authorized) { return authResponse; } if (response.StatusCode == System.Net.HttpStatusCode.Moved) { var location = response.Headers.Location; authResponse.Reason = $"ERROR: Mobile Control API has moved. Please adjust configuration to \"{location}\""; return authResponse; } var responseString = await response.Content.ReadAsStringAsync(); switch (responseString) { case "codeNotFound": authResponse.Reason = $"Authorization failed. Code not found for system UUID {systemUuid}"; break; case "uuidNotFound": authResponse.Reason = $"Authorization failed. System UUID {systemUuid} not found. Check Essentials configuration."; break; default: authResponse.Reason = $"Authorization failed. Response {response.StatusCode}: {responseString}"; break; } return authResponse; } catch (Exception ex) { Debug.LogMessage(ex, "Error authorizing with Mobile Control"); return new AuthorizationResponse { Authorized = false, Reason = ex.Message }; } } } }