Skip to content

Commit

Permalink
feat: Added interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 15, 2024
1 parent cf4d948 commit 7d814da
Show file tree
Hide file tree
Showing 5,026 changed files with 149,924 additions and 505 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private static async Task HandleAsync(
.Select(x => Sources.Class(x)))
.Concat(data.Methods
.Select(x => Sources.Method(x)))
.Concat(data.Methods
.Select(x => Sources.MethodInterface(x)))
.Concat(data.Authorizations
.Select(x => Sources.Authorization(x)))
.Concat([Sources.MainAuthorizationConstructor(data.Authorizations)])
Expand Down
5 changes: 5 additions & 0 deletions src/libs/AutoSDK.SourceGenerators/SdkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectAndReportExceptions((x, c) => Sources.Method(x, c)
.AsFileWithName(), context, Id)
.AddSource(context);
data
.SelectMany(static (x, _) => x.Methods)
.SelectAndReportExceptions((x, c) => Sources.MethodInterface(x, c)
.AsFileWithName(), context, Id)
.AddSource(context);
data
.SelectMany(static (x, _) => x.Authorizations)
.SelectAndReportExceptions((x, c) => Sources.Authorization(x, c)
Expand Down
4 changes: 4 additions & 0 deletions src/libs/AutoSDK/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ ImmutableArray<string> Converters
? $"{Namespace}.{ClassName}"
: $"{Namespace}.{ClassName}.{Id.ToPropertyName()}";

public string InterfaceFileNameWithoutExtension => string.IsNullOrWhiteSpace(Path)
? $"{Namespace}.I{ClassName}"
: $"{Namespace}.I{ClassName}.{Id.ToPropertyName()}";

public static EndPoint FromSchema(OperationContext operation)
{
operation = operation ?? throw new ArgumentNullException(nameof(operation));
Expand Down
34 changes: 33 additions & 1 deletion src/libs/AutoSDK/Sources/Sources.Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static string GenerateConstructors(
namespace {endPoint.Namespace}
{{
{(endPoint.Summary + "\nIf no httpClient is provided, a new one will be created.\nIf no baseUri is provided, the default baseUri from OpenAPI spec will be used.").ToXmlDocumentationSummary()}
public sealed partial class {endPoint.ClassName} : global::System.IDisposable
public sealed partial class {endPoint.ClassName} : global::{endPoint.Namespace}.I{endPoint.ClassName}, global::System.IDisposable
{{
{endPoint.BaseUrlSummary.ToXmlDocumentationSummary(level: 8)}
public const string BaseUrl = ""{endPoint.BaseUrl}"";
Expand Down Expand Up @@ -84,4 +84,36 @@ partial void ProcessResponseContent(
}}
}}".RemoveBlankLinesWhereOnlyWhitespaces();
}


public static string GenerateInterfaces(
EndPoint endPoint)
{
var serializer = endPoint.Settings.JsonSerializerType.GetSerializer();
var hasOptions = string.IsNullOrWhiteSpace(endPoint.Settings.JsonSerializerContext);

return $@"
#nullable enable
namespace {endPoint.Namespace}
{{
{(endPoint.Summary + "\nIf no httpClient is provided, a new one will be created.\nIf no baseUri is provided, the default baseUri from OpenAPI spec will be used.").ToXmlDocumentationSummary()}
public partial interface I{endPoint.ClassName} : global::System.IDisposable
{{
{endPoint.BaseUrlSummary.ToXmlDocumentationSummary(level: 8)}
public const string BaseUrl = ""{endPoint.BaseUrl}"";
{string.Empty.ToXmlDocumentationSummary(level: 8)}
{(hasOptions ? $@"
{serializer.GetOptionsType()} JsonSerializerOptions {{ get; set; }}" : $@"
global::System.Text.Json.Serialization.JsonSerializerContext JsonSerializerContext {{ get; set; }}")}
{(endPoint.Properties.Length != 0 ? "\n" + endPoint.Properties.Select(x => $@"
{x.Summary.ToXmlDocumentationSummary(level: 8)}
public {x.Type.CSharpType} {x.Name} {{ get; }}
").Inject() : " ")}
}}
}}".RemoveBlankLinesWhereOnlyWhitespaces();
}
}
103 changes: 65 additions & 38 deletions src/libs/AutoSDK/Sources/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ public partial class {endPoint.ClassName}
}}".RemoveBlankLinesWhereOnlyWhitespaces();
}

public static string GenerateEndPointInterface(
EndPoint endPoint,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(endPoint.Path))
{
return GenerateInterfaces(endPoint);
}

return $@"#nullable enable
namespace {endPoint.Namespace}
{{
public partial interface I{endPoint.ClassName}
{{
{GenerateMethod(endPoint, isInterface: true)}
{GenerateExtensionMethod(endPoint, isInterface: true)}
}}
}}".RemoveBlankLinesWhereOnlyWhitespaces();
}

public static string GetHttpMethod(string targetFramework, OperationType operationType)
{
targetFramework = targetFramework ?? throw new ArgumentNullException(nameof(targetFramework));
Expand All @@ -81,7 +102,7 @@ public static string GetHttpMethod(string targetFramework, OperationType operati
}

public static string GenerateMethod(
EndPoint endPoint)
EndPoint endPoint, bool isInterface = false)
{
var jsonSerializer = endPoint.Settings.JsonSerializerType.GetSerializer();
var taskType = endPoint.Stream
Expand All @@ -94,7 +115,7 @@ public static string GenerateMethod(
var httpCompletionOption = endPoint.Stream
? nameof(HttpCompletionOption.ResponseHeadersRead)
: nameof(HttpCompletionOption.ResponseContentRead);
var cancellationTokenAttribute = endPoint.Stream
var cancellationTokenAttribute = endPoint.Stream && !isInterface
? "[global::System.Runtime.CompilerServices.EnumeratorCancellation] "
: string.Empty;
var cancellationTokenInsideReadAsync = endPoint.Settings.TargetFramework.StartsWith("net8", StringComparison.OrdinalIgnoreCase)
Expand All @@ -106,24 +127,9 @@ public static string GenerateMethod(
ContentType.Stream => "Stream",
_ => "ByteArray",
};

return $@"
{endPoint.Summary.ToXmlDocumentationSummary(level: 8)}
{endPoint.Properties.Where(x => x.ParameterLocation != null).Select(x => $@"
{x.Summary.ToXmlDocumentationForParam(x.ParameterName, level: 8)}").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @"
/// <param name=""request""></param>")}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
{(endPoint.IsDeprecated ? "[global::System.Obsolete(\"This method marked as deprecated.\")]" : " ")}
public async {taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(x => x is { ParameterLocation: not null, IsRequired: true }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @$"
{endPoint.RequestType.CSharpTypeWithoutNullability} request,")}
{endPoint.Properties.Where(x => x is { ParameterLocation: not null, IsRequired: false }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName} = {x.ParameterDefaultValue},").Inject()}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default)
var body = isInterface
? ";"
: @$"
{{
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) || endPoint.RequestType.IsAnyOf ? " " : @"
request = request ?? throw new global::System.ArgumentNullException(nameof(request));
Expand Down Expand Up @@ -219,7 +225,25 @@ public static string GenerateMethod(
yield return streamedResponse;
}}" : " ")}
}}
}}";

return $@"
{endPoint.Summary.ToXmlDocumentationSummary(level: 8)}
{endPoint.Properties.Where(x => x.ParameterLocation != null).Select(x => $@"
{x.Summary.ToXmlDocumentationForParam(x.ParameterName, level: 8)}").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @"
/// <param name=""request""></param>")}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
{(endPoint.IsDeprecated ? "[global::System.Obsolete(\"This method marked as deprecated.\")]" : " ")}
{(isInterface ? "" : "public async ")}{taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(x => x is { ParameterLocation: not null, IsRequired: true }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @$"
{endPoint.RequestType.CSharpTypeWithoutNullability} request,")}
{endPoint.Properties.Where(x => x is { ParameterLocation: not null, IsRequired: false }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName} = {x.ParameterDefaultValue},").Inject()}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default){body}
".RemoveBlankLinesWhereOnlyWhitespaces();
}

Expand Down Expand Up @@ -311,7 +335,7 @@ public static string GenerateRequestData(
}

public static string GenerateExtensionMethod(
EndPoint endPoint)
EndPoint endPoint, bool isInterface = false)
{
if (string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ||
endPoint.RequestType.IsArray ||
Expand All @@ -329,7 +353,7 @@ public static string GenerateExtensionMethod(
: string.IsNullOrWhiteSpace(endPoint.ResponseType.CSharpType)
? "global::System.Threading.Tasks.Task"
: $"global::System.Threading.Tasks.Task<{endPoint.ResponseType.CSharpTypeWithoutNullability}>";
var cancellationTokenAttribute = endPoint.Stream
var cancellationTokenAttribute = endPoint.Stream && !isInterface
? "[global::System.Runtime.CompilerServices.EnumeratorCancellation] "
: string.Empty;
var response = endPoint.Stream
Expand All @@ -340,20 +364,9 @@ public static string GenerateExtensionMethod(
var configureAwaitResponse = !endPoint.Stream
? ".ConfigureAwait(false)"
: string.Empty;

return $@"
{endPoint.Summary.ToXmlDocumentationSummary(level: 8)}
{endPoint.Properties.Where(static x => x is { IsDeprecated: false } or { IsRequired:true }).Select(x => $@"
{x.Summary.ToXmlDocumentationForParam(x.ParameterName, level: 8)}").Inject()}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
{(endPoint.IsDeprecated ? "[global::System.Obsolete(\"This method marked as deprecated.\")]" : " ")}
public async {taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(static x => x.IsRequired).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{endPoint.Properties.Where(static x => x is { IsRequired: false, IsDeprecated: false }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName} = {x.ParameterDefaultValue},").Inject()}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default)
var body = isInterface
? ";"
: @$"
{{
var request = new {endPoint.RequestType.CSharpTypeWithoutNullability}
{{
Expand All @@ -372,7 +385,21 @@ public static string GenerateExtensionMethod(
{
yield return response;
}" : " ")}
}}
}}";

return $@"
{endPoint.Summary.ToXmlDocumentationSummary(level: 8)}
{endPoint.Properties.Where(static x => x is { IsDeprecated: false } or { IsRequired:true }).Select(x => $@"
{x.Summary.ToXmlDocumentationForParam(x.ParameterName, level: 8)}").Inject()}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
{(endPoint.IsDeprecated ? "[global::System.Obsolete(\"This method marked as deprecated.\")]" : " ")}
{(isInterface ? "" : "public async ")}{taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(static x => x.IsRequired).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{endPoint.Properties.Where(static x => x is { IsRequired: false, IsDeprecated: false }).Select(x => $@"
{x.Type.CSharpType} {x.ParameterName} = {x.ParameterDefaultValue},").Inject()}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default){body}
".RemoveBlankLinesWhereOnlyWhitespaces();
}
}
9 changes: 9 additions & 0 deletions src/libs/AutoSDK/Sources/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public static FileWithName Method(
Text: GenerateEndPoint(endPoint, cancellationToken: cancellationToken));
}

public static FileWithName MethodInterface(
EndPoint endPoint,
CancellationToken cancellationToken = default)
{
return new FileWithName(
Name: $"{endPoint.InterfaceFileNameWithoutExtension}.g.cs",
Text: GenerateEndPointInterface(endPoint, cancellationToken: cancellationToken));
}

public static FileWithName Authorization(
Authorization authorization,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class Api : global::System.IDisposable
public sealed partial class Api : global::G.IApi, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class ChatClient : global::System.IDisposable
public sealed partial class ChatClient : global::G.IChatClient, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class CompletionClient : global::System.IDisposable
public sealed partial class CompletionClient : global::G.ICompletionClient, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class CustomModelsClient : global::System.IDisposable
public sealed partial class CustomModelsClient : global::G.ICustomModelsClient, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class DatasetsClient : global::System.IDisposable
public sealed partial class DatasetsClient : global::G.IDatasetsClient, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace G
/// If no httpClient is provided, a new one will be created.<br/>
/// If no baseUri is provided, the default baseUri from OpenAPI spec will be used.
/// </summary>
public sealed partial class GrammaticalErrorCorrectionsClient : global::System.IDisposable
public sealed partial class GrammaticalErrorCorrectionsClient : global::G.IGrammaticalErrorCorrectionsClient, global::System.IDisposable
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//HintName: G.IApi.ConvertDocumentFileStudioV1ChatFilesConvertPost.g.cs
#nullable enable

namespace G
{
public partial interface IApi
{
/// <summary>
/// Convert Document File
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
global::System.Threading.Tasks.Task<global::G.ConvertDocumentFileStudioV1ChatFilesConvertPostResponse> ConvertDocumentFileStudioV1ChatFilesConvertPostAsync(
global::G.BodyConvertDocumentFileStudioV1ChatFilesConvertPost request,
global::System.Threading.CancellationToken cancellationToken = default);

/// <summary>
/// Convert Document File
/// </summary>
/// <param name="files"></param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
global::System.Threading.Tasks.Task<global::G.ConvertDocumentFileStudioV1ChatFilesConvertPostResponse> ConvertDocumentFileStudioV1ChatFilesConvertPostAsync(
global::System.Collections.Generic.IList<byte[]> files,
global::System.Threading.CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//HintName: G.IApi.V1Answer.g.cs
#nullable enable

namespace G
{
public partial interface IApi
{
/// <summary>
/// Answer
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
global::System.Threading.Tasks.Task<global::G.V1AnswerResponse> V1AnswerAsync(
global::G.AnswerBody request,
global::System.Threading.CancellationToken cancellationToken = default);

/// <summary>
/// Answer
/// </summary>
/// <param name="context"></param>
/// <param name="question"></param>
/// <param name="answerLength">
/// An enumeration.
/// </param>
/// <param name="mode">
/// An enumeration.
/// </param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
global::System.Threading.Tasks.Task<global::G.V1AnswerResponse> V1AnswerAsync(
string context,
string question,
global::G.AnswerLength? answerLength = default,
global::G.Mode? mode = default,
global::System.Threading.CancellationToken cancellationToken = default);
}
}
Loading

0 comments on commit 7d814da

Please sign in to comment.