-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
api_client.mustache
272 lines (248 loc) · 9.63 KB
/
api_client.mustache
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{{> partial_header}}
function Invoke-{{{apiNamePrefix}}}ApiClient {
[OutputType('System.Collections.Hashtable')]
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]$Uri,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[string[]]$Accepts,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[string[]]$ContentTypes,
[Parameter(Mandatory)]
[hashtable]$HeaderParameters,
[Parameter(Mandatory)]
[hashtable]$FormParameters,
[Parameter(Mandatory)]
[hashtable]$QueryParameters,
[Parameter(Mandatory)]
[hashtable]$CookieParameters,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Body,
[Parameter(Mandatory)]
[string]$Method,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$ReturnType,
[Parameter(Mandatory)]
[bool]$IsBodyNullable
)
'Calling method: Invoke-{{{apiNamePrefix}}}ApiClient' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug
$Configuration = Get-{{{apiNamePrefix}}}Configuration
$RequestUri = $Configuration["BaseUrl"] + $Uri
$SkipCertificateCheck = $Configuration["SkipCertificateCheck"]
# cookie parameters
foreach ($Parameter in $CookieParameters.GetEnumerator()) {
if ($Parameter.Name -eq "cookieAuth") {
$HeaderParameters["Cookie"] = $Parameter.Value
} else {
$HeaderParameters[$Parameter.Name] = $Parameter.Value
}
}
if ($CookieParameters -and $CookieParameters.Count -gt 1) {
Write-Warning "Multiple cookie parameters found. Currently only the first one is supported/used"
}
# accept, content-type headers
$Accept = SelectHeaders -Headers $Accepts
if ($Accept) {
$HeaderParameters['Accept'] = $Accept
}
[string]$MultiPartBoundary = $null
$ContentType= SelectHeaders -Headers $ContentTypes
if ($ContentType) {
$HeaderParameters['Content-Type'] = $ContentType
if ($ContentType -eq 'multipart/form-data') {
[string]$MultiPartBoundary = [System.Guid]::NewGuid()
$MultiPartBoundary = "---------------------------$MultiPartBoundary"
$HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary"
}
}
# add default headers if any
foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) {
$HeaderParameters[$header.Name] = $header.Value
}
# construct URL query string
$HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Parameter in $QueryParameters.GetEnumerator()) {
if ($Parameter.Value.Count -gt 1) { // array
foreach ($Value in $Parameter.Value) {
$HttpValues.Add($Parameter.Key + '[]', $Value)
}
} else {
$HttpValues.Add($Parameter.Key,$Parameter.Value)
}
}
# Build the request and load it with the query string.
$UriBuilder = [System.UriBuilder]($RequestUri)
$UriBuilder.Query = $HttpValues.ToString()
# include form parameters in the request body
if ($FormParameters -and $FormParameters.Count -gt 0) {
if (![string]::IsNullOrEmpty($MultiPartBoundary)) {
$RequestBody = ""
$LF = "`r`n"
$FormParameters.Keys | ForEach-Object {
$value = $FormParameters[$_]
$isFile = $value.GetType().FullName -eq "System.IO.FileInfo"
$RequestBody += "--$MultiPartBoundary$LF"
$RequestBody += "Content-Disposition: form-data; name=`"$_`""
if ($isFile) {
$fileName = $value.Name
$RequestBody += "; filename=`"$fileName`"$LF"
$RequestBody += "Content-Type: application/octet-stream$LF$LF"
$RequestBody += Get-Content -Path $value.FullName
} else {
$RequestBody += "$LF$LF"
$RequestBody += ([string]$value)
}
$RequestBody += "$LF--$MultiPartBoundary"
}
$RequestBody += "--"
} else {
$RequestBody = $FormParameters
}
}
if ($Body -or $IsBodyNullable) {
$RequestBody = $Body
if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) {
$RequestBody = "null"
}
}
{{#hasHttpSignatureMethods}}
# http signature authentication
$httpSigningConfig = Get-{{{apiNamePrefix}}}ConfigurationHttpSigning
if ($null -ne $httpSigningConfig) {
$httpSignHeaderArgument = @{
Method = $Method
UriBuilder = $UriBuilder
Body = $Body
RequestHeader = $HeaderParameters
}
$signedHeader = Get-{{{apiNamePrefix}}}HttpSignedHeader @httpSignHeaderArgument
if($null -ne $signedHeader -and $signedHeader.Count -gt 0){
foreach($item in $signedHeader.GetEnumerator()){
$HeaderParameters[$item.Name] = $item.Value
}
}
}
{{/hasHttpSignatureMethods}}
if ($SkipCertificateCheck -eq $true) {
if ($null -eq $Configuration["Proxy"]) {
# skip certification check, no proxy
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
-Method $Method `
-Headers $HeaderParameters `
-Body $RequestBody `
-ErrorAction Stop `
-UseBasicParsing `
-SkipCertificateCheck
} else {
# skip certification check, use proxy
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
-Method $Method `
-Headers $HeaderParameters `
-Body $RequestBody `
-ErrorAction Stop `
-UseBasicParsing `
-SkipCertificateCheck `
-Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) `
-ProxyUseDefaultCredentials
}
} else {
if ($null -eq $Configuration["Proxy"]) {
# perform certification check, no proxy
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
-Method $Method `
-Headers $HeaderParameters `
-Body $RequestBody `
-ErrorAction Stop `
-UseBasicParsing
} else {
# perform certification check, use proxy
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
-Method $Method `
-Headers $HeaderParameters `
-Body $RequestBody `
-ErrorAction Stop `
-UseBasicParsing `
-Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) `
-ProxyUseDefaultCredentials
}
}
return @{
Response = DeserializeResponse -Response $Response -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"]
StatusCode = $Response.StatusCode
Headers = $Response.Headers
}
}
# Select JSON MIME if present, otherwise choose the first one if available
function SelectHeaders {
Param(
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[String[]]$Headers
)
foreach ($Header in $Headers) {
if (IsJsonMIME -MIME $Header) {
return $Header
}
}
if (!($Headers) -or $Headers.Count -eq 0) {
return $null
} else {
return $Headers[0] # return the first one
}
}
function IsJsonMIME {
Param(
[Parameter(Mandatory)]
[string]$MIME
)
if ($MIME -match "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$") {
return $true
} else {
return $false
}
}
function DeserializeResponse {
Param(
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$ReturnType,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Response,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[string[]]$ContentTypes
)
If ($null -eq $ContentTypes) {
$ContentTypes = [string[]]@()
}
If ([string]::IsNullOrEmpty($ReturnType) -and $ContentTypes.Count -eq 0) { # void response
return $Response
} Elseif ($ReturnType -match '\[\]$') { # array
return ConvertFrom-Json $Response
} Elseif (@("String", "Boolean", "System.DateTime") -contains $ReturnType) { # string, boolean ,datetime
return $Response
} Else { # others (e.g. model, file)
if ($ContentTypes) {
$ContentType = $null
if ($ContentTypes.Count -gt 1) {
$ContentType = SelectHeaders -Headers $ContentTypes
} else {
$ContentType = $ContentTypes[0]
}
if (IsJsonMIME -MIME $ContentType) { # JSON
return ConvertFrom-Json $Response
} else { # XML, file, etc
return $Response
}
} else { # no content type in response header, returning raw response
return $Response
}
}
}