Skip to content

Commit 5750df6

Browse files
author
Emile
authored
Removed redundant code
Added method to a variable, default to `DELETE` and change to `PATCH` if parameter set is `bulk_destroy`. Moved `$body` etc to within `bulk_destroy` in order to keep as `null` and not fail during `Invoke-RestMethod`.
1 parent 704c09a commit 5750df6

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

ITGlueAPI/Resources/Passwords.ps1

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ function Remove-ITGluePasswords {
222222
)
223223

224224
$resource_uri = ('/passwords/{0}' -f $id)
225+
$method = 'DELETE'
225226

226227
$body = @{}
227228

@@ -244,22 +245,17 @@ function Remove-ITGluePasswords {
244245
if ($filter_cached_resource_name) {
245246
$body += @{'filter[cached_resource_name]' = $filter_cached_resource_name}
246247
}
247-
}
248+
$method = 'PATCH'
248249

249-
$body += @{'data' = $data}
250+
$body += @{'data' = $data}
250251

251-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
252+
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
253+
}
252254

253255
try {
254-
if($PSCmdlet.ParameterSetName -eq 'destroy') {
255-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
256-
$rest_output = Invoke-RestMethod -method 'DELETE' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
257-
-ErrorAction Stop -ErrorVariable $web_error
258-
} else {
259-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
260-
$rest_output = Invoke-RestMethod -method 'PATCH' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
261-
-body $body -ErrorAction Stop -ErrorVariable $web_error
262-
}
256+
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
257+
$rest_output = Invoke-RestMethod -method $method -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
258+
-body $body -ErrorAction Stop -ErrorVariable $web_error
263259
} catch {
264260
Write-Error $_
265261
} finally {

0 commit comments

Comments
 (0)