Functions/Private/Invoke-GocdApi.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
#requires -Version 4
#requires -Modules WebRequest

function Invoke-GocdApi {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Path
        ,
        [Parameter()]
        [ValidateSet('Delete', 'Get', 'Post', 'Put')]
        [string]
        $Method = 'Get'
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [hashtable]
        $Headers = @{}
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Accept = 'application/json'
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Body
    )

    if ($Headers.ContainsKey('Accept')) {
        $Headers.Accept = $Accept
    
    } else {
        $Headers.Add('Accept', $Accept)
    }

    $Gocd = Get-GocdServer
    $IwrParams = @{
        Uri     = "$($Gocd.Server)$Path"
        Method  = $Method
        User    = $Gocd.User
        Token   = $Gocd.Token
        Headers = $Headers
    }
    if ($Body) {
        $IwrParams.Add('Body', $Body)
    }
    Invoke-AuthenticatedWebRequest @IwrParams
}