Private/Get-PASResponse.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function Get-PASResponse {
    <#
    .SYNOPSIS
    Receives and returns the content of the web response from the CyberArk API
 
    .DESCRIPTION
    Accepts a WebResponseObject.
    By default returns the Content property passed in the output of Invoke-PASRestMethod.
    Processes the API response as required depending on the format of the response, and
    the format required by the functions which initiated the request.
 
    .PARAMETER APIResponse
    A WebResponseObject, as returned from the PAS API using Invoke-WebRequest
 
    .EXAMPLE
    $WebResponseObject | Get-PASResponse
 
    Parses, if required, and returns, the required properties of $WebResponseObject
 
    #>

    [CmdletBinding()]
    [OutputType('System.Object')]
    param(
        [parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [Microsoft.PowerShell.Commands.WebResponseObject]$APIResponse

    )

    BEGIN {    }#begin

    PROCESS {

        if ($APIResponse.Content) {

            #Default Response - Return Content
            $PASResponse = $APIResponse.Content

            #get response content type
            $ContentType = $APIResponse.Headers["Content-Type"]

            #handle content type
            switch ($ContentType) {

                'text/html; charset=utf-8' {

                    If ($PASResponse -match '<HTML>') {

                        #Fail if HTML received from API

                        $PSCmdlet.ThrowTerminatingError(

                            [System.Management.Automation.ErrorRecord]::new(

                                "Guru Meditation - HTML Response Received",
                                $StatusCode,
                                [System.Management.Automation.ErrorCategory]::NotSpecified,
                                $APIResponse

                            )

                        )

                    }

                }

                'application/json; charset=utf-8' {

                    #application/json content expected for most responses.

                    #Create Return Object from Returned JSON
                    $PASResponse = ConvertFrom-Json -InputObject $APIResponse.Content

                }

                default {

                    # Byte Array expected for files to be saved
                    if ($($PASResponse | Get-Member | Select-Object -ExpandProperty typename) -eq "System.Byte" ) {

                        #return content and headers
                        $PASResponse = $APIResponse | Select-Object Content, Headers

                        #! to be passed to `Out-PASFile`

                    }

                }

            }

            #Return PASResponse
            $PASResponse

        }

    }#process

    END {    }#end

}