NetboxPS/Functions/Helpers/InvokeNetboxRequest.ps1

107 lines
3.1 KiB
PowerShell
Raw Normal View History

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