Repository navigation
Add Aspire recording commands via DevProxy API client #55
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
Merged
waldekmastykarz
merged 4 commits into
dev-proxy-tools:main
from
hansmbakker:feature/recording-commands
Nov 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
50d44fe
Add Aspire recording commands via DevProxy API client
hansmbakker 65f2956
Add authentication requirement to OpenAPI spec and client
hansmbakker 61e88f7
Update resources with API token behavior
hansmbakker dd80162
Simplify recording commands to use an AppHost-provided API token
waldekmastykarz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using System.Net; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Http.Json; | ||
| using System.Security.Cryptography; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using DevProxy.Hosting; | ||
|
|
||
| #pragma warning disable IDE0130 | ||
| namespace Aspire.Hosting; | ||
| #pragma warning restore IDE0130 | ||
|
|
||
| /// <summary> | ||
| /// Adds dashboard commands for controlling Dev Proxy through its API. | ||
| /// </summary> | ||
| internal static class DevProxyCommandExtensions | ||
| { | ||
| internal const string ApiTokenEnvironmentVariable = "DEV_PROXY_API_TOKEN"; | ||
|
|
||
| /// <summary> | ||
| /// Generates an API token for the Dev Proxy resource, passes it to Dev Proxy | ||
| /// and adds commands to start and stop recording. | ||
| /// </summary> | ||
| internal static IResourceBuilder<TResource> WithRecordingCommands<TResource>( | ||
| this IResourceBuilder<TResource> builder) | ||
| where TResource : IResourceWithEndpoints, IResourceWithEnvironment | ||
| { | ||
| var apiToken = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)); | ||
|
|
||
| builder.WithEnvironment(ApiTokenEnvironmentVariable, apiToken); | ||
|
|
||
| builder.WithCommand( | ||
| name: "start-recording", | ||
| displayName: "Start recording", | ||
| executeCommand: context => SetRecordingAsync(builder.Resource, apiToken, recording: true, context), | ||
| commandOptions: new CommandOptions | ||
| { | ||
| Description = "Start recording requests in Dev Proxy", | ||
| IconName = "Record", | ||
| UpdateState = UpdateCommandState | ||
| }); | ||
|
|
||
| builder.WithCommand( | ||
| name: "stop-recording", | ||
| displayName: "Stop recording", | ||
| executeCommand: context => SetRecordingAsync(builder.Resource, apiToken, recording: false, context), | ||
| commandOptions: new CommandOptions | ||
| { | ||
| Description = "Stop recording requests in Dev Proxy", | ||
| IconName = "RecordStop", | ||
| UpdateState = UpdateCommandState | ||
| }); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| private static ResourceCommandState UpdateCommandState(UpdateCommandStateContext context) => | ||
| context.ResourceSnapshot.State?.Text == KnownResourceStates.Running | ||
| ? ResourceCommandState.Enabled | ||
| : ResourceCommandState.Disabled; | ||
|
|
||
| private static async Task<ExecuteCommandResult> SetRecordingAsync( | ||
| IResourceWithEndpoints resource, | ||
| string apiToken, | ||
| bool recording, | ||
| ExecuteCommandContext context) | ||
| { | ||
| try | ||
| { | ||
| var endpointUrl = await resource | ||
| .GetEndpoint(DevProxyResource.ApiEndpointName) | ||
| .GetValueAsync(context.CancellationToken); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(endpointUrl)) | ||
| { | ||
| return CommandResults.Failure("The Dev Proxy API endpoint is not available."); | ||
| } | ||
|
|
||
| using var httpClient = new HttpClient(new HttpClientHandler { UseProxy = false }) | ||
| { | ||
| BaseAddress = new Uri(endpointUrl), | ||
| Timeout = TimeSpan.FromSeconds(10) | ||
| }; | ||
| httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken); | ||
|
|
||
| using var response = await httpClient.PostAsJsonAsync( | ||
| "/proxy", | ||
| new DevProxyUpdate { Recording = recording }, | ||
| context.CancellationToken); | ||
|
|
||
| if (response.StatusCode == HttpStatusCode.Unauthorized) | ||
| { | ||
| return CommandResults.Failure( | ||
| $"Dev Proxy rejected the API token. Make sure you're using a version of Dev Proxy that supports the {ApiTokenEnvironmentVariable} environment variable."); | ||
| } | ||
|
|
||
| if (!response.IsSuccessStatusCode) | ||
| { | ||
| var content = await response.Content.ReadAsStringAsync(context.CancellationToken); | ||
| return CommandResults.Failure( | ||
| $"Dev Proxy returned {(int)response.StatusCode} {response.ReasonPhrase}: {content}"); | ||
| } | ||
|
|
||
| return CommandResults.Success(recording ? "Recording started." : "Recording stopped."); | ||
| } | ||
| catch (HttpRequestException exception) | ||
| { | ||
| return CommandResults.Failure($"Could not reach the Dev Proxy API: {exception.Message}"); | ||
| } | ||
| catch (TaskCanceledException) when (!context.CancellationToken.IsCancellationRequested) | ||
| { | ||
| return CommandResults.Failure("The Dev Proxy API did not respond in time."); | ||
| } | ||
| } | ||
|
|
||
| private sealed class DevProxyUpdate | ||
| { | ||
| public bool? Recording { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.