private/_PSTSServices.ps1
if ( $null -eq $(get-module |Where-Object {$_.Name -eq "Newtonsoft.Json"})) { import-module Newtonsoft.Json } #region Mockable Function _PSTSGetToken { param() return [Environment]::GetEnvironmentVariable("TOKEN") #$env:TOKEN } Function _PSTSGetAccount { param() return [Environment]::GetEnvironmentVariable("ACCOUNT") #$env:ACCOUNT } Function _PSTSGetEndPoint { param() return [Environment]::GetEnvironmentVariable("ENDPOINT") #$env:ENDPOINT } #endregion Function _PSTSAPI { param( [string] $subDomain=$null, [string] $project=$null, [string] $resource, [string] $contentType="application/json", [string] $queryParameters=$null, [string] $inFile=$null, [string] $body=$null, [string] $apiVersion="api-version=5.1-preview.1", [ValidateSet('Get', 'Post', 'Patch', 'Delete', 'Options', 'Put', 'Head')] [string] $method, [string] $id, [switch] $Debug ) if ($( $(_PSTSGetToken) -eq [string]::Empty) -or $($null -eq $(_PSTSGetToken))) { Throw [Exception]::new("No account has been defined. ` Please run Add-PSTSAccount or Set-PSTSAccount prior to any use") } $params = @{} $UriParams = @{} + $PSBoundParameters; $otherParams = @{} + $PSBoundParameters; #build url $('method', 'body', 'inFile', 'contentType', 'apiVersion') | ` Foreach-Object -process { $UriParams.Remove($_) | Out-Null } $params.Add("Uri", $( _PSTSURL @UriParams )) #build anything else $('subDomain', 'project', 'resource', 'queryParameters', 'apiVersion', 'id') | ` Foreach-Object -process { $otherParams.Remove($_) | Out-Null } $(_PSTSParams @otherParams) | ` Foreach-Object -process { $params.Add($_.Key, $_.Value) } #add headers #$params.Add("Headers", ` #@{"Authorization"="Basic $(_PSTSGetToken)"; ` #"Accept"="application/json; $($apiversion)"; }) #add headers $params.Add("Headers", ` @{"Authorization"="Basic $(_PSTSGetToken)"; ` "Accept"="$($contentType)" }) if ([string]::IsNullOrEmpty($apiVersion) -eq $false) { $params.Headers.Accept = "$($params.Headers.Accept); $($apiVersion)" } # try { $response = Invoke-WebRequest @params -UseBasicParsing $ret = $($response.Content | ConvertFrom-JsonNewtonsoft) # } #catch { # # Write-Host "Failed to process the web request. status code : $($_.Exception.Response.StatusCode.Value__)" -ForegroundColor Red # return $null # } return $ret } Function _PSTSURL { [OutputType([String])] [CmdletBinding()] param( [string] $resource, [string] $project=$null, [string] $subDomain=$null, [object] $queryParameters=$null, [string] $id=$null ) if ($null -ne $queryParameters) { $queryParameters = "?" + $queryParameters } if ($subDomain -ne [string]::Empty) { $subDomain = $subDomain + "."} if ($id -ne [string]::Empty) { $id = "/" + $id} $url = "https://$($subDomain)$(_PSTSGetEndPoint)/$(_PSTSGetAccount)/$($project)/_apis/$($resource)$($id)$($queryParameters)" if ($null -ne $DebugPreference -and $DebugPreference -ne [string]::Empty ) { Write-Debug $url } return $url } Function _PSTSParams { [CmdletBinding()] param( [string] $contentType="application/json", [string] $inFile=$null, [string] $body=$null, [ValidateSet('Get', 'Post', 'Patch', 'Delete', 'Options', 'Put', 'Head')] [string] $method ) $params = @{} $PSBoundParameters.GetEnumerator() |Foreach-Object -Process { if ($null -ne $_.Value) { $params.Add($_.Key, $_.Value) } } return $params.GetEnumerator() } Function IIf($If, $Right, $Wrong) { If ($If) { $Right } Else { $Wrong } } |