|
| 1 | +# Copyright 2026 Milestone Systems A/S |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +function InvokeVmsRestApi { |
| 16 | + <# |
| 17 | + .SYNOPSIS |
| 18 | + Invokes an authenticated request against the XProtect API Gateway REST endpoint. |
| 19 | +
|
| 20 | + .DESCRIPTION |
| 21 | + InvokeVmsRestApi is an internal helper that sends HTTP requests to Milestone's |
| 22 | + XProtect API Gateway REST API under the /rest/v1 route. It resolves the API |
| 23 | + Gateway URI from the registered services for the active site, adds a bearer |
| 24 | + authorization header from the current login token cache, and returns the |
| 25 | + deserialized response. When a dictionary body is supplied it is serialized to |
| 26 | + JSON before sending. |
| 27 | +
|
| 28 | + .PARAMETER Method |
| 29 | + Specifies the HTTP method used for the request. The default is Get. |
| 30 | +
|
| 31 | + .PARAMETER Body |
| 32 | + Specifies the payload to send with methods that accept a request body. A |
| 33 | + dictionary/hashtable is converted to JSON with depth 10 before transmission. |
| 34 | +
|
| 35 | + .PARAMETER ContentType |
| 36 | + Specifies the Content-Type header value to use when Body is present. The |
| 37 | + default is application/json. |
| 38 | +
|
| 39 | + .PARAMETER ResourcePath |
| 40 | + Specifies the relative API resource path under /rest/v1, for example |
| 41 | + 'loginproviders' or 'loginproviders/{id}'. |
| 42 | +
|
| 43 | + .PARAMETER GatewayUri |
| 44 | + Specifies the base URI of the API Gateway service. If omitted, the function |
| 45 | + discovers the gateway URI from the registered services for the active site. |
| 46 | +
|
| 47 | + .EXAMPLE |
| 48 | + InvokeVmsRestApi -ResourcePath 'loginproviders' |
| 49 | +
|
| 50 | + Retrieves the collection of login providers from the registered API Gateway |
| 51 | + for the current site. |
| 52 | +
|
| 53 | + .NOTES |
| 54 | + Requires a valid login token from (Get-LoginSettings).IdentityTokenCache. |
| 55 | + #> |
| 56 | + [CmdletBinding()] |
| 57 | + param ( |
| 58 | + [Parameter()] |
| 59 | + [string] |
| 60 | + $Method = 'Get', |
| 61 | + |
| 62 | + [Parameter()] |
| 63 | + [object] |
| 64 | + $Body, |
| 65 | + |
| 66 | + [Parameter()] |
| 67 | + [string] |
| 68 | + $ContentType = 'application/json', |
| 69 | + |
| 70 | + [Parameter(Mandatory)] |
| 71 | + [Alias('Path')] |
| 72 | + [string] |
| 73 | + $ResourcePath, |
| 74 | + |
| 75 | + [Parameter()] |
| 76 | + [uri] |
| 77 | + $GatewayUri |
| 78 | + ) |
| 79 | + |
| 80 | + begin { |
| 81 | + Assert-VmsRequirementsMet |
| 82 | + if (-not $MyInvocation.BoundParameters.ContainsKey('GatewayUri')) { |
| 83 | + $GatewayUri = (Get-RegisteredService -ServiceType 'e46b7bf9-03ce-44eb-bbdc-8ba16d0aaa80').UriArray | Select-Object -First 1 |
| 84 | + } |
| 85 | + if ($null -eq $GatewayUri) { |
| 86 | + throw "No 'API Gateway Service' registered on site $((Get-VmsSite).Name)." |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + process { |
| 91 | + $uriBuilder = [uribuilder]$GatewayUri |
| 92 | + $uriBuilder.Path = $uriBuilder.Path.TrimEnd('/') + '/rest/v1/' + $ResourcePath.TrimStart('/') |
| 93 | + $requestParams = @{ |
| 94 | + Uri = $uriBuilder.Uri |
| 95 | + Method = $Method |
| 96 | + Headers = @{ |
| 97 | + Authorization = "Bearer $((Get-LoginSettings).IdentityTokenCache.Token)" |
| 98 | + } |
| 99 | + } |
| 100 | + if ($MyInvocation.BoundParameters.ContainsKey('Body')) { |
| 101 | + $requestParams.ContentType = $ContentType |
| 102 | + if ($Body -is [System.Collections.IDictionary]) { |
| 103 | + $requestParams.Body = [pscustomobject]$Body | ConvertTo-Json -Depth 10 |
| 104 | + } else { |
| 105 | + $requestParams.Body = $Body.ToString() |
| 106 | + } |
| 107 | + } |
| 108 | + Invoke-RestMethod @requestParams |
| 109 | + } |
| 110 | +} |
0 commit comments