Skip to content

Commit 529de39

Browse files
authored
Merge pull request #160 from davidhaymond/api-refactor
Refactor all API calls into single function. @davidhaymond thanks for this PR. @Celerium thanks for the testing and comments.
2 parents 89e9b0e + 3dfacee commit 529de39

32 files changed

+453
-1532
lines changed

ITGlueAPI/ITGlueAPI.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
NestedModules = 'Internal/BaseURI.ps1',
7373
'Internal/APIKey.ps1',
7474
'Internal/ModuleSettings.ps1',
75+
'Internal/APICalls.ps1',
7576
'Resources/Attachments.ps1',
7677
'Resources/ConfigurationInterfaces.ps1',
7778
'Resources/Configurations.ps1',

ITGlueAPI/Internal/APICalls.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function ConvertTo-QueryString([Hashtable]$QueryParams) {
2+
if (-not $QueryParams) {
3+
return ""
4+
}
5+
6+
$params = @()
7+
foreach ($key in $QueryParams.Keys) {
8+
$value = [System.Net.WebUtility]::UrlEncode($QueryParams[$key])
9+
$params += "$key=$value"
10+
}
11+
12+
$query_string = '?' + ($params -join '&')
13+
return $query_string
14+
}
15+
16+
function Invoke-ITGlueRequest {
17+
param (
18+
[ValidateSet('GET', 'POST', 'PATCH', 'DELETE')]
19+
[string]$Method = 'GET',
20+
21+
[Parameter(Mandatory = $true)]
22+
[string]$ResourceURI,
23+
24+
[Hashtable]$QueryParams = $null,
25+
26+
[Hashtable]$Data = $null
27+
)
28+
29+
$query_string = ConvertTo-QueryString -QueryParams $QueryParams
30+
31+
if ($null -eq $Data) {
32+
$body = $null
33+
} else {
34+
$body = @{'data'=$Data} | ConvertTo-Json -Depth $ITGlue_JSON_Conversion_Depth
35+
}
36+
37+
try {
38+
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
39+
40+
$parameters = @{
41+
'Method' = $Method
42+
'Uri' = $ITGlue_Base_URI + $ResourceURI + $query_string
43+
'Headers' = $ITGlue_Headers
44+
'Body' = $body
45+
}
46+
47+
$api_response = Invoke-RestMethod @parameters -ErrorAction Stop
48+
} catch {
49+
Write-Error $_
50+
} finally {
51+
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
52+
}
53+
54+
return $api_response
55+
}

ITGlueAPI/Resources/Attachments.ps1

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,8 @@ function New-ITGlueAttachments {
1515

1616
$resource_uri = ('/{0}/{1}/relationships/attachments' -f $resource_type, $resource_id)
1717

18-
$body = @{}
18+
return Invoke-ITGlueRequest -Method GET -ResourceURI $resource_uri -Data $data
1919

20-
$body += @{'data'= $data}
21-
22-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
23-
24-
try {
25-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
26-
$rest_output = Invoke-RestMethod -method 'POST' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
27-
-body $body -ErrorAction Stop -ErrorVariable $web_error
28-
} catch {
29-
Write-Error $_
30-
} finally {
31-
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
32-
}
33-
34-
$data = @{}
35-
$data = $rest_output
36-
return $data
3720
}
3821

3922
function Set-ITGlueAttachments {
@@ -57,25 +40,7 @@ function Set-ITGlueAttachments {
5740

5841
$resource_uri = ('/{0}/{1}/relationships/attachments/{2}' -f $resource_type, $resource_id, $id)
5942

60-
$body = @{}
61-
62-
$body += @{'data' = $data}
63-
64-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
65-
66-
try {
67-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
68-
$rest_output = Invoke-RestMethod -method 'PATCH' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
69-
-body $body -ErrorAction Stop -ErrorVariable $web_error
70-
} catch {
71-
Write-Error $_
72-
} finally {
73-
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
74-
}
75-
76-
$data = @{}
77-
$data = $rest_output
78-
return $data
43+
return Invoke-ITGlueRequest -Method PATCH -ResourceURI $resource_uri -Data $data
7944
}
8045

8146
function Remove-ITGlueAttachments {
@@ -95,23 +60,5 @@ function Remove-ITGlueAttachments {
9560

9661
$resource_uri = ('/{0}/{1}/relationships/attachments' -f $resource_type, $resource_id)
9762

98-
$body = @{}
99-
100-
$body += @{'data' = $data}
101-
102-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
103-
104-
try {
105-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
106-
$rest_output = Invoke-RestMethod -method 'DELETE' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
107-
-body $body -ErrorAction Stop -ErrorVariable $web_error
108-
} catch {
109-
Write-Error $_
110-
} finally {
111-
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
112-
}
113-
114-
$data = @{}
115-
$data = $rest_output
116-
return $data
63+
return Invoke-ITGlueRequest -Method DELETE -ResourceURI $resource_uri -Data $data
11764
}

ITGlueAPI/Resources/ConfigurationInterfaces.ps1

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,7 @@ function New-ITGlueConfigurationInterfaces {
1212
$resource_uri = ('/configurations/{0}/relationships/configuration_interfaces' -f $conf_id)
1313
}
1414

15-
$body = @{}
16-
17-
$body += @{'data' = $data}
18-
19-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
20-
21-
try {
22-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
23-
$rest_output = Invoke-RestMethod -method 'POST' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
24-
-body $body -ErrorAction Stop -ErrorVariable $web_error
25-
} catch {
26-
Write-Error $_
27-
} finally {
28-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
29-
}
30-
31-
$data = @{}
32-
$data = $rest_output
33-
return $data
15+
return Invoke-ITGlueRequest -Method POST -ResourceURI $resource_uri -Data $data
3416
}
3517

3618
function Get-ITGlueConfigurationInterfaces {
@@ -71,39 +53,27 @@ function Get-ITGlueConfigurationInterfaces {
7153
}
7254
}
7355

74-
$body = @{}
56+
$query_params = @{}
7557

7658
if ($PSCmdlet.ParameterSetName -eq 'index') {
7759
if ($filter_id) {
78-
$body += @{'filter[id]' = $filter_id}
60+
$query_params['filter[id]'] = $filter_id
7961
}
8062
if ($filter_ip_address) {
81-
$body += @{'filter[ip_address]' = $filter_ip_address }
63+
$query_params['filter[ip_address]'] = $filter_ip_address
8264
}
8365
if ($sort) {
84-
$body += @{'sort' = $sort}
66+
$query_params['sort'] = $sort
8567
}
8668
if ($page_number) {
87-
$body += @{'page[number]' = $page_number}
69+
$query_params['page[number]'] = $page_number
8870
}
8971
if ($page_size) {
90-
$body += @{'page[size]' = $page_size}
72+
$query_params['page[size]'] = $page_size
9173
}
9274
}
9375

94-
try {
95-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
96-
$rest_output = Invoke-RestMethod -method 'GET' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
97-
-body $body -ErrorAction Stop -ErrorVariable $web_error
98-
} catch {
99-
Write-Error $_
100-
} finally {
101-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
102-
}
103-
104-
$data = @{}
105-
$data = $rest_output
106-
return $data
76+
return Invoke-ITGlueRequest -Method GET -ResourceURI $resource_uri -QueryParams $query_params
10777
}
10878

