Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for clearing variables from HttpKernel #3467

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@shyamnamboodiripad shyamnamboodiripad Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems generally useful. Any reason why this is not defined in the core Interactive assembly?

Currently the http editor does not (need to) directly reference the Interactive.Http assembly. Adding that reference may not be problematic - but still wanted to double check whether we can move this to the main assembly and avoid the additional dependency.

{
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 @@ This Kernel is able to execute http requests and display the results.
RegisterForDisposal(_client);
}

public Task HandleAsync(ClearValues command, KernelInvocationContext context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we implement this using explicit interface implementation and avoid the public API similar to what we do for the other commands below?

{
_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
Loading