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
49
50
51
52
53
54
55
56
57
58
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 {
        $ReturnObject = @()
        $headers = @{
            'ovio-api-key' = $OverheidIO_APIKey
        }

        $APIEndpoint = "/openkvk"

        $URL = $OverheidIO_APIHost + $APIEndpoint

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

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

    Process {
        if ($KVKID) {
            $QueryURL = $URL + "?query=$KVKID&queryfields[]=dossiernummer"
            $QueryResponses = Invoke-RestMethod -Method GET -Uri $QueryURL -Headers $headers
            Write-Debug "Overheid.io returned $($QueryResponses.count) links"
            Foreach ($QueryResponse in $QueryResponses._embedded.bedrijf) {
                $URL = $OverheidIO_APIHost + $QueryResponse._links.self.href
                $Response = Invoke-RestMethod -Method GET -Uri $URL -Headers $headers
                Write-Debug "Making a request to the following URL: $URL"
                $ReturnObject += $Response
            }
            
        }
        else {
            $Response = Invoke-RestMethod -Method GET -Uri $URL -Headers $headers
            Write-Debug "Making a request to the following URL: $URL"
            $ReturnObject += $Response
        }
    }

    End {
        return $ReturnObject
    }
}