diff --git a/4-series/epi-essentials-mobile-control/Services/MobileControlApiService.cs b/4-series/epi-essentials-mobile-control/Services/MobileControlApiService.cs index 3087b2d..00ae00b 100644 --- a/4-series/epi-essentials-mobile-control/Services/MobileControlApiService.cs +++ b/4-series/epi-essentials-mobile-control/Services/MobileControlApiService.cs @@ -23,47 +23,54 @@ public MobileControlApiService(string apiUrl) public async Task SendAuthorizationRequest(string apiUrl, string grantCode, string systemUuid) { - var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}"); + try + { + var request = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/system/{systemUuid}/authorize?grantCode={grantCode}"); - Debug.Console(1, $"Sending authorization request to {request.RequestUri}"); + Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Sending authorization request to {host}", null, request.RequestUri); - var response = await _client.SendAsync(request); + var response = await _client.SendAsync(request); - var authResponse = new AuthorizationResponse - { - Authorized = response.StatusCode == System.Net.HttpStatusCode.OK - }; + var authResponse = new AuthorizationResponse + { + Authorized = response.StatusCode == System.Net.HttpStatusCode.OK + }; - if (authResponse.Authorized) - { - return authResponse; - } + if (authResponse.Authorized) + { + return authResponse; + } - if (response.StatusCode == System.Net.HttpStatusCode.Moved) - { - var location = response.Headers.Location; + 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}\""; + authResponse.Reason = $"ERROR: Mobile Control API has moved. Please adjust configuration to \"{location}\""; - return authResponse; - } + return authResponse; + } - var responseString = await response.Content.ReadAsStringAsync(); + var responseString = await response.Content.ReadAsStringAsync(); - switch (responseString) + 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) { - 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; + Debug.LogMessage(ex, "Error authorizing with Mobile Control"); + return new AuthorizationResponse { Authorized = false, Reason = ex.Message }; } - - return authResponse; } } } diff --git a/4-series/epi-essentials-mobile-control/WebApiHandlers/MobileAuthRequestHandler.cs b/4-series/epi-essentials-mobile-control/WebApiHandlers/MobileAuthRequestHandler.cs index 2dc8407..4f2774f 100644 --- a/4-series/epi-essentials-mobile-control/WebApiHandlers/MobileAuthRequestHandler.cs +++ b/4-series/epi-essentials-mobile-control/WebApiHandlers/MobileAuthRequestHandler.cs @@ -19,28 +19,30 @@ public MobileAuthRequestHandler(MobileControlSystemController controller) : base protected override async Task HandlePost(HttpCwsContext context) { - var requestBody = EssentialsWebApiHelpers.GetRequestBody(context.Request); + try + { + var requestBody = EssentialsWebApiHelpers.GetRequestBody(context.Request); - var grantCode = JsonConvert.DeserializeObject(requestBody); + var grantCode = JsonConvert.DeserializeObject(requestBody); - if (string.IsNullOrEmpty(grantCode?.GrantCode)) - { - context.Response.StatusCode = 400; - context.Response.StatusDescription = "Missing grant code"; - context.Response.End(); - return; - } + if (string.IsNullOrEmpty(grantCode?.GrantCode)) + { + Debug.LogMessage(Serilog.Events.LogEventLevel.Error, "Missing grant code"); + context.Response.StatusCode = 400; + context.Response.StatusDescription = "Missing grant code"; + context.Response.End(); + return; + } - var response = await mcController.ApiService.SendAuthorizationRequest(mcController.Host, grantCode.GrantCode, mcController.SystemUuid); + var response = await mcController.ApiService.SendAuthorizationRequest(mcController.Host, grantCode.GrantCode, mcController.SystemUuid); + + Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, $"response received"); + if (response.Authorized) + { + mcController.RegisterSystemToServer(); + } - Debug.Console(1, $"response received"); - if (response.Authorized) - { - mcController.RegisterSystemToServer(); - } - try - { context.Response.StatusCode = 200; var responseBody = JsonConvert.SerializeObject(response, Formatting.None); context.Response.ContentType = "application/json"; @@ -50,14 +52,7 @@ protected override async Task HandlePost(HttpCwsContext context) } catch (Exception ex) { - Debug.Console(0, $"Exception handling MC Auth request: {ex.Message}"); - Debug.Console(2, $"Stack Trace: {ex.StackTrace}"); - - if (ex.InnerException != null) - { - Debug.Console(0, $"Inner Exception: {ex.InnerException.Message}"); - Debug.Console(2, $"Inner Exception Stack Trace: {ex.InnerException.StackTrace}"); - } + Debug.LogMessage(ex, "Exception recieved authorizing system"); } } }