Private/New-URIString.ps1
|
function New-URIString { <# .SYNOPSIS Builds the URI of an endpoint of the connected Ghost site. .DESCRIPTION Appends the endpoint path from Get-GhostAPIData to the Admin API address of the connected site and replaces tokens such as {id} with the value of the parameter that has that name. .EXAMPLE New-URIString -Endpoint '/posts/{id}/' -Parameters $PSBoundParameters Returns https://example.ghost.io/ghost/api/admin/posts/5b7ada404f87d200b5b1f9c8/ when -Id was passed. #> [CmdletBinding()] [OutputType([string])] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Builds a string. Nothing is changed on the system or on the site.' )] param( # The endpoint path as defined in Get-GhostAPIData, such as /posts/{id}/. [Parameter(Mandatory)] [string] $Endpoint, # The parameters of the calling function, usually $PSBoundParameters. [System.Collections.IDictionary] $Parameters = @{}, # The address of the Admin API. Defaults to the connected site. [string] $BaseUri ) if (-not $BaseUri) { $BaseUri = $script:GhostConnection['ApiUrl'] } $path = $Endpoint foreach ($token in [regex]::Matches($Endpoint, '\{(?<name>\w+)\}')) { $name = $token.Groups['name'].Value $value = if ($Parameters.Keys -contains $name) { [string] $Parameters[$name] } else { '' } if ([string]::IsNullOrWhiteSpace($value)) { throw "The endpoint '$Endpoint' needs a value for '$name'." } $path = $path.Replace($token.Value, [uri]::EscapeDataString($value)) } $BaseUri.TrimEnd('/') + $path } |