Private/Invoke-AirTableApi.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
function Invoke-AirTableApi
{
    param
    (
        [Parameter(Mandatory)]
        [string]$ApiKey,

        [Parameter(Mandatory)]
        [string]$BaseName,

        [Parameter(Mandatory)]
        [string]$TableName,

        [Parameter(Mandatory)]
        [ValidateSet('Get', 'Post', 'Patch', 'Delete')]
        [string]$Method,

        [Parameter()]
        [string]$Path,

        [Parameter()]
        [string]$QueryString,

        [Parameter()]
        [string]$PayLoad
    )

    $Uri = ((("https://api.airtable.com/v0/$BaseName/$TableName", $Path) -join '/'), $QueryString) -join '?'

    Write-Verbose "Uri: $Uri"
    Write-Verbose "Method: $Method"
    Write-Verbose "Payload: $Payload"

    Invoke-RestMethod `
        -Uri $Uri `
        -Method $Method `
        -ContentType 'application/json' `
        -Headers @{'Authorization' = "Bearer $($ApiKey)" } `
        -Body ([System.Text.Encoding]::UTF8.GetBytes($Payload))
}