Public/Curanet/Invoke-CuranetAPI.ps1
function Invoke-CuranetAPI { [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateSet("3370", "3850")] [string]$Account, [Parameter(Mandatory)] [string]$Uri, [Parameter()] [Microsoft.PowerShell.Commands.WebRequestMethod] $Method = 'Get', [Parameter()] $Body, [Parameter()] [switch]$Force ) try { # Get access token $token = Get-CuranetAccessToken -Account $Account -Force:$Force $params = @{ Uri = $Uri Method = $Method Headers = @{ Authorization = $token.AuthorizationHeader } ContentType = 'application/json' } if ($Body) { $params.Body = ($Body | ConvertTo-Json -Depth 10) } $response = Invoke-RestMethod @params return $response } catch { if ($_.Exception.Response.StatusCode -eq 401) { # Token expired - retry once with force if (!$Force) { return Invoke-CuranetApi @PSBoundParameters -Force } } throw } } |