70 lines
3.1 KiB
PowerShell
70 lines
3.1 KiB
PowerShell
function Get-NewRelicNextLink {
|
|
<#
|
|
.SYNOPSIS
|
|
Takes a Link header value (in the case of an invalidly formatted header, $null, or "", will return "")
|
|
|
|
.DESCRIPTION
|
|
Assumption (per docs) you will get a header that looks like this:
|
|
|
|
Link: <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=1>;rel="first",
|
|
<https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=18>;rel="prev",
|
|
<https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="next",
|
|
<https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="last"
|
|
|
|
Find the "next" header line, get the url from it.
|
|
|
|
See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link and https://tools.ietf.org/html/rfc5988
|
|
|
|
.PARAMETER LinkHeader
|
|
The header key to parse
|
|
|
|
.PARAMETER LinkType
|
|
The link type to look for. Values: first,prev,next,last
|
|
|
|
.EXAMPLE
|
|
Get-NewRelicNextLink -LinkHeader Link: '<https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=1>;rel="first", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=18>;rel="prev", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="next", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="last"'
|
|
|
|
"https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20"
|
|
.EXAMPLE
|
|
Get-NewRelicNextLink -LinkHeader Link: '<https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=1>;rel="first", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=18>;rel="prev", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="next", <https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20>;rel="last"' -LinkType Last
|
|
|
|
"https://api.newrelic.com/v2/applications/5313884/metrics.xml?page=20"
|
|
#>
|
|
[CmdletBinding()]
|
|
param (
|
|
[string]$LinkHeader,
|
|
[ValidateSet("first","prev","next","last")]
|
|
[string]$LinkType = "next"
|
|
)
|
|
|
|
# It's ok to just not pass in a header value
|
|
if ([string]::IsNullOrWhiteSpace($LinkHeader)) {
|
|
return ""
|
|
}
|
|
|
|
$links += @($LinkHeader.Split(","))
|
|
|
|
# This shape will look like @{ first = url; next = url; last = url; ... }
|
|
$linkHash = @{}
|
|
|
|
if ($links.Count -gt 0) {
|
|
foreach($link in $links) {
|
|
# This is idiosyncratic powershell and does magic where it basically splits on either value
|
|
# You can't combine them, but you could write some complex loop logic to replace it.
|
|
# Code we don't write is code we don't have to support.
|
|
$linkSplits = $link -split '; rel=' -split ';rel='
|
|
if (@($linkSplits).Count -gt 0) {
|
|
# lop off the first and last chars (<,>,") of each string
|
|
$url = $linkSplits[0].Trim().Substring(1,$linkSplits[0].Trim().Length - 2)
|
|
$name = $linkSplits[1].Trim().Substring(1,$linkSplits[1].Trim().Length - 2)
|
|
|
|
# stuff them into an object
|
|
$linkHash[$name] = $url
|
|
Write-Verbose "$logLead : Found a url for '$name' - '$url'"
|
|
}
|
|
}
|
|
}
|
|
|
|
return $linkHash[$LinkType]
|
|
}
|