Added Http Responses and Codes

This commit is contained in:
Elias Stepanik 2023-05-31 20:07:21 +02:00
parent 5057c541a4
commit 66acf4c34e
4 changed files with 165 additions and 98 deletions

View File

@ -1,4 +1,5 @@
using Docker.DotNet.Models; using System.Net;
using Docker.DotNet.Models;
using Functions.Services; using Functions.Services;
using Functions.Services.Interfaces; using Functions.Services.Interfaces;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -18,7 +19,7 @@ public class FunctionController : ControllerBase
_functionManager = functionManager; _functionManager = functionManager;
} }
[HttpPost] [HttpPost("{functionName}/edit")]
public async Task<IActionResult> CreateFunction(string functionName, string imageTag) public async Task<IActionResult> CreateFunction(string functionName, string imageTag)
{ {
await _functionManager.CreateFunction(functionName, imageTag); await _functionManager.CreateFunction(functionName, imageTag);
@ -27,30 +28,96 @@ public class FunctionController : ControllerBase
[HttpPost("{functionName}")] [HttpPost("{functionName}")]
public async Task<string> RunFunctionPost(string functionName,[FromBody] string text) public async Task<IActionResult> RunFunctionPost(string functionName,[FromBody] string text)
{ {
var response = await _functionManager.RunInstance(functionName,HttpMethod.Post, text); var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Post, text);
_logger.LogInformation(functionName);
return response; 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}")] [HttpGet("{functionName}")]
public async Task<string> RunFunctionGet(string functionName) public async Task<IActionResult> RunFunctionGet(string functionName)
{ {
var response = await _functionManager.RunInstance(functionName,HttpMethod.Get); var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Get);
_logger.LogInformation(functionName);
return response; 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}")] [HttpPatch("{functionName}")]
public async Task<string> RunFunctionPatch(string functionName,[FromBody] string text) public async Task<IActionResult> RunFunctionPatch(string functionName,[FromBody] string text)
{ {
var response = await _functionManager.RunInstance(functionName,HttpMethod.Patch, text); var responseContext = await _functionManager.RunInstance(functionName,HttpMethod.Patch, text);
_logger.LogInformation(functionName);
return response; 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<IActionResult> 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<IActionResult> 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<IActionResult> DeleteFunction(string functionName) public async Task<IActionResult> DeleteFunction(string functionName)
{ {
await _functionManager.DeleteFunction(functionName); await _functionManager.DeleteFunction(functionName);

View File

@ -1,4 +1,5 @@
using System.Text; using System.Net;
using System.Text;
using Functions.Services.Interfaces; using Functions.Services.Interfaces;
namespace Functions.Services; namespace Functions.Services;
@ -14,7 +15,7 @@ public class ExternalEndpointManager : IExternalEndpointManager
_httpClient = httpClient; _httpClient = httpClient;
} }
public async Task<string> Get(string hostname) public async Task<HttpResponseMessage> Get(string hostname)
{ {
try try
{ {
@ -24,22 +25,17 @@ public class ExternalEndpointManager : IExternalEndpointManager
// Ensure the response was successful // Ensure the response was successful
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Display the response content // Display the response content
return responseBody; return response;
} }
catch (HttpRequestException ex) catch (HttpRequestException ex)
{ {
// Handle any errors that occurred during the request // Handle any errors that occurred during the request
return "error"; return new HttpResponseMessage(HttpStatusCode.BadRequest);
} }
return "error";
} }
public async Task<string> Post(string hostname, string body) public async Task<HttpResponseMessage> Post(string hostname, string body)
{ {
try try
{ {
@ -50,22 +46,17 @@ public class ExternalEndpointManager : IExternalEndpointManager
// Ensure the response was successful // Ensure the response was successful
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Display the response content // Display the response content
return responseBody; return response;
} }
catch (HttpRequestException ex) catch (HttpRequestException ex)
{ {
// Handle any errors that occurred during the request // Handle any errors that occurred during the request
return "error"; return new HttpResponseMessage(HttpStatusCode.BadRequest);
} }
return "error";
} }
public async Task<string> Delete(string hostname) public async Task<HttpResponseMessage> Delete(string hostname)
{ {
try try
{ {
@ -75,22 +66,17 @@ public class ExternalEndpointManager : IExternalEndpointManager
// Ensure the response was successful // Ensure the response was successful
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Display the response content // Display the response content
return responseBody; return response;
} }
catch (HttpRequestException ex) catch (HttpRequestException ex)
{ {
// Handle any errors that occurred during the request // Handle any errors that occurred during the request
return "error"; return new HttpResponseMessage(HttpStatusCode.BadRequest);
} }
return "error";
} }
public async Task<string> Patch(string hostname, string body) public async Task<HttpResponseMessage> Patch(string hostname, string body)
{ {
try try
{ {
@ -101,18 +87,34 @@ public class ExternalEndpointManager : IExternalEndpointManager
// Ensure the response was successful // Ensure the response was successful
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Display the response content // Display the response content
return responseBody; return response;
} }
catch (HttpRequestException ex) catch (HttpRequestException ex)
{ {
// Handle any errors that occurred during the request // Handle any errors that occurred during the request
return "error"; return new HttpResponseMessage(HttpStatusCode.BadRequest);
} }
}
return "error"; public async Task<HttpResponseMessage> 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);
}
} }
} }