10979
function Set-ITGlueConfigurationInterfaces {
@@ -130,29 +100,13 @@ function Set-ITGlueConfigurationInterfaces {
130100
$resource_uri = ('/configurations/{0}/relationships/configuration_interfaces/{1}' -f $conf_id, $id)
131101
}
132102

133-
$body = @{}
103+
$query_params = @{}
134104

135105
if ($PSCmdlet.ParameterSetName -eq 'bulk_delete') {
136106
if ($filter_id) {
137-
$body += @{'filter[id]' = $filter_id}
107+
$query_params['filter[id]'] = $filter_id
138108
}
139109
}
140110

141-
$body += @{'data' = $data}
142-
143-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
144-
145-
try {
146-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
147-
$rest_output = Invoke-RestMethod -method 'PATCH' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
148-
-body $body -ErrorAction Stop -ErrorVariable $web_error
149-
} catch {
150-
Write-Error $_
151-
} finally {
152-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
153-
}
154-
155-
$data = @{}
156-
$data = $rest_output
157-
return $data
111+
return Invoke-ITGlueRequest -Method PATCH -ResourceURI $resource_uri -data $Data -QueryParams $query_params
158112
}

ITGlueAPI/Resources/ConfigurationStatuses.ps1

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@ function New-ITGlueConfigurationStatuses {
66

77
$resource_uri = '/configuration_statuses/'
88

9-
$body = @{}
10-
11-
$body += @{'data' = $data}
12-
13-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
14-
15-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
16-
$rest_output = Invoke-RestMethod -method 'POST' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
17-
-body $body -ErrorAction Stop -ErrorVariable $web_error
18-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
19-
20-
$data = @{}
21-
$data = $rest_output
22-
return $data
9+
return Invoke-ITGlueRequest -Method POST -ResourceURI $resource_uri -Data $data
2310
}
2411

2512
function Get-ITGlueConfigurationStatuses {
@@ -45,31 +32,24 @@ function Get-ITGlueConfigurationStatuses {
4532

4633
$resource_uri = ('/configuration_statuses/{0}' -f $id)
4734

48-
$body = @{}
35+
$query_params = @{}
4936

5037
if ($PSCmdlet.ParameterSetName -eq 'index') {
5138
if ($filter_name) {
52-
$body += @{'filter[name]' = $filter_name}
39+
$query_params['filter[name]'] = $filter_name
5340
}
5441
if ($sort) {
55-
$body += @{'sort' = $sort}
42+
$query_params['sort'] = $sort
5643
}
5744
if ($page_number) {
58-
$body += @{'page[number]' = $page_number}
45+
$query_params['page[number]'] = $page_number
5946
}
6047
if ($page_size) {
61-
$body += @{'page[size]' = $page_size}
48+
$query_params['page[size]'] = $page_size
6249
}
6350
}
6451

65-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
66-
$rest_output = Invoke-RestMethod -method 'GET' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
67-
-body $body -ErrorAction Stop -ErrorVariable $web_error
68-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
69-
70-
$data = @{}
71-
$data = $rest_output
72-
return $data
52+
return Invoke-ITGlueRequest -Method GET -ResourceURI $resource_uri -QueryParams $query_params
7353
}
7454

7555
function Set-ITGlueConfigurationStatuses {
@@ -83,18 +63,5 @@ function Set-ITGlueConfigurationStatuses {
8363

8464
$resource_uri = ('/configuration_statuses/{0}' -f $id)
8565

86-
$body = @{}
87-
88-
$body += @{'data' = $data}
89-
90-
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
91-
92-
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
93-
$rest_output = Invoke-RestMethod -method 'PATCH' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
94-
-body $body -ErrorAction Stop -ErrorVariable $web_error
95-
[void] ($ITGlue_Headers.Remove('x-api-key')) # Quietly clean up scope so the API key doesn't persist
96-
97-
$data = @{}
98-
$data = $rest_output
99-
return $data
66+
return Invoke-ITGlueRequest -Method PATCH -ResourceURI $resource_uri -Data $data
10067
}

0 commit comments

Comments
 (0)