Private/api/Invoke-SlackAPI.ps1
function Invoke-SlackAPI { [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0 )] [string]$Method, [Parameter(Mandatory = $true, Position = 1 )] [string]$Token, [Parameter(Mandatory = $false, Position = 2 )] [System.Collections.Hashtable]$Parameters ) begin { Write-Verbose "Cmdlet Invoke-SlackAPI - Begin" } process { Write-Verbose "Cmdlet Invoke-SlackAPI - Process" $headers = Get-RequestHeader $Token $uri = "https://slack.com/api/$Method" if ($Parameters.Keys.Count -gt 0) { $qs = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($key in $Parameters.Keys) { $value = $Parameters.$key if ($value) { $qs.Add($key, $value) } } $uriRequest = [System.UriBuilder]$uri $uriRequest.Query = $qs.ToString() $uri = $uriRequest.ToString() } $responseRaw = Invoke-WebRequest -Method Get -Uri $uri -Headers $headers $response = $responseRaw.Content | ConvertFrom-Json if ($response.ok) { $response } else { if ($response.error -eq "ratelimited") { [int]$delay = $responseRaw.Headers['Retry-After'] + 1 Start-Sleep -Seconds $delay Invoke-SlackAPI $Method $Token $Parameters }else{ Write-Error "$response.error" } } } end { Write-Verbose "Cmdlet Invoke-SlackAPI - End" } } |