-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInvoke-DattoRequest.ps1
191 lines (131 loc) · 5.81 KB
/
Invoke-DattoRequest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Invoke-DattoRequest {
<#
.SYNOPSIS
Makes an API request
.DESCRIPTION
The Invoke-DattoRequest cmdlet invokes an API request to Datto API.
This is an internal function that is used by all public functions
As of 2023-09 the Datto v1 API only supports GET and PUT requests
.PARAMETER method
Defines the type of API method to use
Allowed values:
'GET', 'PUT'
.PARAMETER resource_Uri
Defines the resource uri (url) to use when creating the API call
.PARAMETER uri_Filter
Used with the internal function [ ConvertTo-DattoQueryString ] to combine
a functions parameters with the resource_Uri parameter.
This allows for the full uri query to occur
The full resource path is made with the following data
$Datto_Base_URI + $resource_Uri + ConvertTo-DattoQueryString
.PARAMETER data
Place holder parameter to use when other methods are supported
by the Datto v1 API
.PARAMETER allPages
Returns all items from an endpoint
When using this parameter there is no need to use either the page or perPage
parameters
.EXAMPLE
Invoke-DattoRequest -method GET -resource_Uri '/account' -uri_Filter $uri_Filter
Invoke a rest method against the defined resource using any of the provided parameters
Example:
Name Value
---- -----
Method GET
Uri https://api.datto.com/v1/account?accountId=12345&details=True
Headers {Authorization = Bearer 123456789}
Body
.NOTES
N\A
.LINK
https://celerium.github.io/Datto-PowerShellWrapper/site/Internal/Invoke-DattoRequest.html
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[ValidateSet('GET','PUT')]
[String]$method = 'GET',
[Parameter(Mandatory = $true)]
[String]$resource_Uri,
[Parameter(Mandatory = $false)]
[Hashtable]$uri_Filter = $null,
[Parameter(Mandatory = $false)]
[Hashtable]$data = $null,
[Parameter(Mandatory = $false)]
[Switch]$allPages
)
begin {}
process {
# Load Web assembly when needed as PowerShell Core has the assembly preloaded
if ( !("System.Web.HttpUtility" -as [Type]) ) {
Add-Type -Assembly System.Web
}
$query_string = ConvertTo-DattoQueryString -uri_Filter $uri_Filter -resource_Uri $resource_Uri
Set-Variable -Name 'Datto_queryString' -Value $query_string -Scope Global -Force
if ($null -eq $data) {
$body = $null
} else {
$body = $data | ConvertTo-Json -Depth $Datto_JSON_Conversion_Depth
}
try {
$Api_Token = Get-DattoAPIKey -PlainText
$Api_Token_base64 = [Convert]::ToBase64String( [Text.Encoding]::ASCII.GetBytes( ("{0}:{1}" -f ($Api_Token).PublicKey,($Api_Token).SecretKey) ) )
$parameters = [ordered] @{
"Method" = $method
"Uri" = $query_string.Uri
"Headers" = @{ 'Authorization' = 'Basic {0}'-f $Api_Token_base64 }
"Body" = $body
}
if ( $method -ne 'GET' ) {
$parameters['ContentType'] = 'application/json; charset=utf-8'
}
Set-Variable -Name 'Datto_invokeParameters' -Value $parameters -Scope Global -Force
if ($allPages){
Write-Verbose "Gathering all items from [ $( $Datto_Base_URI + $resource_Uri ) ] "
$page_number = 1
$all_responseData = [System.Collections.Generic.List[object]]::new()
do {
$parameters['Uri'] = $query_string.Uri -replace '_page=\d+',"_page=$page_number"
$current_page = Invoke-RestMethod @parameters -ErrorAction Stop
Write-Verbose "[ $page_number ] of [ $($current_page.pagination.totalPages) ] pages"
foreach ($item in $current_page.items){
$all_responseData.add($item)
}
$page_number++
} while ($current_page.pagination.totalPages -ne $page_number - 1 -and $current_page.pagination.totalPages -ne 0)
}
else{
$api_response = Invoke-RestMethod @parameters -ErrorAction Stop
}
}
catch {
$exceptionError = $_.Exception.Message
Write-Warning 'The [ Datto_invokeParameters, Datto_queryString, & Datto_CmdletNameParameters ] variables can provide extra details'
switch -Wildcard ($exceptionError) {
'*404*' { Write-Error "Invoke-DattoRequest : [ $resource_Uri ] not found!" }
'*429*' { Write-Error 'Invoke-DattoRequest : API rate limited' }
'*504*' { Write-Error "Invoke-DattoRequest : Gateway Timeout" }
default { Write-Error $_ }
}
}
finally {
$Auth = $Datto_invokeParameters['headers']['Authorization']
$Datto_invokeParameters['headers']['Authorization'] = $Auth.Substring( 0, [Math]::Min($Auth.Length, 9) ) + '*******'
}
if($allPages){
#Making output consistent
if( [string]::IsNullOrEmpty($all_responseData.data) ){
$api_response = $null
}
else{
$api_response = [PSCustomObject]@{
pagination = $null
items = $all_responseData
}
}
return $api_response
}
else{ return $api_response }
}
end {}
}