Skip to content

Commit

Permalink
fix: Get-Twinkly reconnection ( Fixes #130 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Jul 14, 2024
1 parent 48494c1 commit 4fcecff
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions Commands/Twinkly/Get-Twinkly.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,34 @@
Write-Verbose "GET $uri"
}

Invoke-RestMethod -Uri $uri -Headers @{
"X-Auth-Token" = $script:TwinklyCache["$ip"].authentication_token
} |
& { process {
$restOutput =
try {
Invoke-RestMethod -Uri $uri -Headers @{
"X-Auth-Token" = $script:TwinklyCache["$ip"].authentication_token
} -ErrorAction Stop *>&1
} catch {
$ex = $_
# Twinly API returns 401 when the token has expired or another device has logged in.
if ($ex.Exception.StatusCode -eq 401) {
# Reconnect and try again.
$twinklyConnection = Connect-Twinkly -IPAddress $ip -PassThru
Invoke-RestMethod -Uri $uri -Headers @{
"X-Auth-Token" = $twinklyConnection.authentication_token
}
} else {
$ex
}
}
$restOutput |
& { process {
$out = $_

$out.pstypenames.clear()
$out.pstypenames.add(($PSCmdlet.ParameterSetName -replace '/xled/v1/' -replace '/', '.'))
$out
if ($out -is [Management.Automation.ErrorRecord]) {
$out
} else {
$out.pstypenames.clear()
$out.pstypenames.add(($PSCmdlet.ParameterSetName -replace '/xled/v1/' -replace '/', '.'))
$out
}
} }
}
} elseif ($PSCmdlet.ParameterSetName -eq 'ListDevices') {
Expand Down

0 comments on commit 4fcecff

Please sign in to comment.