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 Vs-Code functionality to generate http snippets #5268

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b95aea3
add basic functionality to generate http snippets
koros Aug 29, 2024
7504f5a
make function static
koros Aug 29, 2024
d904494
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Aug 29, 2024
a551861
format code
koros Aug 29, 2024
f9aeef5
move the http snippet generation to the 'generate' drop down menu
koros Sep 9, 2024
3b1007c
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 9, 2024
d8b4d12
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 10, 2024
2382c6f
delete only relevant files when generation type is http
koros Sep 10, 2024
65df5ba
skip code generation part for http snippet generation
koros Sep 10, 2024
5aed882
fix sonar warnings
koros Sep 12, 2024
c0d999d
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 12, 2024
d3981c4
add Request body to the http snippets
koros Sep 12, 2024
68b52d6
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 23, 2024
e5f4313
split http snippets to separate output files based on path segment
koros Sep 24, 2024
2ccad10
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 24, 2024
380953f
remove unused paramater
koros Sep 25, 2024
b098a65
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Sep 25, 2024
5c17d21
unit tests for HttpSnippetWriter
koros Sep 25, 2024
3a29bb2
format code
koros Sep 25, 2024
75e3b36
refactor http snippet generation logic
koros Sep 30, 2024
ec9ffb2
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Oct 1, 2024
c33d499
increase unit tests coverage
koros Oct 1, 2024
46f9a5c
format code
koros Oct 1, 2024
52f33d0
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Oct 1, 2024
82de350
add unit test for KiotaBuilder http snippet generation feature
koros Oct 2, 2024
d5f1913
use http generation config while testing
koros Oct 2, 2024
beaa4df
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros Oct 2, 2024
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
64 changes: 64 additions & 0 deletions src/Kiota.Builder/HttpSnippetGenerationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.Writers;
using Kiota.Builder.Writers.Go;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Writers;

namespace Kiota.Builder
{
public partial class HttpSnippetGenerationService
{
private readonly OpenApiDocument OAIDocument;
private readonly OpenApiUrlTreeNode TreeNode;
private readonly GenerationConfiguration Configuration;
private readonly string WorkingDirectory;

public HttpSnippetGenerationService(OpenApiDocument document, OpenApiUrlTreeNode openApiUrlTreeNode, GenerationConfiguration configuration, string workingDirectory)
{
ArgumentNullException.ThrowIfNull(document);
ArgumentNullException.ThrowIfNull(openApiUrlTreeNode);
ArgumentNullException.ThrowIfNull(configuration);
ArgumentException.ThrowIfNullOrEmpty(workingDirectory);
OAIDocument = document;
TreeNode = openApiUrlTreeNode;
Configuration = configuration;
WorkingDirectory = workingDirectory;
}

public async Task GenerateHttpSnippetAsync(CancellationToken cancellationToken = default)
{
var descriptionRelativePath = "index.http";
var descriptionFullPath = Path.Combine(Configuration.OutputPath, descriptionRelativePath);
var directory = Path.GetDirectoryName(descriptionFullPath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Directory.CreateDirectory(directory);

#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var descriptionStream = File.Create(descriptionFullPath, 4096);
await using var fileWriter = new StreamWriter(descriptionStream);
var serverUrl = ExtractServerUrl(OAIDocument);
await fileWriter.WriteLineAsync($"# Http snippet for {serverUrl}");
await fileWriter.WriteLineAsync($"@url = {serverUrl}");
await fileWriter.WriteLineAsync();
var httpSnippetWriter = new HttpSnippetWriter(fileWriter);
httpSnippetWriter.Write(TreeNode);
httpSnippetWriter.Flush();
await fileWriter.FlushAsync(cancellationToken);
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
}

private string? ExtractServerUrl(OpenApiDocument document)
{
return document.Servers?.FirstOrDefault()?.Url;
}
}
}
15 changes: 15 additions & 0 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ public async Task<bool> GeneratePluginAsync(CancellationToken cancellationToken)
}, cancellationToken).ConfigureAwait(false);
}

