Skip to content

Instantly share code, notes, and snippets.

@software-programmer
Last active August 11, 2016 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save software-programmer/442a3ebf4741d50f36c4bfd2f4cb57aa to your computer and use it in GitHub Desktop.
Save software-programmer/442a3ebf4741d50f36c4bfd2f4cb57aa to your computer and use it in GitHub Desktop.
This script can be used as a wrapper around making REST API calls in TeamCity. http://evsoftware.co.uk
# -----------------------------------------------
# REST API Wrapper
# -----------------------------------------------
#
# Ver Who When What
# 1.0 Evolve Software Ltd 11-08-16 Initial Version
# Script Input Parameters
param (
[ValidateNotNullOrEmpty()]
[string] $RestEndPoint = $(throw "-RestEndPoint is mandatory, please provide a value."),
[ValidateNotNullOrEmpty()]
[string] $ApiUsername = $(throw "-ApiUsername is mandatory, please provide a value."),
[ValidateNotNullOrEmpty()]
[string] $ApiPassword = $(throw "-ApiPassword is mandatory, please provide a value."),
[ValidateNotNullOrEmpty()]
[string] $HttpVerb = $(throw "-HttpVerb is mandatory, please provide a value."),
[string] $RestData = "",
[string] $ContentType = "text/plain"
)
function Main()
{
$CurrentScriptVersion = "1.0"
$ApiCredentials = New-Object System.Management.Automation.PSCredential($ApiUsername, (ConvertTo-SecureString $ApiPassword -AsPlainText -Force))
$ApiCredentials_ForHeader = $ApiUsername + ":" + $ApiPassword
$ApiCredentialsBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ApiCredentials_ForHeader))
$ApiCredentialsHeader = @{};
$ApiCredentialsHeader.Add("Authorization", "Basic $ApiCredentialsBase64")
Write-Host "================== REST API Wrapper - Version"$CurrentScriptVersion": START =================="
# Log input variables passed in
Log-Variables
Write-Host
$success = $false;
$attemptsLeft = 3;
do {
try {
switch ($HttpVerb)
{
"GET" { Api-Get $RestEndPoint }
"DELETE" { Api-Delete $RestEndPoint }
"POST" { Api-Post $RestEndPoint $RestData $ContentType }
"PUT" { Api-Put $RestEndPoint $RestData $ContentType }
}
$success = $true
}
catch [System.Exception] {
Write-Output $_
Start-Sleep -Milliseconds 10000
}
$attemptsLeft--;
}
while (-not $success -and $attemptsLeft -gt 0)
if (-not $success) {
Write-Output "Unable to call rest API: $RestEndPoint"
Write-Output ##teamcity[buildStatus status='FAILURE' ]
Exit 1
}
Write-Host "================== REST API Wrapper - Version"$CurrentScriptVersion": END =================="
}
function Log-Variables
{
Write-Host "RestEndPoint: " $RestEndPoint
Write-Host "HttpVerb: " $HttpVerb
Write-Host "RestData: " $RestData
Write-Host "Computername:" (gc env:computername)
}
function Api-Get($Url)
{
Write-Host $Url
return Invoke-RestMethod -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Get -TimeoutSec 30;
}
function Api-Delete($Url)
{
Write-Host $Url
return Invoke-WebRequest -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Delete -TimeoutSec 30 -UseBasicParsing;
}
function Api-Post($Url, $Data, $Type)
{
Write-Host $Url
return Invoke-RestMethod -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Post -ContentType $Type -Body $Data -TimeoutSec 30 -DisableKeepAlive;
}
function Api-Put($Url, $Data, $Type)
{
Write-Host $Url
return Invoke-RestMethod -Headers $ApiCredentialsHeader -Credential $ApiCredentials -Uri $Url -Method Put -ContentType $Type -Body $Data -TimeoutSec 30 -DisableKeepAlive;
}
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment