public/New-PACRestMethod.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
function New-PACRestMethod {
    [CmdletBinding(DefaultParameterSetName="Default")]
    param (
        # Function Path
        [Parameter(Mandatory,Position=0)]
        [string]
        $FunctionPath,

        # HTTP Method
        [Parameter(Mandatory,Position=1)]
        [string]
        $HttpMethod,

        # Query Params
        [Parameter(Position=2)]
        [AllowNull()]
        [RestMethodQueryParams]
        $QueryParams,

        # Form
        [Parameter(Mandatory,Position=3,ParameterSetName="Form")]
        [hashtable]
        $Form,

        # Filepath
        [Parameter(Mandatory,Position=3,ParameterSetName="File")]
        [string]
        $FilePath,

        # Body
        [Parameter(Mandatory,Position=3,ParameterSetName="Body")]
        [RestMethodBody]
        $Body
    )
    begin {}
    
    process {
        #maybe a better way to check if query params were supplied?
        #idea is to allow supply null so that positional params can still be used
        #for the form/file/body options if the call doesn't need query params
        if ($QueryParams -and $null -ne $QueryParams) {
            switch ($PSCmdlet.ParameterSetName) {
                "Form" { New-Object FormRestMethod @($FunctionPath,$HttpMethod,$QueryParams,$Form) }
                "File" { New-Object FileRestMethod @($FunctionPath,$HttpMethod,$QueryParams,$FilePath) }
                "Body" { New-Object BodyRestMethod @($FunctionPath,$HttpMethod,$QueryParams,$Body) }
                Default { New-Object RestMethod @($FunctionPath,$HttpMethod,$QueryParams) }
            }
        } else {
            switch ($PSCmdlet.ParameterSetName) {
                "Form" { New-Object FormRestMethod @($FunctionPath,$HttpMethod,$Form) }
                "File" { New-Object FileRestMethod @($FunctionPath,$HttpMethod,$FilePath) }
                "Body" { New-Object BodyRestMethod @($FunctionPath,$HttpMethod,$Body) }
                Default { New-Object RestMethod @($FunctionPath,$HttpMethod) }
            }
        }
    }
    
    end {}
}