public async Task<bool> GenerateHttpSnippetAsync(CancellationToken cancellationToken)
{
return await GenerateConsumerAsync(async (sw, stepId, openApiTree, CancellationToken) =>
{
if (openApiDocument is null || openApiTree is null)
throw new InvalidOperationException("The OpenAPI document and the URL tree must be loaded before generating the http snippet");
// generate plugin
sw.Start();
var service = new HttpSnippetGenerationService(openApiDocument, openApiTree, config, Directory.GetCurrentDirectory());
await service.GenerateHttpSnippetAsync(cancellationToken).ConfigureAwait(false);
StopLogAndReset(sw, $"step {++stepId} - generate plugin - took");
return stepId;
}, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Generates the code from the OpenAPI document
/// </summary>
Expand Down
122 changes: 122 additions & 0 deletions src/Kiota.Builder/Writers/HttpSnippetWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.IO;
using Kiota.Builder.Writers.Go;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;

namespace Kiota.Builder.Writers
{
internal class HttpSnippetWriter
{
/// <summary>
/// The text writer.
/// </summary>
protected TextWriter Writer
{
get;
}

public HttpSnippetWriter(TextWriter writer)
{
Writer = writer;
}

/// <summary>
/// Writes the given OpenAPI URL tree node to the writer.
/// This includes writing all path items and their children.
/// </summary>
/// <param name="node">The OpenAPI URL tree node to write.</param>
public void Write(OpenApiUrlTreeNode node)
{
WritePathItems(node);
WriteChildren(node);
}

/// <summary>
/// Writes all the path items for the given OpenAPI URL tree node to the writer.
/// Each path item is processed by calling the <see cref="WriteOpenApiPathItemOperation"/> method.
/// </summary>
/// <param name="node">The OpenAPI URL tree node containing the path items to write.</param>
private void WritePathItems(OpenApiUrlTreeNode node)
{
// Write all the path items
foreach (var item in node.PathItems)
{
WriteOpenApiPathItemOperation(item.Value, node);
}
}

/// <summary>
/// Writes the children of the given OpenAPI URL tree node to the writer.
/// Each child node is processed by calling the <see cref="Write"/> method.
/// </summary>
/// <param name="node">The OpenAPI URL tree node whose children are to be written.</param>
private void WriteChildren(OpenApiUrlTreeNode node)
{
foreach (var item in node.Children)
{
Write(item.Value);
}
}

/// <summary>
/// Writes the operations for the given OpenAPI path item to the writer.
/// Each operation includes the HTTP method, sanitized path, parameters, and a formatted HTTP request line.
/// </summary>
/// <param name="pathItem">The OpenAPI path item containing the operations to write.</param>
/// <param name="node">The OpenAPI URL tree node representing the path.</param>
private void WriteOpenApiPathItemOperation(OpenApiPathItem pathItem, OpenApiUrlTreeNode node)
{
// Write the operation
foreach (var item in pathItem.Operations)
{
var path = SanitizePath(node.Path);
var operation = item.Key.ToString().ToUpperInvariant();

Writer.WriteLine($"### {operation} {path}");
// write the parameters
WriteParameters(item.Value.Parameters);
Writer.WriteLine($"{operation} {{{{url}}}}{path} HTTP/1.1");
Writer.WriteLine();
}
}

/// <summary>
/// Sanitizes the given path by replacing '\\' with '/' and '\' with '/'.
/// Also converts '{foo}' into '{{foo}}' so that they can be used as variables in the HTTP snippet.
/// </summary>
/// <param name="path">The path to sanitize.</param>
/// <returns>The sanitized path.</returns>
private static string SanitizePath(string path)
{
return path.Replace("\\\\", "/", StringComparison.OrdinalIgnoreCase)
.Replace("\\", "/", StringComparison.OrdinalIgnoreCase)
.Replace("{", "{{", StringComparison.OrdinalIgnoreCase)
.Replace("}", "}}", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Writes the given list of OpenAPI parameters to the writer.
/// Each parameter's description and example value are written as comments and variable assignments, respectively.
/// </summary>
/// <param name="parameters">The list of OpenAPI parameters to write.</param>
private void WriteParameters(IList<OpenApiParameter> parameters)
{
foreach (var parameter in parameters)
{
Writer.WriteLine($"# {parameter.Description}");
Writer.WriteLine($"@{parameter.Name} = {parameter.Example}");
}
}

/// <summary>
/// Flush the writer.
/// </summary>
public void Flush()
{
Writer.Flush();
}

}
}
2 changes: 2 additions & 0 deletions src/kiota/Rpc/IServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ internal interface IServer
Task<List<LogEntry>> GenerateAsync(string openAPIFilePath, string outputPath, GenerationLanguage language, string[] includePatterns, string[] excludePatterns, string clientClassName, string clientNamespaceName, bool usesBackingStore, bool cleanOutput, bool clearCache, bool excludeBackwardCompatible, string[] disabledValidationRules, string[] serializers, string[] deserializers, string[] structuredMimeTypes, bool includeAdditionalData, CancellationToken cancellationToken);
Task<LanguagesInformation> InfoForDescriptionAsync(string descriptionPath, bool clearCache, CancellationToken cancellationToken);
Task<List<LogEntry>> GeneratePluginAsync(string openAPIFilePath, string outputPath, PluginType[] pluginTypes, string[] includePatterns, string[] excludePatterns, string clientClassName, bool cleanOutput, bool clearCache, string[] disabledValidationRules, CancellationToken cancellationToken);
Task<List<LogEntry>> GenerateHttpSnippetAsync(string openAPIFilePath, string outputPath, string[] includePatterns, string[] excludePatterns, bool cleanOutput, bool clearCache, bool excludeBackwardCompatible, string[] disabledValidationRules, string[] structuredMimeTypes, CancellationToken cancellationToken);

}
31 changes: 31 additions & 0 deletions src/kiota/Rpc/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,35 @@ private static string NormalizeSlashesInPath(string path)
return path.Replace('/', '\\');
return path.Replace('\\', '/');
}

public async Task<List<LogEntry>> GenerateHttpSnippetAsync(string openAPIFilePath, string outputPath, string[] includePatterns, string[] excludePatterns, bool cleanOutput, bool clearCache, bool excludeBackwardCompatible, string[] disabledValidationRules, string[] structuredMimeTypes, CancellationToken cancellationToken)
{
var globalLogger = new ForwardedLogger<KiotaBuilder>();
var configuration = Configuration.Generation;
configuration.OpenAPIFilePath = GetAbsolutePath(openAPIFilePath);
configuration.OutputPath = GetAbsolutePath(outputPath);
configuration.CleanOutput = cleanOutput;
configuration.ClearCache = clearCache;
configuration.Operation = ConsumerOperation.Add; //TODO should be updated to edit in the edit scenario
if (disabledValidationRules is { Length: > 0 })
configuration.DisabledValidationRules = disabledValidationRules.ToHashSet(StringComparer.OrdinalIgnoreCase);
if (includePatterns is { Length: > 0 })
configuration.IncludePatterns = includePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase);
if (excludePatterns is { Length: > 0 })
configuration.ExcludePatterns = excludePatterns.Select(static x => x.TrimQuotes()).ToHashSet(StringComparer.OrdinalIgnoreCase);
configuration.OpenAPIFilePath = GetAbsolutePath(configuration.OpenAPIFilePath);
configuration.OutputPath = NormalizeSlashesInPath(GetAbsolutePath(configuration.OutputPath));
try
{
using var fileLogger = new FileLogLogger<KiotaBuilder>(configuration.OutputPath, LogLevel.Warning);
var logger = new AggregateLogger<KiotaBuilder>(globalLogger, fileLogger);
await new KiotaBuilder(logger, configuration, httpClient, IsConfigPreviewEnabled.Value).GenerateHttpSnippetAsync(cancellationToken);
}
catch (Exception ex)
{
globalLogger.LogCritical(ex, "error adding the client: {exceptionMessage}", ex.Message);
}
return globalLogger.LogEntries;
}

}
11 changes: 11 additions & 0 deletions vscode/microsoft-kiota/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
"command": "kiota.openApiExplorer.removeAllFromSelectedEndpoints",
"when": "view == kiota.openApiExplorer",
"group": "inline@5"
},
{
"command": "kiota.openApiExplorer.generateHttpSnippet",
"when": "view == kiota.openApiExplorer",
"group": "inline@6"
}
],
"commandPalette": [
Expand Down Expand Up @@ -408,6 +413,12 @@
"category": "Kiota",
"title": "%kiota.openApiExplorer.openDescription.title%",
"icon": "$(go-to-file)"
},
{
"command": "kiota.openApiExplorer.generateHttpSnippet",
"category": "Kiota",
"title": "Generate http snippets",
"icon": "$(debug-alt)"
}
]
},
Expand Down
74 changes: 73 additions & 1 deletion vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
LogLevel,
parseGenerationLanguage,
} from "./kiotaInterop";
import { filterSteps, generateSteps, openManifestSteps, openSteps, searchLockSteps, searchSteps, selectApiManifestKey } from "./steps";
import { filterSteps, generateHttpSnippetsSteps, generateSteps, openManifestSteps, openSteps, searchLockSteps, searchSteps, selectApiManifestKey } from "./steps";
import { getKiotaVersion } from "./getKiotaVersion";
import { searchDescription } from "./searchDescription";
import { generateClient } from "./generateClient";
Expand All @@ -22,6 +22,7 @@ import { DependenciesViewProvider } from "./dependenciesViewProvider";
import { updateClients } from "./updateClients";
import { ApiManifest } from "./apiManifest";
import { getExtensionSettings } from "./extensionSettings";
import { generateHttpSnippet } from "./generateHttpSnippet";

