diff --git a/src/UniversalDashboard.UITest/Integration/Api.Tests.ps1 b/src/UniversalDashboard.UITest/Integration/Api.Tests.ps1 index 1673fa02..4914402f 100644 --- a/src/UniversalDashboard.UITest/Integration/Api.Tests.ps1 +++ b/src/UniversalDashboard.UITest/Integration/Api.Tests.ps1 @@ -85,6 +85,10 @@ Describe "Api" { param($test) @("Adam", "Bill", "Frank", $test) | ConvertTo-Json } + New-UDEndpoint -Url "user" -Method "PATCH" -Endpoint { + param($test) + @("Adam", "Bill", "Frank", $test) | ConvertTo-Json + } New-UDEndpoint -Url "project" -Method "GET" -Endpoint { Set-UDContentType "application/xml" @@ -193,6 +197,14 @@ Describe "Api" { $users[3] | Should be "xyz" } + It "returns users from the patch endpoint" -Skip { + $users = Invoke-RestMethod -Uri http://localhost:10001/api/user -Method Patch -Body @{Test="xyz"} + $users[0] | Should be "Adam" + $users[1] | Should be "Bill" + $users[2] | Should be "Frank" + $users[3] | Should be "xyz" + } + It "returns users from query string" { $users = Invoke-RestMethod -Uri http://localhost:10001/api/querystring?Test=xyz -Method POST $users[0] | Should be "Adam" diff --git a/src/UniversalDashboard/Cmdlets/NewEndpointCommand.cs b/src/UniversalDashboard/Cmdlets/NewEndpointCommand.cs index b1891536..a5ac6698 100644 --- a/src/UniversalDashboard/Cmdlets/NewEndpointCommand.cs +++ b/src/UniversalDashboard/Cmdlets/NewEndpointCommand.cs @@ -28,7 +28,7 @@ public class NewEndpointCommand : PSCmdlet public SwitchParameter EvaluateUrlAsRegex { get; set; } [Parameter(ParameterSetName = "Rest")] - [ValidateSet("GET", "POST", "DELETE", "PUT")] + [ValidateSet("GET", "POST", "DELETE", "PUT", "PATCH")] public string Method { get; set; } = "GET"; [Parameter(ParameterSetName = "Rest")] diff --git a/src/UniversalDashboard/Controllers/ComponentController.cs b/src/UniversalDashboard/Controllers/ComponentController.cs index ca55bd69..f5c02318 100644 --- a/src/UniversalDashboard/Controllers/ComponentController.cs +++ b/src/UniversalDashboard/Controllers/ComponentController.cs @@ -504,7 +504,38 @@ public async Task PostEndpoint() return StatusCode(404); } - [HttpPut] + [HttpPatch] + [Route("/api/{*parts}")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + public async Task PatchEndpoint() + { + var parts = HttpContext.GetRouteValue("parts") as string; + + Log.Info($"PatchEndpoint - {parts}"); + + var variables = new Dictionary(); + SetQueryStringValues(variables); + + if (!await TryProcessBodyAsForm(Request, variables)) + { + //If we made it here we either have a non-form content type + //or the request was made with the default content type of form + //when it is really something else (probably application/json) + ProcessBodyAsRaw(Request, variables); + } + + var endpoint = _dashboardService.EndpointService.GetByUrl(parts, "PATCH", variables); + if (endpoint != null) + { + return await RunScript(endpoint, variables); + } + + Log.Info("Did not match endpoint."); + + return StatusCode(404); + } + + [HttpPut] [Route("/api/{*parts}")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public async Task PutEndpoint()