Skip to content

Commit 4eb82da

Browse files
authored
Update the example and fix two bugs in worker (#34)
1. Add a check to see if there is anything returned before getting the last item. 2. When deserializing JSON, return a case-insensitive Hashtable instance, so `<blah>.Name` works for both `"name": "Joe"` and `"Name": "Joe"`.
1 parent d16d1a9 commit 4eb82da

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
# Invoked with Invoke-RestMethod:
2-
# irm http://localhost:7071/api/MyHttpTrigger?Name=Tyler
3-
# Input bindings are added via param block
1+
# Trigger the function by running Invoke-RestMethod:
2+
# (via get method): Invoke-RestMethod -Uri http://localhost:7071/api/MyHttpTrigger?Name=Joe
3+
# (via post method): Invoke-RestMethod `
4+
# -Uri http://localhost:7071/api/MyHttpTrigger `
5+
# -Method Post `
6+
# -Body (ConvertTo-Json @{ Name="Joe" }) `
7+
# -Headers @{'Content-Type' = 'application/json' }`
48

9+
# Input bindings are passed in via param block.
510
param($req, $TriggerMetadata)
611

7-
# If no name was passed by query parameter
8-
$name = 'World'
12+
# You can write to the Azure Functions log streams as you would in a normal PowerShell script.
13+
Write-Verbose "PowerShell HTTP trigger function processed a request." -Verbose
914

1015
# You can interact with query parameters, the body of the request, etc.
11-
if($req.Query.Name) {
12-
$name = $req.Query.Name
13-
}
14-
15-
# you can write to the same streams as you would in a normal PowerShell script
16-
Write-Verbose "Verbose $name" -Verbose
17-
Write-Warning "Warning $name"
16+
$name = $req.Query.Name
17+
if (-not $name) { $name = $req.Body.Name }
1818

19-
# items in the pipeline get logged
20-
$name
19+
if($name) {
20+
$status = 200
21+
$body = "Hello " + $name
22+
}
23+
else {
24+
$status = 400
25+
$body = "Please pass a name on the query string or in the request body."
26+
}
2127

22-
# You set the value of your output bindings by assignment `$nameOfOutputBinding = 'foo'`
28+
# You associate values to output bindings by calling 'Push-OutputBinding'.
2329
Push-OutputBinding -Name res -Value ([HttpResponseContext]@{
24-
Body = @{ Hello = $name }
25-
ContentType = 'application/json'
30+
StatusCode = $status
31+
Body = $body
2632
})
33+

src/PowerShell/PowerShellManager.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ internal Hashtable InvokeFunction(
113113
{
114114
// Log everything we received from the pipeline and set the last one to be the ReturnObject
115115
Collection<PSObject> pipelineItems = _pwsh.InvokeAndClearCommands<PSObject>();
116-
foreach (var psobject in pipelineItems)
116+
if (pipelineItems.Count > 0)
117117
{
118-
_logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}");
118+
foreach (var psobject in pipelineItems)
119+
{
120+
_logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}");
121+
}
122+
returnObject = pipelineItems[pipelineItems.Count - 1];
119123
}
120-
121-
returnObject = pipelineItems[pipelineItems.Count - 1];
122124
}
123125

124126
var result = _pwsh.AddCommand("Azure.Functions.PowerShell.Worker.Module\\Get-OutputBinding")

src/Utility/TypeExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public static object ToObject (this TypedData data)
7070
switch (data.DataCase)
7171
{
7272
case TypedData.DataOneofCase.Json:
73-
return JsonConvert.DeserializeObject<Hashtable>(data.Json);
73+
var hashtable = JsonConvert.DeserializeObject<Hashtable>(data.Json);
74+
return new Hashtable(hashtable, StringComparer.OrdinalIgnoreCase);
7475
case TypedData.DataOneofCase.Bytes:
7576
return data.Bytes.ToByteArray();
7677
case TypedData.DataOneofCase.Double:

0 commit comments

Comments
 (0)