|
| 1 | +function Invoke-JsmApi { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Internal HTTP transport for the JSM Cloud Operations API. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Single private helper that every public JsmOperations cmdlet routes through. |
| 8 | + Reads the active connection from script-scoped state (set by Connect-JsmService), |
| 9 | + builds Basic authentication headers from the SecureString token, and invokes the REST call. |
| 10 | +
|
| 11 | + Not exported; not callable by module consumers. |
| 12 | +
|
| 13 | + .PARAMETER Method |
| 14 | + HTTP method. Currently only Get and Post are used by v0.1.0 cmdlets. |
| 15 | +
|
| 16 | + .PARAMETER Path |
| 17 | + API path relative to the connection's BaseUri (e.g. '/alerts' or "/alerts/$id/acknowledge"). |
| 18 | +
|
| 19 | + .PARAMETER Body |
| 20 | + Optional hashtable. Serialized to JSON and sent as the request body. |
| 21 | +
|
| 22 | + .PARAMETER Query |
| 23 | + Optional hashtable of query-string parameters. |
| 24 | +
|
| 25 | + .EXAMPLE |
| 26 | + Invoke-JsmApi -Method Get -Path '/alerts' -Query @{ size = 5 } |
| 27 | +
|
| 28 | + Lists the first five alerts via the JSM Cloud canonical API. |
| 29 | +
|
| 30 | + .OUTPUTS |
| 31 | + System.Object |
| 32 | + Returns the deserialized response from Invoke-RestMethod. |
| 33 | +
|
| 34 | + .NOTES |
| 35 | + Throws a friendly error if no connection is active. HTTP errors propagate as-is. |
| 36 | + #> |
| 37 | + [CmdletBinding()] |
| 38 | + [OutputType([object])] |
| 39 | + param( |
| 40 | + [Parameter(Mandatory = $true)] |
| 41 | + [ValidateSet('Get', 'Post')] |
| 42 | + [string] |
| 43 | + $Method, |
| 44 | + |
| 45 | + [Parameter(Mandatory = $true)] |
| 46 | + [ValidateNotNullOrEmpty()] |
| 47 | + [string] |
| 48 | + $Path, |
| 49 | + |
| 50 | + [Parameter(Mandatory = $false)] |
| 51 | + [hashtable] |
| 52 | + $Body, |
| 53 | + |
| 54 | + [Parameter(Mandatory = $false)] |
| 55 | + [hashtable] |
| 56 | + $Query |
| 57 | + ) |
| 58 | + |
| 59 | + begin { |
| 60 | + Write-Verbose 'Starting Invoke-JsmApi' |
| 61 | + if ($null -eq $script:JsmConnection) { |
| 62 | + throw 'No active JSM connection. Run Connect-JsmService before calling other JsmOperations cmdlets.' |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + process { |
| 67 | + try { |
| 68 | + $uri = $script:JsmConnection.BaseUri + $Path |
| 69 | + |
| 70 | + if ($Query -and $Query.Count -gt 0) { |
| 71 | + $queryStringPairs = foreach ($entry in $Query.GetEnumerator()) { |
| 72 | + $encodedKey = [uri]::EscapeDataString($entry.Key) |
| 73 | + $encodedValue = [uri]::EscapeDataString([string]$entry.Value) |
| 74 | + "$encodedKey=$encodedValue" |
| 75 | + } |
| 76 | + $uri = $uri + '?' + ($queryStringPairs -join '&') |
| 77 | + } |
| 78 | + |
| 79 | + $tokenPlain = [pscredential]::new( |
| 80 | + 'jsm', |
| 81 | + $script:JsmConnection.ApiToken |
| 82 | + ).GetNetworkCredential().Password |
| 83 | + $basicAuthCredential = "$($script:JsmConnection.Email):$tokenPlain" |
| 84 | + $basicAuthToken = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($basicAuthCredential)) |
| 85 | + $headers = @{ Authorization = "Basic $basicAuthToken" } |
| 86 | + |
| 87 | + $invokeParameters = @{ |
| 88 | + Method = $Method |
| 89 | + Uri = $uri |
| 90 | + Headers = $headers |
| 91 | + ContentType = 'application/json' |
| 92 | + ErrorAction = 'Stop' |
| 93 | + } |
| 94 | + if ($Body -and $Body.Count -gt 0) { |
| 95 | + $invokeParameters.Body = ($Body | ConvertTo-Json -Depth 10 -Compress) |
| 96 | + } |
| 97 | + |
| 98 | + Write-Verbose "Calling $Method $uri" |
| 99 | + $response = Invoke-RestMethod @invokeParameters |
| 100 | + Write-Output $response |
| 101 | + } |
| 102 | + catch { |
| 103 | + throw |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + end { |
| 108 | + Write-Verbose 'Completed Invoke-JsmApi' |
| 109 | + } |
| 110 | +} |
0 commit comments