-
Notifications
You must be signed in to change notification settings - Fork 2
/
RestWrapperSkeleton.psm1
104 lines (93 loc) · 3.79 KB
/
RestWrapperSkeleton.psm1
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
#Make the URL global since it will likely be consistent between all Rest methods
$global:headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$global:headers.Add("Content-Type", "application/json")
$global:url = "https://pvwa.acme.corp"
function SimpleFunctionName {
param (
#Collect first parameter from command line or pipeline
[Parameter(
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true
)]
[string]$Param1,
#Collect second parameter from command line or pipeline
[Parameter(
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true
)]
[string]$Param2
)
$uri="$url/API/method" #Append URI to the URL to identify the specific Rest Method
$method="POST" #HTTP Verb for the Rest Method
$body=@{ #Assemble body from parameters
"Attribute1" = $Param1
"Attribute2" = $Param2
} | ConvertTo-Json #Convert to JSON for Rest
TRY{
#Invoke the Rest Method and assign the response to $response
$response=Invoke-RestMethod -Uri $uri -Method $method -Headers $headers -Body $body
}
CATCH{
#If the Rest Method fails, assign an appropriate error message to $response
$response = "ErrorCode: " + $_.Exception.Response.StatusCode.value__ + "`n"
$response += "Uri: " + $uri + "`n"
$response += "Method: " + $method + "`n"
$response += "Body: " + $body + "`n"
$response += "Headers: `n"# + $headers.GetEnumerator() | ForEach-Object { $_.Value }
foreach ($key in $headers.Keys) {
$response += "$key -> $($headers[$key])`n"
}
}
#Return Rest response or error message as necessary.
return $response;
}
function PipelineFunctionName {
param (
#Collect first parameter from command line or pipeline
[Parameter(
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true
)]
[string]$Param1,
#Collect second parameter from command line or pipeline
[Parameter(
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true
)]
[string]$Param2
)
BEGIN{ #Execute one time before looking at inputs
$uri="$url/API/method" #Append URI to the URL to identify the specific Rest Method
$method="POST" #HTTP Verb for the Rest Method
}
PROCESS{ #Do this once for each input
$body=@{ #Assemble body from parameters
"Attribute1" = $Param1
"Attribute2" = $Param2
} | ConvertTo-Json #Convert to JSON for Rest
TRY{
#Invoke the Rest Method and assign the response to $response
$response=Invoke-RestMethod -Uri $uri -Method $method -Headers $headers -Body $body
}
CATCH{
#If the Rest Method fails, assign an appropriate error message to $response
$response = "ErrorCode: " + $_.Exception.Response.StatusCode.value__ + "`n"
$response += "Uri: " + $uri + "`n"
$response += "Method: " + $method + "`n"
$response += "Body: " + $body + "`n"
$response += "Headers: `n"# + $headers.GetEnumerator() | ForEach-Object { $_.Value }
foreach ($key in $headers.Keys) {
$response += "$key -> $($headers[$key])`n"
}
}
#Return Rest response or error message as necessary.
return $response;
}
END{
#Execute one time after looking at inputs
}
}