From 66acf4c34e0326b0eb18e63c4e9aa3aee5fc8c01 Mon Sep 17 00:00:00 2001 From: Elias Stepanik <40958815+eliasstepanik@users.noreply.github.com> Date: Wed, 31 May 2023 20:07:21 +0200 Subject: [PATCH] Added Http Responses and Codes --- Functions/API/FunctionController.cs | 97 ++++++++++++++++--- Functions/Services/ExternalEndpointManager.cs | 66 +++++++------ Functions/Services/FunctionManager.cs | 91 +++++++++-------- .../Interfaces/IExternalEndpointManager.cs | 9 +- 4 files changed, 165 insertions(+), 98 deletions(-) diff --git a/Functions/API/FunctionController.cs b/Functions/API/FunctionController.cs index 69abbb1..f84dcaa 100644 --- a/Functions/API/FunctionController.cs +++ b/Functions/API/FunctionController.cs @@ -1,4 +1,5 @@ -using Docker.DotNet.Models; +using System.Net; +using Docker.DotNet.Models; using Functions.Services; using Functions.Services.Interfaces; using Microsoft.AspNetCore.Mvc; @@ -18,7 +19,7 @@ public class FunctionController : ControllerBase _functionManager = functionManager; } - [HttpPost] + [HttpPost("{functionName}/edit")] public async Task CreateFunction(string functionName, string imageTag) { await _functionManager.CreateFunction(functionName, imageTag); @@ -27,30 +28,96 @@ public class FunctionController : ControllerBase [HttpPost("{functionName}")] - public async Task RunFunctionPost(string functionName,[FromBody] string text) + public async Task RunFunctionPost(string functionName,[FromBody] string text) { - var response = await _functionManager.RunInstance(functionName,HttpMethod.Post, text); - _logger.LogInformation(functionName); - return response; + var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Post, text); + + if (responseContext.IsSuccessStatusCode) + { + //_logger.LogInformation(""); TODO: Write Log Message + return Ok(await responseContext.Content.ReadAsStringAsync()); + } + if(responseContext.StatusCode == HttpStatusCode.BadRequest) + { + return BadRequest(responseContext.ReasonPhrase); + } + + return NoContent(); } [HttpGet("{functionName}")] - public async Task RunFunctionGet(string functionName) + public async Task RunFunctionGet(string functionName) { - var response = await _functionManager.RunInstance(functionName,HttpMethod.Get); - _logger.LogInformation(functionName); - return response; + var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Get); + + if (responseContext.IsSuccessStatusCode) + { + //_logger.LogInformation(""); TODO: Write Log Message + return Ok(await responseContext.Content.ReadAsStringAsync()); + } + if(responseContext.StatusCode == HttpStatusCode.BadRequest) + { + return BadRequest(responseContext.ReasonPhrase); + } + + return NoContent(); } [HttpPatch("{functionName}")] - public async Task RunFunctionPatch(string functionName,[FromBody] string text) + public async Task RunFunctionPatch(string functionName,[FromBody] string text) { - var response = await _functionManager.RunInstance(functionName,HttpMethod.Patch, text); - _logger.LogInformation(functionName); - return response; + var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Patch, text); + + if (responseContext.IsSuccessStatusCode) + { + //_logger.LogInformation(""); TODO: Write Log Message + return Ok(await responseContext.Content.ReadAsStringAsync()); + } + if(responseContext.StatusCode == HttpStatusCode.BadRequest) + { + return BadRequest(responseContext.ReasonPhrase); + } + + return NoContent(); + } + + [HttpPut("{functionName}")] + public async Task RunFunctionPut(string functionName,[FromBody] string text) + { + var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Put, text); + + if (responseContext.IsSuccessStatusCode) + { + //_logger.LogInformation(""); TODO: Write Log Message + return Ok(await responseContext.Content.ReadAsStringAsync()); + } + if(responseContext.StatusCode == HttpStatusCode.BadRequest) + { + return BadRequest(responseContext.ReasonPhrase); + } + + return NoContent(); + } + + [HttpDelete("{functionName}")] + public async Task RunFunctionDelete(string functionName,[FromBody] string text) + { + var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Delete, text); + + if (responseContext.IsSuccessStatusCode) + { + //_logger.LogInformation(""); TODO: Write Log Message + return Ok(await responseContext.Content.ReadAsStringAsync()); + } + if(responseContext.StatusCode == HttpStatusCode.BadRequest) + { + return BadRequest(responseContext.ReasonPhrase); + } + + return NoContent(); } - [HttpDelete("{functionName}/delete")] + [HttpDelete("{functionName}/edit")] public async Task DeleteFunction(string functionName) { await _functionManager.DeleteFunction(functionName); diff --git a/Functions/Services/ExternalEndpointManager.cs b/Functions/Services/ExternalEndpointManager.cs index 3d67f8a..6e36b46 100644 --- a/Functions/Services/ExternalEndpointManager.cs +++ b/Functions/Services/ExternalEndpointManager.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Net; +using System.Text; using Functions.Services.Interfaces; namespace Functions.Services; @@ -14,7 +15,7 @@ public class ExternalEndpointManager : IExternalEndpointManager _httpClient = httpClient; } - public async Task Get(string hostname) + public async Task Get(string hostname) { try { @@ -24,22 +25,17 @@ public class ExternalEndpointManager : IExternalEndpointManager // Ensure the response was successful response.EnsureSuccessStatusCode(); - // Read the response content as a string - string responseBody = await response.Content.ReadAsStringAsync(); - // Display the response content - return responseBody; + return response; } catch (HttpRequestException ex) { // Handle any errors that occurred during the request - return "error"; + return new HttpResponseMessage(HttpStatusCode.BadRequest); } - - return "error"; } - public async Task Post(string hostname, string body) + public async Task Post(string hostname, string body) { try { @@ -50,22 +46,17 @@ public class ExternalEndpointManager : IExternalEndpointManager // Ensure the response was successful response.EnsureSuccessStatusCode(); - // Read the response content as a string - string responseBody = await response.Content.ReadAsStringAsync(); - // Display the response content - return responseBody; + return response; } catch (HttpRequestException ex) { // Handle any errors that occurred during the request - return "error"; + return new HttpResponseMessage(HttpStatusCode.BadRequest); } - - return "error"; } - public async Task Delete(string hostname) + public async Task Delete(string hostname) { try { @@ -75,22 +66,17 @@ public class ExternalEndpointManager : IExternalEndpointManager // Ensure the response was successful response.EnsureSuccessStatusCode(); - // Read the response content as a string - string responseBody = await response.Content.ReadAsStringAsync(); - // Display the response content - return responseBody; + return response; } catch (HttpRequestException ex) { // Handle any errors that occurred during the request - return "error"; + return new HttpResponseMessage(HttpStatusCode.BadRequest); } - - return "error"; } - public async Task Patch(string hostname, string body) + public async Task Patch(string hostname, string body) { try { @@ -101,18 +87,34 @@ public class ExternalEndpointManager : IExternalEndpointManager // Ensure the response was successful response.EnsureSuccessStatusCode(); - // Read the response content as a string - string responseBody = await response.Content.ReadAsStringAsync(); - // Display the response content - return responseBody; + return response; } catch (HttpRequestException ex) { // Handle any errors that occurred during the request - return "error"; + return new HttpResponseMessage(HttpStatusCode.BadRequest); } + } - return "error"; + public async Task Put(string hostname, string body) + { + try + { + var content = new StringContent(body, Encoding.UTF8, "application/json"); + // Send GET request to the API + HttpResponseMessage response = await _httpClient.PutAsync($"http://{hostname}",content); + + // Ensure the response was successful + response.EnsureSuccessStatusCode(); + + // Display the response content + return response; + } + catch (HttpRequestException ex) + { + // Handle any errors that occurred during the request + return new HttpResponseMessage(HttpStatusCode.BadRequest); + } } } \ No newline at end of file diff --git a/Functions/Services/FunctionManager.cs b/Functions/Services/FunctionManager.cs index ced9401..fbb295a 100644 --- a/Functions/Services/FunctionManager.cs +++ b/Functions/Services/FunctionManager.cs @@ -1,4 +1,5 @@ -using Functions.Data; +using System.Net; +using Functions.Data; using Functions.Data.DB; using Functions.Services.Interfaces; using Microsoft.EntityFrameworkCore; @@ -40,7 +41,7 @@ public class FunctionManager await db.SaveChangesAsync(); } - public async Task RunInstance(string functionName, HttpMethod method, string body = "") + public async Task RunInstance(string functionName, HttpMethod method, string body = "") { var db = await _dbContextFactory.CreateDbContextAsync(); var function = db.Functions.Include(s => s.Instances).Include(s => s.EnvironmentVariables).First(s => s.Name.Equals(functionName)); @@ -60,62 +61,58 @@ public class FunctionManager if (method.Equals(HttpMethod.Post)) { var message = await _externalEndpointManager.Post(instance.Name, body); - if (message.Equals("error")) - { - _dockerManager.DeleteContainer(instance.InstanceId); - var i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(i); - await db.SaveChangesAsync(); - return "Failed to send Message to Container"; - } - - _dockerManager.DeleteContainer(instance.InstanceId); - var temp_i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(temp_i); - await db.SaveChangesAsync(); - return message; + return await HandleError(message, instance); } if (method.Equals(HttpMethod.Get)) { var message = await _externalEndpointManager.Get(instance.Name); - if (message.Equals("error")) - { - _dockerManager.DeleteContainer(instance.InstanceId); - var i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(i); - await db.SaveChangesAsync(); - return "Failed to send Message to Container"; - } - - _dockerManager.DeleteContainer(instance.InstanceId); - var temp_i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(temp_i); - await db.SaveChangesAsync(); - return message; + return await HandleError(message, instance); } if (method.Equals(HttpMethod.Patch)) { var message = await _externalEndpointManager.Patch(instance.Name, body); - if (message.Equals("error")) - { - _dockerManager.DeleteContainer(instance.InstanceId); - var i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(i); - await db.SaveChangesAsync(); - return "Failed to send Message to Container"; - } + return await HandleError(message, instance); + + + } + if (method.Equals(HttpMethod.Put)) + { + var message = await _externalEndpointManager.Put(instance.Name, body); + return await HandleError(message, instance); + + + } + if (method.Equals(HttpMethod.Delete)) + { + var message = await _externalEndpointManager.Delete(instance.Name); + return await HandleError(message, instance); - _dockerManager.DeleteContainer(instance.InstanceId); - var temp_i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); - db.Instances.Remove(temp_i); - await db.SaveChangesAsync(); - return message; - } - return "Failed to send Message to Container"; + return new HttpResponseMessage(HttpStatusCode.BadRequest); } - - + + + private async Task HandleError(HttpResponseMessage message, Instance instance) + { + var db = await _dbContextFactory.CreateDbContextAsync(); + if (!message.IsSuccessStatusCode) + { + _dockerManager.DeleteContainer(instance.InstanceId); + var i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); + db.Instances.Remove(i); + await db.SaveChangesAsync(); + return new HttpResponseMessage(HttpStatusCode.BadRequest); + } + + _dockerManager.DeleteContainer(instance.InstanceId); + var temp_i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); + db.Instances.Remove(temp_i); + await db.SaveChangesAsync(); + await db.DisposeAsync(); + return message; + } + + } \ No newline at end of file diff --git a/Functions/Services/Interfaces/IExternalEndpointManager.cs b/Functions/Services/Interfaces/IExternalEndpointManager.cs index 32aa04b..584cf95 100644 --- a/Functions/Services/Interfaces/IExternalEndpointManager.cs +++ b/Functions/Services/Interfaces/IExternalEndpointManager.cs @@ -4,8 +4,9 @@ namespace Functions.Services.Interfaces; public interface IExternalEndpointManager { - public Task Get(string hostname); - public Task Post(string hostname, string body); - public Task Delete(string hostname); - public Task Patch(string hostname, string body); + public Task Get(string hostname); + public Task Post(string hostname, string body); + public Task Delete(string hostname); + public Task Patch(string hostname, string body); + public Task Put(string hostname, string body); } \ No newline at end of file