public/Invoke-PulpRestMethod.ps1

# .ExternalHelp powershell-pulp-help.xml
Function Invoke-PulpRestMethod {
  [Cmdletbinding()]
  Param(
    [Parameter(Mandatory=$false)]
    [string]$Server = (Get-PulpLocalConfig -Server).Server,

    [Parameter(Mandatory=$false)]
    [int]$Port = (Get-PulpLocalConfig -Port).Port,

    [Parameter(Mandatory=$false)]
    [string]$Protocol = (Get-PulpLocalConfig -Protocol).Protocol,

    [Parameter(Mandatory=$false)]
    [string]$AuthenticationMethod = (Get-PulpLocalConfig -AuthenticationMethod).AuthenticationMethod,

    [Parameter(Mandatory=$true)]
    [string]$Uri,

    [Parameter(Mandatory=$false)]
    [string]$Method = 'Get',

    [Parameter(Mandatory=$false)]
    [object]$Body,

    [Parameter(Mandatory=$false)]
    [string]$ContentType,

    [Parameter(Mandatory=$false)]
    [string]$InFile,
    
    [Parameter(Mandatory=$false)]
    [int32]$TimeoutSec
  )
  Begin {
    $trimmedUri = $Uri.TrimStart("/")
    $newUri = "${Protocol}://${Server}:${Port}/${trimmedUri}"
    foreach ($p in @('Server','Port','Protocol','AuthenticationMethod','Uri')) {
      $PSBoundParameters.Remove($p) | Out-Null
    }
    try {
      $outBuffer = $null
      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
        $PSBoundParameters['OutBuffer'] = 1
      }
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(
        'Invoke-RestMethod', [System.Management.Automation.CommandTypes]::Cmdlet)
      If ($AuthenticationMethod -eq 'certificate') {
        $cert = Get-PulpCertificate -Server $Server -Port $Port -Protocol $Protocol
        $scriptCmd = {& $wrappedCmd @PSBoundParameters -Uri $newUri -Certificate $cert}
      } Else {
        $authHeaders = Get-PulpBasicAuthHeaders -Server $Server -Port $Port -Protocol $Protocol
        $scriptCmd = {& $wrappedCmd @PSBoundParameters -Uri $newUri -Headers $authHeaders}
      }
      $steppablePipeline = $scriptCmd.GetSteppablePipeline()
      $steppablePipeline.Begin($PSCmdlet)
    }
    catch { throw }
  }
  Process {
    try {
      $steppablePipeline.Process($_)
    }
    catch {
      throw
    }
  }
  End {
    try {
      $steppablePipeline.End()
    }
    catch {
      throw
    }
  }
}