-
Notifications
You must be signed in to change notification settings - Fork 368
/
KernelExtensions.cs
48 lines (40 loc) · 1.62 KB
/
KernelExtensions.cs
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Utility;
namespace Microsoft.DotNet.Interactive.Http;
public static class KernelExtensions
{
public static T UseHttpApi<T>(this T kernel, HttpPort httpPort, HttpProbingSettings httpProbingSettings)
where T : Kernel
{
var initApiCommand = new Command("#!enable-http")
{
IsHidden = true,
Handler = CommandHandler.Create((InvocationContext cmdLineContext) =>
{
var context = cmdLineContext.GetService<KernelInvocationContext>();
if (context.Command is SubmitCode)
{
var probingUrls = httpProbingSettings is not null
? httpProbingSettings.AddressList
: new[]
{
new Uri($"http://localhost:{httpPort}")
};
var html =
HttpApiBootstrapper.GetHtmlInjection(probingUrls, httpPort?.ToString() ?? Guid.NewGuid().ToString("N"));
context.Display(html, "text/html");
}
return Task.CompletedTask;
})
};
kernel.AddDirective(initApiCommand);
return kernel;
}
}