Skip to content

Instantly share code, notes, and snippets.

@Nillth
Created March 29, 2023 09:57
Show Gist options
  • Save Nillth/4a0caf6078a2b56075755badf85d477b to your computer and use it in GitHub Desktop.
Save Nillth/4a0caf6078a2b56075755badf85d477b to your computer and use it in GitHub Desktop.
[CmdletBinding()]
param
(
[Parameter()]
[System.IO.DirectoryInfo]$SourceSchemas,
[Switch]$Testing
)
if (-not $SourceSchemas.Exists)
{
$SourceSchemas.Create()
}
[System.IO.FileInfo]$LastRun = "$($SourceSchemas.Parent.FullName)\$($SourceSchemas.Name)_LastRun.json"
$BaseURI = 'https://qlik.dev/specs/rest'
$PageData = Invoke-RestMethod 'https://qlik.dev/page-data/apis/page-data.json'
$APIClasses = $PageData.result.pageContext.specs.psobject.Properties.name
$RestAPIs = [System.Collections.Generic.List[PSCustomObject]]::new()
$GenerateAPIs = [System.Collections.Generic.List[string]]::new()
$OpenAPIFiles = [System.Collections.Generic.List[System.IO.FileInfo]]::new()
$OpenAPIFilesFullList = [System.Collections.Generic.List[System.IO.FileInfo]]::new()
foreach ($APIClass in $APIClasses)
{
$PageData.result.pageContext.specs.$($APIClass).endpoints | Where-Object { $_.type -eq 'Rest' } | ForEach-Object { $RestAPIs.Add($_) }
}
[System.Collections.Generic.List[pscustomobject]]$APIVersion = $RestAPIs.parent | Select-Object -Unique -Property relativePath, id
$Configuration = @{
RestAPIVersion = $APIVersion
}
$APIVersion.relativePath | ForEach-Object { $OpenAPIFilesFullList.add("$($SourceSchemas.FullName)\$($_)") }
if ($LastRun.Exists)
{
$LastRunInfo = Get-Content $LastRun | ConvertFrom-Json
foreach ($api in $Configuration.RestAPIVersion)
{
if (-not ($LastRunInfo.RestAPIVersion.Where({ $_.relativePath -eq $api.relativePath }).id -eq $API.id))
{
$GenerateAPIs.Add($API.relativePath)
}
}
}
else
{
$GenerateAPIs.addRange([string[]]$Configuration.RestAPIVersion.relativePath)
}
if ($GenerateAPIs.Count -eq 0 -and $Testing)
{
$selectedAPIs = [string[]]$Configuration.RestAPIVersion.relativePath | Sort-Object | Out-GridView -Title 'Select API' -OutputMode Multiple
$selectedAPIs | ForEach-Object { $GenerateAPIs.Add($_) }
}
foreach ($RestAPIName in $GenerateAPIs)
{
$RestAPI = "$($BaseURI)/$($RestAPIName)"
[System.IO.FileInfo]$OpenAPIFile = "$($SourceSchemas.FullName)\$($RestAPIName)"
$OpenAPIFiles.Add($OpenAPIFile)
Invoke-RestMethod -Method Get -Uri $RestAPI -OutFile $OpenAPIFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment