-
Notifications
You must be signed in to change notification settings - Fork 3
/
Invoke-GitHubAPI.ps1
59 lines (56 loc) · 2 KB
/
Invoke-GitHubAPI.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function Invoke-GitHubAPI {
<#
.Synopsis
.Example
#>
param(
[Parameter(Mandatory)]
$Uri,
[ValidateSet('Default', 'Delete', 'Get', 'Head', 'Merge', 'Options', 'Patch', 'Post', 'Put', 'Trace')]
$Method = 'Get',
$Body,
$OutFile,
$AccessToken,
[Switch]$FollowRelLink,
$MaximumFollowRelLink,
[Switch]$SilentlyContinue
)
try {
# Invoke-RestMethod -Uri $uri -Headers (Get-GitHubAuthHeader -AccessToken $AccessToken) -Method $Method -Body $Body -OutFile $OutFile -ErrorVariable errVar
# Invoke-RestMethod -Uri $uri -Headers (Get-GitHubAuthHeader -AccessToken $AccessToken) -Method $Method -Body $Body -OutFile $OutFile -FollowRelLink:$FollowRelLink -MaximumFollowRelLink $MaximumFollowRelLink -ErrorVariable errVar
$params = @{
Uri = $uri
Headers = (Get-GitHubAuthHeader -AccessToken $AccessToken)
Method = $Method
Body = $Body
OutFile = $OutFile
}
if ($FollowRelLink.IsPresent -and !$MaximumFollowRelLink) {
$params.FollowRelLink = $FollowRelLink
$params.MaximumFollowRelLink = 1
}
elseif ($FollowRelLink.IsPresent -and $MaximumFollowRelLink) {
$params.FollowRelLink = $FollowRelLink
$params.MaximumFollowRelLink = $MaximumFollowRelLink
}
Invoke-RestMethod @params -ErrorVariable errVar
}
catch {
if ($errVar) {
$targetMessage = $errVar
}
else {
$targetMessage = $_.Exception
}
$msg = ($targetMessage.Message | ConvertFrom-Json | Select-Object -ExpandProperty message)
if ($msg -match '^bad credentials$') {
Write-Warning $msg
}
elseif ($msg -match '^You have to supply an access token') {
Write-Warning $msg
}
elseif (!$SilentlyContinue) {
Write-Warning $msg
}
}
}