-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSetupController.cs
More file actions
51 lines (41 loc) · 1.59 KB
/
Copy pathTestSetupController.cs
File metadata and controls
51 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using InvvardDev.Ifttt.Hosting;
using InvvardDev.Ifttt.Toolkit;
using Microsoft.AspNetCore.Mvc;
namespace InvvardDev.Ifttt.Controllers;
[ApiController, Route(IftttConstants.TestingApiPath)]
public class TestSetupController : ControllerBase
{
private readonly ITestSetup testSetup;
private readonly ILogger<TestSetupController> logger;
public TestSetupController(ITestSetup testSetup, ILogger<TestSetupController> logger)
{
ArgumentNullException.ThrowIfNull(testSetup);
this.testSetup = testSetup;
this.logger = logger;
}
/// <summary>
/// Prepares a test setup for the IFTTT service.
/// </summary>
/// <returns>A list of test data for IFTTT to use.</returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK),
ProducesResponseType(StatusCodes.Status400BadRequest)]
[Consumes("application/x-www-form-urlencoded")]
public async Task<IActionResult> SetupTest(CancellationToken cancellationToken)
{
try
{
var processors = await testSetup.PrepareSetupListing(cancellationToken);
var samples = new SamplesPayload(processors);
samples.SkimEmptyProcessors();
var payload = new TopLevelMessageModel<SamplesPayload>(samples);
return Ok(payload);
}
catch (Exception ex)
{
logger.LogError(ex, "Error while setting up test");
var errorMessages = TopLevelErrorModel.Serialize(new[] { new ErrorMessage($"Error while setting up test: {ex.Message}") });
return BadRequest(errorMessages);
}
}
}