mirror of
https://github.com/AlexMacocian/MTSC.git
synced 2026-07-15 14:59:33 +00:00
@@ -20,11 +20,7 @@ namespace MTSC.UnitTests.RoutingModules
|
||||
{
|
||||
var iteration = this.iteratingService.Iteration;
|
||||
|
||||
return Task.FromResult(new HttpResponse
|
||||
{
|
||||
StatusCode = HttpMessage.StatusCodes.OK,
|
||||
BodyString = iteration.ToString()
|
||||
});
|
||||
return Task.FromResult(this.Ok200WithPayload(iteration.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace MTSC.Common.Http
|
||||
UnsupportedMediaType = 415,
|
||||
RequestRangeNotSatisfiable = 416,
|
||||
ExpectationFailed = 417,
|
||||
RequestCancelled = 499,
|
||||
InternalServerError = 500,
|
||||
NotImplemented = 501,
|
||||
BadGateway = 502,
|
||||
|
||||
@@ -14,12 +14,7 @@ namespace MTSC.Common.Http.RoutingModules
|
||||
|
||||
public override Task<HttpResponse> HandleRequest(HttpRequestContext request)
|
||||
{
|
||||
return Task.FromResult(this.OK);
|
||||
return Task.FromResult(this.Ok200);
|
||||
}
|
||||
|
||||
private HttpResponse OK => new()
|
||||
{
|
||||
StatusCode = HttpMessage.StatusCodes.OK
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using MTSC.ServerSide;
|
||||
using MTSC.ServerSide.Handlers;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MTSC.Common.Http.RoutingModules
|
||||
@@ -11,13 +11,6 @@ namespace MTSC.Common.Http.RoutingModules
|
||||
{
|
||||
private bool disposedValue;
|
||||
|
||||
private static HttpResponse InternalServerError500 { get; } =
|
||||
new HttpResponse
|
||||
{
|
||||
StatusCode = HttpMessage.StatusCodes.InternalServerError,
|
||||
BodyString = "An exception ocurred while processing the request"
|
||||
};
|
||||
|
||||
public ClientData ClientData { get; private set; }
|
||||
public HttpRoutingHandler HttpRoutingHandler { get; private set; }
|
||||
public Server Server { get; private set; }
|
||||
@@ -38,7 +31,7 @@ namespace MTSC.Common.Http.RoutingModules
|
||||
{
|
||||
if (this.HttpRoutingHandler.Return500OnUnhandledException is true)
|
||||
{
|
||||
return InternalServerError500;
|
||||
return this.InternalServerError500;
|
||||
}
|
||||
|
||||
throw;
|
||||
@@ -46,6 +39,25 @@ namespace MTSC.Common.Http.RoutingModules
|
||||
}
|
||||
public abstract Task<HttpResponse> HandleRequest(HttpRequestContext request);
|
||||
|
||||
protected HttpResponse Ok200 => CreateResponse(HttpMessage.StatusCodes.OK);
|
||||
protected HttpResponse Ok200WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.OK, payload);
|
||||
protected HttpResponse Created201 => CreateResponse(HttpMessage.StatusCodes.Created);
|
||||
protected HttpResponse Created201WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.Created, payload);
|
||||
protected HttpResponse Accepted202 => CreateResponse(HttpMessage.StatusCodes.Accepted);
|
||||
protected HttpResponse Accepted202WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.Accepted, payload);
|
||||
protected HttpResponse BadRequest400 => CreateResponse(HttpMessage.StatusCodes.BadRequest);
|
||||
protected HttpResponse BadRequest400WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.BadRequest, payload);
|
||||
protected HttpResponse Unauthorized401 => CreateResponse(HttpMessage.StatusCodes.Unauthorized);
|
||||
protected HttpResponse Unauthorized401WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.Unauthorized, payload);
|
||||
protected HttpResponse Forbidden403 => CreateResponse(HttpMessage.StatusCodes.Forbidden);
|
||||
protected HttpResponse Forbidden403WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.Forbidden, payload);
|
||||
protected HttpResponse NotFound404 => CreateResponse(HttpMessage.StatusCodes.NotFound);
|
||||
protected HttpResponse NotFound404WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.NotFound, payload);
|
||||
protected HttpResponse Gone410 => CreateResponse(HttpMessage.StatusCodes.Gone);
|
||||
protected HttpResponse Gone410WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.Gone, payload);
|
||||
protected HttpResponse InternalServerError500 => CreateResponse(HttpMessage.StatusCodes.InternalServerError);
|
||||
protected HttpResponse InternalServerError500WithPayload(object payload) => CreateResponse(HttpMessage.StatusCodes.InternalServerError, payload);
|
||||
|
||||
void ISetHttpContext.SetScopedServiceProvider(Slim.IServiceProvider serviceProvider)
|
||||
{
|
||||
this.ScopedServiceProvider = serviceProvider;
|
||||
@@ -76,11 +88,18 @@ namespace MTSC.Common.Http.RoutingModules
|
||||
this.ScopedServiceProvider = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.Dispose(disposing: true);
|
||||
}
|
||||
|
||||
private static HttpResponse CreateResponse(HttpMessage.StatusCodes statusCode, object payload = null) => new()
|
||||
{
|
||||
StatusCode = statusCode,
|
||||
BodyString = payload is null ? string.Empty :
|
||||
payload is string stringPayload ? stringPayload :
|
||||
JsonConvert.SerializeObject(payload)
|
||||
};
|
||||
}
|
||||
public abstract class HttpRouteBase<T> : HttpRouteBase
|
||||
{
|
||||
|
||||
+3
-3
@@ -5,13 +5,13 @@
|
||||
<TargetFrameworks>net48;netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<Version>5.1.2</Version>
|
||||
<Version>5.1.3</Version>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Authors>Alexandru-Victor Macocian</Authors>
|
||||
<Product>MTSC</Product>
|
||||
<Description>Modular TCP Server and Client</Description>
|
||||
<AssemblyVersion>5.1.2.0</AssemblyVersion>
|
||||
<FileVersion>5.1.2.0</FileVersion>
|
||||
<AssemblyVersion>5.1.3.0</AssemblyVersion>
|
||||
<FileVersion>5.1.3.0</FileVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<PackageProjectUrl>https://github.com/AlexMacocian/MTSC</PackageProjectUrl>
|
||||
|
||||
Reference in New Issue
Block a user