-
Notifications
You must be signed in to change notification settings - Fork 203
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
koros
wants to merge
27
commits into
main
Choose a base branch
from
feature/vscode-extension/add-http-snippet-generation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 7504f5a
make function static
koros d904494
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros a551861
format code
koros f9aeef5
move the http snippet generation to the 'generate' drop down menu
koros 3b1007c
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros d8b4d12
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros 2382c6f
delete only relevant files when generation type is http
koros 65df5ba
skip code generation part for http snippet generation
koros 5aed882
fix sonar warnings
koros c0d999d
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros d3981c4
add Request body to the http snippets
koros 68b52d6
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros e5f4313
split http snippets to separate output files based on path segment
koros 2ccad10
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros 380953f
remove unused paramater
koros b098a65
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros 5c17d21
unit tests for HttpSnippetWriter
koros 3a29bb2
format code
koros 75e3b36
refactor http snippet generation logic
koros ec9ffb2
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros c33d499
increase unit tests coverage
koros 46f9a5c
format code
koros 52f33d0
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros 82de350
add unit test for KiotaBuilder http snippet generation feature
koros d5f1913
use http generation config while testing
koros beaa4df
Merge branch 'main' into feature/vscode-extension/add-http-snippet-ge…
koros 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 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,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; | ||
} | ||
} | ||
} |
This file contains 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 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,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(); | ||
} | ||
|
||
} | ||
} |
This file contains 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 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 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 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.