Skip to content

Commit e0fe106

Browse files
author
Validate base JSON
committed
feat: Add authentication refresh and query parameters
When no PAT is provided, the script will check if the token is still valid if a header is already present. The QueryParameters parameter is added so query parameters can be used with this function.
1 parent c6b8105 commit e0fe106

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

AzureDevOpsPowerShell/Private/Invoke-AzDoRestMethod.ps1

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function Invoke-AzDoRestMethod {
2222
[string]
2323
$Version,
2424

25+
[Parameter()]
26+
[string]
27+
$QueryParameters,
28+
2529
[Parameter(Mandatory)]
2630
[ValidateSet('GET', 'POST', 'PATCH', 'DELETE')]
2731
[string]
@@ -43,6 +47,25 @@ function Invoke-AzDoRestMethod {
4347
Write-Debug "method: $Method"
4448
Write-Debug "body: $($body | ConvertTo-Json -Depth 10)"
4549

50+
if (-not($Pat) -and $script:header) {
51+
$params = @{
52+
Uri = "https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=6.0"
53+
Method = 'GET'
54+
Headers = $script:header
55+
ContentType = 'application/json'
56+
}
57+
try {
58+
$profileData = Invoke-RestMethod @params
59+
60+
if (!$profileData.id) {
61+
throw
62+
}
63+
} catch {
64+
Write-Verbose "Refreshing authentication header"
65+
Clear-AzDoAuthHeader
66+
}
67+
}
68+
4669
if (-not($script:header)) {
4770
try {
4871
New-AzDoAuthHeader -PAT $Pat -ErrorAction Stop
@@ -52,12 +75,17 @@ function Invoke-AzDoRestMethod {
5275
}
5376

5477
$params = @{
55-
Uri = "$($Uri)?api-version=$($Version)"
5678
Method = $Method
5779
Headers = $script:header
5880
ContentType = 'application/json'
5981
}
6082

83+
if ($QueryParameters) {
84+
$params.Uri = "$($Uri)?$($QueryParameters)&api-version=$($Version)"
85+
} else {
86+
$params.Uri = "$($Uri)?api-version=$($Version)"
87+
}
88+
6189
Write-Verbose "Uri: $($params.Uri)"
6290
Write-Verbose "Method: $($params.Method)"
6391
}

0 commit comments

Comments
 (0)