Skip to content

Commit

Permalink
add support for clearing variables from HttpKernel (#3467)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Feb 27, 2024
1 parent 0e90328 commit 3ba37fe
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Microsoft.DotNet.Interactive.Http
public class ClearValues : Microsoft.DotNet.Interactive.Commands.KernelCommand, System.IEquatable<Microsoft.DotNet.Interactive.Commands.KernelCommand>
.ctor(System.String targetKernelName = null)
public class EmptyHttpResponse
.ctor()
public class HttpContent
Expand All @@ -7,8 +9,9 @@ Microsoft.DotNet.Interactive.Http
public System.String ContentType { get;}
public System.Collections.Generic.Dictionary<System.String,System.String[]> Headers { get;}
public System.String Raw { get;}
public class HttpKernel : Microsoft.DotNet.Interactive.Kernel, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestDiagnostics>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestKernelInfo>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestValue>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestValueInfos>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.SendValue>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.SubmitCode>, System.IDisposable
public class HttpKernel : Microsoft.DotNet.Interactive.Kernel, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestDiagnostics>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestKernelInfo>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestValue>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.RequestValueInfos>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.SendValue>, Microsoft.DotNet.Interactive.IKernelCommandHandler<Microsoft.DotNet.Interactive.Commands.SubmitCode>, Microsoft.DotNet.Interactive.IKernelCommandHandler<ClearValues>, System.IDisposable
.ctor(System.String name = null, System.Net.Http.HttpClient client = null, System.Int32 responseDelayThresholdInMilliseconds = 1000, System.Int32 contentByteLengthThreshold = 500000)
public System.Threading.Tasks.Task HandleAsync(ClearValues command, Microsoft.DotNet.Interactive.KernelInvocationContext context)
public class HttpKernelExtension
public static System.Void Load(Microsoft.DotNet.Interactive.Kernel kernel, System.Net.Http.HttpClient httpClient = null, System.Int32 responseDelayThresholdInMilliseconds = 1000, System.Int32 contentByteLengthThreshold = 500000)
.ctor()
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.DotNet.Interactive.Http.Tests/HttpKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2231,4 +2231,26 @@ public async Task traceparent_header_has_a_new_top_level_value_for_each_request(
// the traceparent format looks like this: 00-d00689882649007396cd32ab75c2611c-59402ab4fd5c55f7-00
traceparent1.Single()[0..36].Should().NotBe(traceparent2.Single()[0..36]);
}

[Fact]
public async Task Variables_can_be_cleared()
{
using var kernel = new HttpKernel().UseValueSharing();

var result = await kernel.SendAsync(new SendValue("my_host", "my.host.com"));
result.Events.Should().NotContainErrors();

result = await kernel.SendAsync(new ClearValues());
result.Events.Should().NotContainErrors();

result = await kernel.SendAsync(new RequestValueInfos());

result.Events.Should().NotContainErrors();

result.Events.Should().ContainSingle<ValueInfosProduced>()
.Which
.ValueInfos
.Should()
.BeEmpty();
}
}
13 changes: 13 additions & 0 deletions src/Microsoft.DotNet.Interactive.Http/ClearValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 Microsoft.DotNet.Interactive.Commands;

namespace Microsoft.DotNet.Interactive.Http;

public class ClearValues : KernelCommand
{
public ClearValues(string? targetKernelName = null) : base(targetKernelName)
{
}
}
10 changes: 7 additions & 3 deletions src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -24,6 +21,7 @@ namespace Microsoft.DotNet.Interactive.Http;

public class HttpKernel :
Kernel,
IKernelCommandHandler<ClearValues>,
IKernelCommandHandler<RequestValue>,
IKernelCommandHandler<SendValue>,
IKernelCommandHandler<SubmitCode>,
Expand Down Expand Up @@ -58,6 +56,12 @@ public class HttpKernel :
RegisterForDisposal(_client);
}

public Task HandleAsync(ClearValues command, KernelInvocationContext context)
{
_variables.Clear();
return Task.CompletedTask;
}

Task IKernelCommandHandler<RequestValue>.HandleAsync(RequestValue command, KernelInvocationContext context)
{
if (_variables.TryGetValue(command.Name, out var value))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"token": "the-token",
"commandType": "ClearValues",
"command": {
"targetKernelName": null,
"originUri": null,
"destinationUri": null
},
"routingSlip": [
"kernel://somelocation/kernelName?tag=arrived",
"kernel://somelocation/kernelName"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Http;
using Microsoft.DotNet.Interactive.PowerShell;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Microsoft.DotNet.Interactive.ValueSharing;
using Pocket;
Expand Down Expand Up @@ -120,11 +124,20 @@ public void Event_contract_has_not_been_broken(KernelEvent @event)
[Fact]
public void All_command_types_are_tested_for_round_trip_serialization()
{
var interactiveCommands = typeof(Kernel)
.Assembly
.ExportedTypes
.Concrete()
.DerivedFrom(typeof(KernelCommand));
var commandTypes = new[]
{
typeof(Kernel),
typeof(CSharpKernel),
typeof(FSharpKernel),
typeof(HttpKernel),
typeof(PowerShellKernel),
}
.Select(t => t.Assembly)
.SelectMany(a => a.ExportedTypes);

var interactiveCommands = commandTypes
.Concrete()
.DerivedFrom(typeof(KernelCommand));

Commands()
.Select(e => e[0].GetType())
Expand Down Expand Up @@ -163,6 +176,8 @@ public static IEnumerable<object[]> Commands()

IEnumerable<KernelCommand> commands()
{
yield return new ClearValues();

yield return new DisplayError("oops!");

yield return new DisplayValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ProjectReference Include="..\Microsoft.DotNet.Interactive.CSharp\Microsoft.DotNet.Interactive.CSharp.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Formatting.Tests\Microsoft.DotNet.Interactive.Formatting.Tests.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.FSharp\Microsoft.DotNet.Interactive.FSharp.fsproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Http\Microsoft.DotNet.Interactive.Http.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.PowerShell\Microsoft.DotNet.Interactive.PowerShell.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Jupyter\Microsoft.DotNet.Interactive.Jupyter.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Formatting\Microsoft.DotNet.Interactive.Formatting.csproj" />
Expand Down

0 comments on commit 3ba37fe

Please sign in to comment.