View File

@ -1,4 +1,5 @@
using Functions.Data; using System.Net;
using Functions.Data;
using Functions.Data.DB; using Functions.Data.DB;
using Functions.Services.Interfaces; using Functions.Services.Interfaces;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -40,7 +41,7 @@ public class FunctionManager
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }
public async Task<string> RunInstance(string functionName, HttpMethod method, string body = "") public async Task<HttpResponseMessage> RunInstance(string functionName, HttpMethod method, string body = "")
{ {
var db = await _dbContextFactory.CreateDbContextAsync(); var db = await _dbContextFactory.CreateDbContextAsync();
var function = db.Functions.Include(s => s.Instances).Include(s => s.EnvironmentVariables).First(s => s.Name.Equals(functionName)); 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)) if (method.Equals(HttpMethod.Post))
{ {
var message = await _externalEndpointManager.Post(instance.Name, body); var message = await _externalEndpointManager.Post(instance.Name, body);
if (message.Equals("error")) return await HandleError(message, instance);
{
_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;
} }
if (method.Equals(HttpMethod.Get)) if (method.Equals(HttpMethod.Get))
{ {
var message = await _externalEndpointManager.Get(instance.Name); var message = await _externalEndpointManager.Get(instance.Name);
if (message.Equals("error")) return await HandleError(message, instance);
{
_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;
} }
if (method.Equals(HttpMethod.Patch)) if (method.Equals(HttpMethod.Patch))
{ {
var message = await _externalEndpointManager.Patch(instance.Name, body); var message = await _externalEndpointManager.Patch(instance.Name, body);
if (message.Equals("error")) return await HandleError(message, instance);
{
_dockerManager.DeleteContainer(instance.InstanceId);
var i = db.Instances.First(s => s.InstanceId.Equals(instance.InstanceId)); }
db.Instances.Remove(i); if (method.Equals(HttpMethod.Put))
await db.SaveChangesAsync(); {
return "Failed to send Message to Container"; 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<HttpResponseMessage> 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;
}
} }

View File

@ -4,8 +4,9 @@ namespace Functions.Services.Interfaces;
public interface IExternalEndpointManager public interface IExternalEndpointManager
{ {
public Task<string> Get(string hostname); public Task<HttpResponseMessage> Get(string hostname);
public Task<string> Post(string hostname, string body); public Task<HttpResponseMessage> Post(string hostname, string body);
public Task<string> Delete(string hostname); public Task<HttpResponseMessage> Delete(string hostname);
public Task<string> Patch(string hostname, string body); public Task<HttpResponseMessage> Patch(string hostname, string body);
public Task<HttpResponseMessage> Put(string hostname, string body);
} }