public/Get-OpenKVKResult.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
function Get-OpenKVKResult {
    [cmdletbinding()]
    param(
        [parameter(Mandatory=$False)]
        [ValidateCount(1,100)]
        [int]$Size = 100,
        [parameter(Mandatory=$False, ParameterSetName="ID")]
        [Alias("OpenKVKID")]
        [string]$ID,
        [parameter(Mandatory=$False, ParameterSetName="Filter")]
        [string]$Postcode,
        [parameter(Mandatory=$False, ParameterSetName="KVKID")]
        [string]$KVKID
    )

    Begin {
        $headers = @{
            'ovio-api-key' = $OverheidIO_APIKey
        }

        $APIEndpoint = "/openkvk"

        $URL = $OverheidIO_APIHost + $APIEndpoint

        if ($ID) {
            $URL = $URL + "/$ID"
        }

        if ($Postcode) {
            $URL = $URL + "?filters[postcode]=$Postcode"
        }

        if ($KVKID) {
            $QueryURL = $URL + "?query=$KVKID&queryfields[]=dossiernummer"
            $QueryResponse = Invoke-RestMethod -Method GET -Uri $QueryURL -Headers $headers
            $URL = $OverheidIO_APIHost + $QueryResponse._embedded.bedrijf._links.self.href
        }
    }

    Process {
        Write-Debug "Making a request to the following URL: $URL"
        $Response = Invoke-RestMethod -Method GET -Uri $URL -Headers $headers
    }

    End {
        return $Response
    }
}