InvokeNetboxRequest(Helpers): Fix indent (using Visual Code Formatter)

This commit is contained in:
Alexis La Goutte 2021-07-21 22:22:59 +02:00
parent 4ad0562466
commit 2bf4ed6f6f

View file

@ -1,4 +1,4 @@
<# <#
.NOTES .NOTES
=========================================================================== ===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.172 Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.172
@ -18,44 +18,44 @@ function InvokeNetboxRequest {
( (
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[System.UriBuilder]$URI, [System.UriBuilder]$URI,
[Hashtable]$Headers = @{ [Hashtable]$Headers = @{
}, },
[pscustomobject]$Body = $null, [pscustomobject]$Body = $null,
[ValidateRange(0, 60)] [ValidateRange(0, 60)]
[uint16]$Timeout = 5, [uint16]$Timeout = 5,
[ValidateSet('GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', IgnoreCase = $true)] [ValidateSet('GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', IgnoreCase = $true)]
[string]$Method = 'GET', [string]$Method = 'GET',
[switch]$Raw [switch]$Raw
) )
$creds = Get-NetboxCredential $creds = Get-NetboxCredential
$Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password $Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password
$splat = @{ $splat = @{
'Method' = $Method 'Method' = $Method
'Uri' = $URI.Uri.AbsoluteUri # This property auto generates the scheme, hostname, path, and query 'Uri' = $URI.Uri.AbsoluteUri # This property auto generates the scheme, hostname, path, and query
'Headers' = $Headers 'Headers' = $Headers
'TimeoutSec' = $Timeout 'TimeoutSec' = $Timeout
'ContentType' = 'application/json' 'ContentType' = 'application/json'
'ErrorAction' = 'Stop' 'ErrorAction' = 'Stop'
'Verbose' = $VerbosePreference 'Verbose' = $VerbosePreference
} }
if ($Body) { if ($Body) {
Write-Verbose "BODY: $($Body | ConvertTo-Json -Compress)" Write-Verbose "BODY: $($Body | ConvertTo-Json -Compress)"
$null = $splat.Add('Body', ($Body | ConvertTo-Json -Compress)) $null = $splat.Add('Body', ($Body | ConvertTo-Json -Compress))
} }
$result = Invoke-RestMethod @splat $result = Invoke-RestMethod @splat
#region TODO: Handle errors a little more gracefully... #region TODO: Handle errors a little more gracefully...
<# <#
try { try {
Write-Verbose "Sending request..." Write-Verbose "Sending request..."
@ -69,7 +69,7 @@ function InvokeNetboxRequest {
Write-Verbose "RAW provided...throwing raw exception" Write-Verbose "RAW provided...throwing raw exception"
throw $_ throw $_
} }
Write-Verbose "Converting response to object" Write-Verbose "Converting response to object"
$myError = GetNetboxAPIErrorBody -Response $_.Exception.Response | ConvertFrom-Json $myError = GetNetboxAPIErrorBody -Response $_.Exception.Response | ConvertFrom-Json
} else { } else {
@ -77,27 +77,29 @@ function InvokeNetboxRequest {
$myError = $_ $myError = $_
} }
} }
Write-Verbose "MyError is $($myError.GetType().FullName)" Write-Verbose "MyError is $($myError.GetType().FullName)"
if ($myError -is [Exception]) { if ($myError -is [Exception]) {
throw $_ throw $_
} elseif ($myError -is [pscustomobject]) { } elseif ($myError -is [pscustomobject]) {
throw $myError.detail throw $myError.detail
} }
#> #>
#endregion TODO: Handle errors a little more gracefully... #endregion TODO: Handle errors a little more gracefully...
# If the user wants the raw value from the API... otherwise return only the actual result # If the user wants the raw value from the API... otherwise return only the actual result
if ($Raw) { if ($Raw) {
Write-Verbose "Returning raw result by choice" Write-Verbose "Returning raw result by choice"
return $result return $result
} else { }
else {
if ($result.psobject.Properties.Name.Contains('results')) { if ($result.psobject.Properties.Name.Contains('results')) {
Write-Verbose "Found Results property on data, returning results directly" Write-Verbose "Found Results property on data, returning results directly"
return $result.Results return $result.Results
} else { }
else {
Write-Verbose "Did NOT find results property on data, returning raw result" Write-Verbose "Did NOT find results property on data, returning raw result"
return $result return $result
} }