Skip to content

Commit 03d567f

Browse files
committed
Refactored API functions to enhance error handling and pagination. Adjusted "Deployment Pipeline" functions to new version of Invoke-FabricRestMethod #143
1 parent 30791f5 commit 03d567f

8 files changed

+128
-154
lines changed

source/Private/Get-FabricContinuationToken.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function Get-FabricContinuationToken {
3030
$continuationToken = $null
3131
if ($null -ne $Response) {
3232
# Update the continuation token if present
33-
if ($Response.PSObject.Properties.Match("continuationToken")) {
33+
if ($Response.PSObject.Properties.Match("continuationToken") -and $Response.continuationToken -ne $null) {
3434
$continuationToken = $Response.continuationToken
3535
Write-Message -Message "New continuation token: $continuationToken" -Level Debug
3636
} else {

source/Private/Test-FabricApiResponse.ps1

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ function Test-FabricApiResponse {
1010
.PARAMETER Response
1111
The response body from the API call.
1212
13+
.PARAMETER ResponseHeader
14+
The response headers from the API call. This is used to retrieve operation IDs and other metadata.
15+
16+
.PARAMETER StatusCode
17+
The HTTP status code returned by the API call. This is used to determine how to handle the response.
18+
1319
.PARAMETER Operation
1420
The operation being performed by parent function (e.g., 'New', 'Update', 'Remove', 'Get'). It helps in logging appropriate messages.
21+
This parameter is optional and can be used to customize the logging message based on the operation type.
1522
1623
.PARAMETER ObjectIdOrName
1724
The name or ID of the resource being operated.
1825
1926
.PARAMETER TypeName
2027
The type of resource being operated (default: 'Fabric Item').
2128
29+
.PARAMETER SuccessMessage
30+
A custom success message to log upon successful completion of the operation. This overrides the default message based on Operation, TypeName, ObjectIdOrName.
31+
This parameter is optional and can be used to provide a specific success message for the operation.
32+
2233
.PARAMETER NoWait
2334
If specified, the function will not wait for the operation to complete and will return immediately.
2435
@@ -36,6 +47,7 @@ function Test-FabricApiResponse {
3647
- This function is designed to be used within the context of a Fabric API client.
3748
- It requires the `Write-Message` function to log messages at different levels (Info, Debug, Error).
3849
- The function handles long-running operations by checking the status of the operation and retrieving the result if it has succeeded.
50+
- Supports handling of API rate limiting (HTTP 429) by waiting for the specified retry duration before returning a command to repeat the request.
3951
4052
Author: Kamil Nowinski
4153
@@ -65,10 +77,7 @@ function Test-FabricApiResponse {
6577
[string] $SuccessMessage,
6678

6779
[Parameter(Mandatory = $false)]
68-
[switch] $NoWait = $false,
69-
70-
[Parameter(Mandatory = $false)]
71-
[switch] $ExtractValue = $false
80+
[switch] $NoWait = $false
7281
)
7382

7483
Write-Message -Message "::Begin" -Level Debug
@@ -166,17 +175,5 @@ function Test-FabricApiResponse {
166175

167176
Write-Message -Message "::End" -Level Debug
168177

169-
# Return the "value" object if exists, otherwise return the response directly
170-
if ($result -and $ExtractValue) {
171-
Write-Message -Message "Extracting 'value' property from the response as requested." -Level Debug
172-
if (-not $result.value) {
173-
Write-Message -Message "No 'value' property found in the response." -Level Warning
174-
return $result
175-
}
176-
$result | ForEach-Object { $result.value }
177-
}
178-
else {
179-
$result
180-
}
181-
178+
$result
182179
}

source/Public/Deployment Pipeline/Get-FabricDeploymentPipeline.ps1

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,73 +58,28 @@ function Get-FabricDeploymentPipeline {
5858
if ($DeploymentPipelineId) {
5959
Write-Message -Message "Retrieving specific deployment pipeline with ID: $DeploymentPipelineId" -Level Debug
6060
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId"
61-
62-
$response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Get -HandleResponse -TypeName "deployment pipeline"
63-
return $response
64-
65-
# Validate response
66-
#Test-FabricApiResponse -response $response -ObjectIdOrName $DeploymentPipelineId -typeName "deployment pipeline"
67-
68-
# if ($response) {
69-
# Write-Message -Message "Successfully retrieved deployment pipeline." -Level Debug
70-
# return $response
71-
# } else {
72-
# Write-Message -Message "No deployment pipeline found with the specified ID." -Level Warning
73-
# return $null
74-
# }
61+
} else {
62+
$apiEndpointUrl = "deploymentPipelines"
7563
}
7664

77-
# Step 2: Initialize variables for listing all pipelines
78-
$continuationToken = $null
79-
$pipelines = @()
80-
81-
if (-not ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq "System.Web" })) {
82-
Add-Type -AssemblyName System.Web
65+
# Step 5: Make the API request
66+
$apiParameters = @{
67+
Uri = $apiEndpointUrl
68+
Method = 'GET'
69+
HandleResponse = $true
70+
TypeName = "deployment pipeline"
8371
}
84-
85-
# Step 3: Loop to retrieve all pipelines with continuation token
86-
Write-Message -Message "Loop started to get continuation token" -Level Debug
87-
$baseApiEndpointUrl = "deploymentPipelines"
88-
89-
do {
90-
# Step 4: Construct the API URL
91-
$apiEndpointUrl = $baseApiEndpointUrl
92-
93-
if ($null -ne $continuationToken) {
94-
# URL-encode the continuation token
95-
$encodedToken = [System.Web.HttpUtility]::UrlEncode($continuationToken)
96-
$apiEndpointUrl = "{0}?continuationToken={1}" -f $apiEndpointUrl, $encodedToken
97-
}
98-
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
99-
100-
# Step 5: Make the API request
101-
$apiParameters = @{
102-
Uri = $apiEndpointUrl
103-
Method = 'GET'
104-
HandleResponse = $true
105-
TypeName = "deployment pipeline"
106-
}
107-
$response = Invoke-FabricRestMethod @apiParameters
108-
109-
# Step 6: Process response and update continuation token
110-
if ($null -ne $response) {
111-
$pipelines += $response.value
112-
}
113-
$continuationToken = Get-FabricContinuationToken -Response $response
114-
115-
} while ($null -ne $continuationToken)
116-
117-
Write-Message -Message "Loop finished and all data added to the list" -Level Debug
72+
$response = Invoke-FabricRestMethod @apiParameters
11873

11974
if ($DeploymentPipelineName)
12075
{
12176
# Filter the list by name
12277
Write-Message -Message "Filtering deployment pipelines by name: $DeploymentPipelineName" -Level Debug
123-
$pipelines = $pipelines | Where-Object { $_.displayName -eq $DeploymentPipelineName }
78+
$response = $response | Where-Object { $_.displayName -eq $DeploymentPipelineName }
12479
}
12580

12681
# Step 7: Handle results
127-
$pipelines
82+
$response
12883

12984
} catch {
13085
# Step 8: Error handling

source/Public/Deployment Pipeline/Get-FabricDeploymentPipelineOperation.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function Get-FabricDeploymentPipelineOperation {
4949

5050
# Step 2: Construct the API URL
5151
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId/operations/$OperationId"
52-
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
5352

5453
# Step 3: Make the API request
5554
$apiParameters = @{
@@ -60,6 +59,7 @@ function Get-FabricDeploymentPipelineOperation {
6059
ObjectIdOrName = $DeploymentPipelineId
6160
}
6261
$response = Invoke-FabricRestMethod @apiParameters
62+
6363
$response
6464

6565
} catch {

source/Public/Deployment Pipeline/Get-FabricDeploymentPipelineRoleAssignments.ps1

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,21 @@ function Get-FabricDeploymentPipelineRoleAssignments {
3535
# Step 1: Ensure token validity
3636
Confirm-TokenState
3737

38-
# Step 2: Initialize variables
39-
$continuationToken = $null
40-
$roleAssignments = @()
41-
42-
do {
43-
# Step 3: Construct the API URL
44-
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId/roleAssignments"
45-
if ($continuationToken) {
46-
$apiEndpointUrl += "?continuationToken=$continuationToken"
47-
}
48-
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
49-
50-
# Step 4: Make the API request and validate response
51-
$apiParameters = @{
52-
Uri = $apiEndpointUrl
53-
Method = 'GET'
54-
HandleResponse = $true
55-
TypeName = "deployment pipeline role assignments"
56-
ObjectIdOrName = $DeploymentPipelineId
57-
}
58-
$response = Invoke-FabricRestMethod @apiParameters
59-
60-
# Step 5: Process response and update continuation token
61-
if ($response.value) {
62-
$roleAssignments += $response.value
63-
Write-Message -Message "Added $($response.value.Count) role assignments to the result set." -Level Debug
64-
}
65-
$continuationToken = Get-FabricContinuationToken -Response $response
66-
67-
} while ($continuationToken)
38+
# Step 3: Construct the API URL
39+
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId/roleAssignments"
40+
41+
# Step 4: Make the API request and validate response
42+
$apiParameters = @{
43+
Uri = $apiEndpointUrl
44+
Method = 'GET'
45+
HandleResponse = $true
46+
TypeName = "deployment pipeline role assignments"
47+
ObjectIdOrName = $DeploymentPipelineId
48+
}
49+
$response = Invoke-FabricRestMethod @apiParameters
6850

6951
# Step 7: Return results
70-
Write-Message -Message "??? Successfully retrieved $($roleAssignments.Count) role assignments." -Level Info
71-
$roleAssignments
52+
$response
7253

7354
} catch {
7455
# Step 8: Error handling

source/Public/Deployment Pipeline/Get-FabricDeploymentPipelineStage.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ function Get-FabricDeploymentPipelineStage {
6565
Uri = $apiEndpointUrl
6666
Method = 'GET'
6767
HandleResponse = $true
68+
ExtractValue = $true
6869
TypeName = "deployment pipeline stage"
6970
ObjectIdOrName = $StageId ? $StageId : $DeploymentPipelineId
7071
}
7172
$response = Invoke-FabricRestMethod @apiParameters
72-
$response.value
73+
74+
$response
7375

7476
} catch {
7577
# Step 6: Error handling

source/Public/Deployment Pipeline/Get-FabricDeploymentPipelineStageItem.ps1

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,20 @@ function Get-FabricDeploymentPipelineStageItem {
4848
# Step 1: Ensure token validity
4949
Confirm-TokenState
5050

51-
# Step 2: Initialize variables for pagination
52-
$continuationToken = $null
53-
$allItems = @()
54-
55-
do {
56-
# Step 3: Construct the API URL
57-
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId/stages/$StageId/items"
58-
if ($continuationToken) {
59-
# URL-encode the continuation token
60-
$encodedToken = [System.Web.HttpUtility]::UrlEncode($continuationToken)
61-
$apiEndpointUrl = "{0}?continuationToken={1}" -f $apiEndpointUrl, $encodedToken
62-
}
63-
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
64-
65-
# Step 4: Make the API request
66-
$apiParameters = @{
67-
Uri = $apiEndpointUrl
68-
Method = 'GET'
69-
HandleResponse = $true
70-
TypeName = "deployment pipeline stage items"
71-
ObjectIdOrName = $StageId
72-
}
73-
$response = Invoke-FabricRestMethod @apiParameters
74-
75-
# Step 6: Process results
76-
if ($response.value) {
77-
$allItems += $response.value
78-
Write-Message -Message "Retrieved $($response.value.Count) items." -Level Debug
79-
}
80-
$continuationToken = Get-FabricContinuationToken -Response $response
81-
} while ($continuationToken)
82-
83-
$allItems
51+
# Step 3: Construct the API URL
52+
$apiEndpointUrl = "deploymentPipelines/$DeploymentPipelineId/stages/$StageId/items"
53+
54+
# Step 4: Make the API request
55+
$apiParameters = @{
56+
Uri = $apiEndpointUrl
57+
Method = 'GET'
58+
HandleResponse = $true
59+
TypeName = "deployment pipeline stage items"
60+
ObjectIdOrName = $StageId
61+
}
62+
$response = Invoke-FabricRestMethod @apiParameters
63+
64+
$response
8465

8566
} catch {
8667
# Step 8: Error handling

0 commit comments

Comments
 (0)