Removed FromBody

This commit is contained in:
Mike Phares 2023-03-08 10:04:15 -07:00
parent db44756142
commit de048b6842
7 changed files with 155 additions and 35 deletions

View File

@ -147,7 +147,45 @@
"method": "GET", "method": "GET",
"sortNum": 0, "sortNum": 0,
"created": "2023-03-07T17:21:05.219Z", "created": "2023-03-07T17:21:05.219Z",
"modified": "2023-03-07T17:28:47.716Z", "modified": "2023-03-07T19:35:11.146Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\n \"PSN\": \"5296\",\n \"RDS\": \"578280\",\n \"Reactor\": \"37\",\n \"ID\": 1678216576\n}",
"form": []
},
"tests": []
},
{
"_id": "6d8098aa-eb50-422e-b38d-32709d985e8e",
"colId": "history",
"containerId": "",
"name": "GetHeaders-Dev",
"url": "http://mestsa008/api/export/headers",
"method": "POST",
"sortNum": 0,
"created": "2023-03-07T17:21:05.219Z",
"modified": "2023-03-08T16:39:11.451Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\n \"MesEntity\": \"CDE5\",\n \"Employee\": \"Operator\",\n \"Layer\": \"-\",\n \"PSN\": \"5008\",\n \"RDS\": \"579487\",\n \"Reactor\": \"61\",\n \"Recipe\": \"LSL8IN \\\\ 10PT_5mm\",\n \"Zone\": \"-\",\n \"Ticks\": 638137811504314166,\n \"ID\": 1678209360\n}",
"form": []
},
"tests": []
},
{
"_id": "61383ad6-ceb4-4d98-86c1-bf00c0e4204d",
"colId": "history",
"containerId": "",
"name": "GetHeaders-localhost",
"url": "http://localhost:5126/api/export/headers",
"method": "GET",
"sortNum": 0,
"created": "2023-03-07T17:21:05.219Z",
"modified": "2023-03-08T16:05:18.727Z",
"headers": [], "headers": [],
"params": [], "params": [],
"body": { "body": {

View File

@ -15,25 +15,60 @@ public class ExportController : Controller, IExportController<IActionResult>
public ExportController(IExportRepository exportRepository) => public ExportController(IExportRepository exportRepository) =>
_ExportRepository = exportRepository; _ExportRepository = exportRepository;
private static HeaderCommon GetHeaderCommon(Stream stream)
{
HeaderCommon? result;
if (!stream.CanRead)
result = new();
else
{
Task<string> task = new StreamReader(stream).ReadToEndAsync();
result = string.IsNullOrEmpty(task.Result) ? null : JsonSerializer.Deserialize<HeaderCommon>(task.Result);
result ??= new();
}
return result;
}
[HttpGet] [HttpGet]
[Route("export")] [Route("export")]
public IActionResult GetExport(HeaderCommon headerCommon) => public IActionResult GetExport() =>
Content(_ExportRepository.GetExport(headerCommon)); Content(_ExportRepository.GetExport(GetHeaderCommon(Request.Body)));
[HttpGet] [HttpGet]
[Route("headers")] [Route("headers")]
public IActionResult GetHeaders(HeaderCommon headerCommon) => public IActionResult GetHeaders() =>
Json(_ExportRepository.GetHeaders(headerCommon), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); Json(_ExportRepository.GetHeaders(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
[HttpGet] [HttpGet]
[Route("logistics")] [Route("logistics")]
public IActionResult GetLogistics(HeaderCommon headerCommon) => public IActionResult GetLogistics() =>
Json(_ExportRepository.GetLogistics(headerCommon), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); Json(_ExportRepository.GetLogistics(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
[HttpGet] [HttpGet]
[Route("pdsf")] [Route("pdsf")]
[Route("processDataStandardFormat")] [Route("processDataStandardFormat")]
public IActionResult GetProcessDataStandardFormat(HeaderCommon headerCommon) => public IActionResult GetProcessDataStandardFormat() =>
Content(_ExportRepository.GetProcessDataStandardFormat(headerCommon)); Content(_ExportRepository.GetProcessDataStandardFormat(GetHeaderCommon(Request.Body)));
[HttpPost]
[Route("export")]
public IActionResult PostExport() =>
Content(_ExportRepository.GetExport(GetHeaderCommon(Request.Body)));
[HttpPost]
[Route("headers")]
public IActionResult PostHeaders() =>
Json(_ExportRepository.GetHeaders(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
[HttpPost]
[Route("logistics")]
public IActionResult PostLogistics() =>
Json(_ExportRepository.GetLogistics(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
[HttpPost]
[Route("pdsf")]
[Route("processDataStandardFormat")]
public IActionResult PostProcessDataStandardFormat() =>
Content(_ExportRepository.GetProcessDataStandardFormat(GetHeaderCommon(Request.Body)));
} }

View File

@ -30,9 +30,24 @@ public partial class InboundController : ControllerBase, IInboundController<IAct
_MetrologyRepository = metrologyRepository; _MetrologyRepository = metrologyRepository;
} }
private static JToken GetJToken(Stream stream)
{
JToken jsonbody;
if (!stream.CanRead)
jsonbody = JToken.Parse("{}");
else
{
if (stream.CanSeek)
_ = stream.Seek(0, SeekOrigin.Begin);
string json = new StreamReader(stream).ReadToEnd();
jsonbody = JToken.Parse(json);
}
return jsonbody;
}
[HttpPost] [HttpPost]
[Route("{tooltype}")] [Route("{tooltype}")]
public IActionResult Post(string tooltype, [FromBody] JToken jsonbody) public IActionResult Post(string tooltype)
{ {
IPAddress? remoteIP = HttpContext.Connection.RemoteIpAddress; IPAddress? remoteIP = HttpContext.Connection.RemoteIpAddress;
if (!_InboundRepository.IsIPAddressAllowed(_AppSettings.InboundApiAllowedIPList, remoteIP)) if (!_InboundRepository.IsIPAddressAllowed(_AppSettings.InboundApiAllowedIPList, remoteIP))
@ -42,18 +57,7 @@ public partial class InboundController : ControllerBase, IInboundController<IAct
} }
else else
{ {
if (jsonbody is null || !jsonbody.Any()) JToken jsonbody = GetJToken(Request.Body);
{
if (!Request.Body.CanRead)
jsonbody = JToken.Parse("{}");
else
{
using Stream stream = Request.Body;
_ = stream.Seek(0, SeekOrigin.Begin);
string json = new StreamReader(stream).ReadToEnd();
jsonbody = JToken.Parse(json);
}
}
DataResponse dataResponse = _InboundRepository.Data(_MetrologyRepository, _InboundDataService, tooltype, jsonbody); DataResponse dataResponse = _InboundRepository.Data(_MetrologyRepository, _InboundDataService, tooltype, jsonbody);
if (!dataResponse.Errors.Any()) if (!dataResponse.Errors.Any())
return Ok(dataResponse); return Ok(dataResponse);

View File

@ -96,7 +96,6 @@ public class ExportRepository : IExportRepository
}; };
} }
return result; return result;
} }
Result<HeaderCommon[]> IExportRepository.GetLogistics(HeaderCommon headerCommon) Result<HeaderCommon[]> IExportRepository.GetLogistics(HeaderCommon headerCommon)

View File

@ -1,5 +1,3 @@
using OI.Metrology.Shared.DataModels;
namespace OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Shared.Models.Stateless;
public interface IExportController<T> public interface IExportController<T>
@ -14,9 +12,13 @@ public interface IExportController<T>
} }
static string GetRouteName() => nameof(IExportController<T>)[1..^10]; static string GetRouteName() => nameof(IExportController<T>)[1..^10];
T GetExport(HeaderCommon headerCommon); T GetExport();
T GetHeaders(HeaderCommon headerCommon); T GetHeaders();
T GetLogistics(HeaderCommon headerCommon); T GetLogistics();
T GetProcessDataStandardFormat(HeaderCommon headerCommon); T GetProcessDataStandardFormat();
T PostExport();
T PostHeaders();
T PostLogistics();
T PostProcessDataStandardFormat();
} }