let kiotaStatusBarItem: vscode.StatusBarItem;
let kiotaOutputChannel: vscode.LogOutputChannel;
Expand Down Expand Up @@ -242,6 +243,77 @@ export async function activate(
}
}
),
registerCommandWithTelemetry(reporter,
`${treeViewId}.generateHttpSnippet`,
async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is new code, and there's planned refactoring, it would be safe to move the functionality of this command out of the extension.ts file.

The command can follow the abstract command class as defined in this PR #5160

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thewahome I can wait for yours to get merged first to avoid increasing the conflict delta, if we both refactor at the same time, the conflict will be greater down the line

Copy link
Contributor

Choose a reason for hiding this comment

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

It is likely not going to get merged because of how much the code has changed since freezing it. It's going to be maintained as a reference.

const selectedPaths = openApiTreeProvider.getSelectedPaths();
if (selectedPaths.length === 0) {
await vscode.window.showErrorMessage(
vscode.l10n.t("No endpoints selected, select endpoints first")
);
return;
}
if (
!vscode.workspace.workspaceFolders ||
vscode.workspace.workspaceFolders.length === 0
) {
await vscode.window.showErrorMessage(
vscode.l10n.t("No workspace folder found, open a folder first")
);
return;
}
const config = await generateHttpSnippetsSteps(
{
outputPath: openApiTreeProvider.outputPath,
}
);
const outputPath = typeof config.outputPath === "string"
? config.outputPath
: "./output";
if (!openApiTreeProvider.descriptionUrl) {
await vscode.window.showErrorMessage(
vscode.l10n.t("No description found, select a description first")
);
return;
}
const settings = getExtensionSettings(extensionId);
const result = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Generating client...")
}, async (progress, _) => {
const start = performance.now();
const result = await generateHttpSnippet(
context,
openApiTreeProvider.descriptionUrl,
outputPath,
selectedPaths,
[],
settings.clearCache,
settings.cleanOutput,
settings.excludeBackwardCompatible,
settings.disableValidationRules,
settings.structuredMimeTypes
);
const duration = performance.now() - start;
const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0;
reporter.sendRawTelemetryEvent(`${extensionId}.generateHttpSnippet.completed`, {
"errorsCount": errorsCount.toString(),
}, {
"duration": duration,
});

// open the http snippet file
if (result && getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length === 0) {
const httpSnippetFilePath = path.join(outputPath, "index.http");
if (fs.existsSync(httpSnippetFilePath)) {
await vscode.window.showTextDocument(vscode.Uri.file(httpSnippetFilePath));
}
}
return result;
});
}
),
registerCommandWithTelemetry(reporter,
`${extensionId}.searchApiDescription`,
async () => {
Expand Down
Loading