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

function Invoke-ArtifactoryApi {
    [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'
    )

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

    $Artifactory = Get-Artifactory

    $AuthString = ConvertTo-Base64 -Data "$($Artifactory.User):$($Artifactory.Token)" -Encoding ASCII
    $IwrParams = @{
        Uri     = "$($Artifactory.Server)$Path"
        Method  = 'Get'
        Headers = @{
            Authorization = "basic $AuthString"
            Accept        = 'application/json'
        }
    }

    $bytes = Invoke-WebRequest -UseBasicParsing @IwrParams | Select-Object -ExpandProperty Content
    $response = [System.Text.Encoding]::ASCII.GetString($bytes) | ConvertFrom-Json
    $response
}