-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.dib
74 lines (53 loc) · 2.25 KB
/
HTTP.dib
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
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"},{"aliases":[],"languageName":"HTTP","name":"http"}]}}
#!csharp
#r "nuget:Microsoft.DotNet.Interactive.HttpRequest, *-*"
#!markdown
### Add Kernel Event Handler to process HTTP response
#!csharp
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;
using System.Linq;
using System.Text.Json;
var responseBody = "";
// var httpKernelEvents = new List<KernelEvent>();
Kernel.Root.KernelEvents.Subscribe(e =>
{
// if(e.Command.TargetKernelName == "http")
// httpKernelEvents.Add(e);
if (e is DisplayedValueProduced de && e.Command.TargetKernelName == "http")
{
var dvp = de as DisplayedValueProduced;
var jsonResponse = dvp.FormattedValues.First(x=>x.MimeType == "application/json").Value.ToString();
var content = JsonDocument.Parse(jsonResponse).RootElement.GetProperty("Content").ToString();
var rawResponse = JsonDocument.Parse(content).RootElement.GetProperty("Raw").ToString();
responseBody = JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(rawResponse), new JsonSerializerOptions { WriteIndented = true });
if(dvp.Command is SubmitCode sc)
{
Kernel.Root.SendAsync(new SendEditableCode("markdown", $"### {sc.Code.Split("\n").First()}{Environment.NewLine}```json{Environment.NewLine}{responseBody}"));
}
else
{
Kernel.Root.SendAsync(new SendEditableCode("markdown", $"### Response{Environment.NewLine}```json{Environment.NewLine}{responseBody}"));
}
}
});
#!markdown
### Get Bearer Token from Azure CLI
#!pwsh
#!set --name url --value @input:"Please enter the Dataverse Environment URL"
#!pwsh
$token="Bearer $(az account get-access-token --resource=$url --query accessToken --output tsv)"
#!markdown
### Share Token and Url with HTTP Kernel
#!http
#!set --name token --value @pwsh:token --mime-type text/plain
#!set --name url --value @pwsh:url --mime-type text/plain
#!markdown
### Execute WhoAmI request
#!http
GET {{url}}/api/data/v9.2/systemusers?$select=fullname&$top=5
Accept: application/json
Authorization: {{token}}