View File

@ -1,5 +1,3 @@
using Newtonsoft.Json.Linq;
namespace OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Shared.Models.Stateless;
public interface IInboundController<T> public interface IInboundController<T>
@ -11,7 +9,7 @@ public interface IInboundController<T>
} }
static string GetRouteName() => nameof(IInboundController<T>)[1..^10]; static string GetRouteName() => nameof(IInboundController<T>)[1..^10];
T Post(string tooltype, JToken jsonbody); T Post(string tooltype);
T AttachFile(string tooltype, long headerid, string datauniqueid = ""); T AttachFile(string tooltype, long headerid, string datauniqueid = "");
} }

View File

@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless; using OI.Metrology.Shared.Models.Stateless;
using Serilog; using Serilog;
using System.Text;
namespace OI.Metrology.Tests; namespace OI.Metrology.Tests;
@ -39,6 +40,9 @@ public class UnitTestExportController
private static HeaderCommon GetHeaderCommon() => private static HeaderCommon GetHeaderCommon() =>
new() { PSN = "5008", Reactor = "61", RDS = "579487", ID = 1678209360 }; new() { PSN = "5008", Reactor = "61", RDS = "579487", ID = 1678209360 };
private static StringContent GetStringContent() =>
new(System.Text.Json.JsonSerializer.Serialize(GetHeaderCommon()), Encoding.UTF8, "application/json");
[TestMethod] [TestMethod]
public void GetExport() public void GetExport()
{ {
@ -55,12 +59,22 @@ public class UnitTestExportController
{ {
HttpClient httpClient = _WebApplicationFactory.CreateClient(); HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application"); _Logger.Information("Starting Web Application");
string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/export");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExport)}.txt"), result); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExport)}.txt"), result);
Assert.IsNotNull(result); Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed"); _Logger.Information($"{_TestContext?.TestName} completed");
} }
[TestMethod]
public async Task PostExportApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/export", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod] [TestMethod]
public void GetHeaders() public void GetHeaders()
{ {
@ -84,6 +98,16 @@ public class UnitTestExportController
_Logger.Information($"{_TestContext?.TestName} completed"); _Logger.Information($"{_TestContext?.TestName} completed");
} }
[TestMethod]
public async Task PostHeadersApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/headers", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod] [TestMethod]
public void GetLogistics() public void GetLogistics()
{ {
@ -100,13 +124,23 @@ public class UnitTestExportController
{ {
HttpClient httpClient = _WebApplicationFactory.CreateClient(); HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application"); _Logger.Information("Starting Web Application");
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/logistics");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLogistics)}.json"), json); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLogistics)}.json"), json);
Result<HeaderCommon[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json); Result<HeaderCommon[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
Assert.IsNotNull(result?.Results); Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed"); _Logger.Information($"{_TestContext?.TestName} completed");
} }
[TestMethod]
public async Task PostLogisticsApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/logistics", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod] [TestMethod]
public void GetProcessDataStandardFormat() public void GetProcessDataStandardFormat()
{ {
@ -123,10 +157,20 @@ public class UnitTestExportController
{ {
HttpClient httpClient = _WebApplicationFactory.CreateClient(); HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application"); _Logger.Information("Starting Web Application");
string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/processDataStandardFormat");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProcessDataStandardFormat)}.pdsf"), result); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProcessDataStandardFormat)}.pdsf"), result);
Assert.IsNotNull(result); Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed"); _Logger.Information($"{_TestContext?.TestName} completed");
} }
[TestMethod]
public async Task PostProcessDataStandardFormatApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/processDataStandardFormat", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
}
} }