-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-DomeneshopApiRequest.ps1
More file actions
79 lines (61 loc) · 2.09 KB
/
Copy pathInvoke-DomeneshopApiRequest.ps1
File metadata and controls
79 lines (61 loc) · 2.09 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Invoke-DomeneshopApiRequest {
<#
.SYNOPSIS
Send an authenticated request to the Domeneshop API.
.DESCRIPTION
Build an HTTP Basic credential from a resolved Domeneshop context and send a REST request, optionally with a JSON body.
.EXAMPLE
Invoke-DomeneshopApiRequest -Method Get -Uri $uri -Context $resolvedContext
Send an authenticated GET request.
.EXAMPLE
Invoke-DomeneshopApiRequest -Method Post -Uri $uri -Context $resolvedContext -Body $body
Send an authenticated POST request with a JSON body.
.INPUTS
None
You can't pipe objects to Invoke-DomeneshopApiRequest.
.OUTPUTS
System.Object
The response returned by the Domeneshop API.
.NOTES
Transport errors are terminating so public callers fail fast.
.LINK
https://api.domeneshop.no/docs/
#>
[OutputType([object])]
[CmdletBinding()]
param(
# The HTTP method accepted by the Domeneshop API.
[Parameter(Mandatory)]
[ValidateSet('Get', 'Post', 'Put', 'Delete')]
[string] $Method,
# The absolute Domeneshop API endpoint URI.
[Parameter(Mandatory)]
[ValidateNotNull()]
[uri] $Uri,
# The resolved Domeneshop context containing valid credentials.
[Parameter(Mandatory)]
[ValidateNotNull()]
[object] $Context,
# The request payload to serialize as JSON.
[Parameter()]
[AllowNull()]
[object] $Body
)
$credential = [pscredential]::new(
[string] $Context.Token,
[securestring] $Context.Secret
)
$params = @{
Method = $Method
Uri = $Uri
Authentication = 'Basic'
Credential = $credential
ErrorAction = 'Stop'
}
if ($PSBoundParameters.ContainsKey('Body')) {
$params['ContentType'] = 'application/json'
$params['Body'] = ($Body | ConvertTo-Json -Depth 100)
}
Write-Debug "Sending $Method request to [$Uri]."
Invoke-RestMethod @params
}