-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathDownloadOpenApiDoc.ps1
More file actions
71 lines (68 loc) · 2.68 KB
/
DownloadOpenApiDoc.ps1
File metadata and controls
71 lines (68 loc) · 2.68 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
[CmdletBinding()]
Param(
[string] $ModuleName,
[string] $ModuleRegex,
[string] $OpenApiDocOutput,
[string] $GraphVersion,
[switch] $ForceRefresh,
[switch] $SingularizeOperationIds,
[int] $RequestCount = 1
)
if (-not (Test-Path $OpenApiDocOutput)) {
New-Item -Path $OpenApiDocOutput -Type Directory
}
$OpenApiBaseUrl = "https://devxapi-func-prod-eastus.azurewebsites.net"
$OpenApiServiceUrl = ("$OpenApiBaseUrl/`$openapi?tags={0}&title=$ModuleName&openapiversion=3&style=Powershell&fileName=powershell_v2&graphVersion=$GraphVersion" -f $ModuleRegex)
if ($ForceRefresh.IsPresent) {
$OpenApiServiceUrl = "$OpenApiServiceUrl&forceRefresh=true"
}
if ($SingularizeOperationIds.IsPresent) {
$OpenApiServiceUrl = "$OpenApiServiceUrl&singularizeOperationIds=true"
}
Write-Debug "[$RequestCount] Downloading OpenAPI doc for '$ModuleName' module: $OpenApiServiceUrl"
$Retries = 3
$Delay = 3
$Retrycount = 0
$Completed = $false
while (-not $Completed) {
try {
Invoke-WebRequest $OpenApiServiceUrl -OutFile "$OpenApiDocOutput\$ModuleName.yml"
$Completed = $true
}
catch {
$Exception = $_.Exception
switch ($Exception.Response.StatusCode.value__) {
{ @(429, 503, 504) -contains $_ } {
if ($Retrycount -ge $Retries) {
Write-Warning "Request to $OpenApiServiceUrl failed the maximum number of $Retrycount times."
throw
}
else {
[ref]$RetryAfterHeader = $null
if ($Exception.Response.Headers.TryGetValues("Retry-After", $RetryAfterHeader)) {
# Use Retry-After response header
$DelayInSeconds = $RetryAfterHeader.Value
}
else {
# Use exponential backoff
$mPow = [math]::Pow(2, $Retrycount)
$DelayInSeconds = $mPow * $Delay
}
Write-Warning "Request to $OpenApiServiceUrl failed. Retrying in $DelayInSeconds seconds."
Start-Sleep $DelayInSeconds
$Retrycount++
}
}
404 {
Write-Warning "Request to $OpenApiServiceUrl returned 404. Download will be skipped."
$Completed = $true
}
default {
# Get HTTP error message from DevX api, Re-throw error to be handled Upstream
$ErrorMessage = $Exception.Message
Write-Warning "[$RequestCount] Request for $ModuleName failed with error message: $ErrorMessage"
throw
}
}
}
}