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

added support for basic async APIs #46974

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;

namespace System.ClientModel.TypeSpec;
public static class TypeSpecWriter
Expand Down Expand Up @@ -104,7 +105,9 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS
{
string httpVerb = ReadHttpVerb(method);

writer.Write($"{httpVerb} @route(\"{ToCamel(method.Name)}\") {method.Name}(");
var methodName = method.Name;
if (methodName.EndsWith("Async")) methodName = methodName.Substring(0, methodName.Length - "Async".Length);
writer.Write($"{httpVerb} @route(\"{ToCamel(methodName)}\") {methodName}(");

bool first = true;
foreach (var parameter in method.GetParameters())
Expand Down Expand Up @@ -136,11 +139,16 @@ private static void WriteOperation(StreamWriter writer, MethodInfo method, HashS
}
writer.WriteLine(") : {");
writer.WriteLine($" @statusCode statusCode: 200;");
if (method.ReflectedType != typeof(void))
writer.WriteLine($" @body response : {method.ReturnType.ToTspType()};");

var returnType = method.ReturnType;
if (returnType == typeof(Task)) returnType = typeof(void);
else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
returnType = returnType.GetGenericArguments()[0];

writer.WriteLine($" @body response : {returnType.ToTspType()};");
writer.WriteLine(" };");
if (method.ReturnType.IsModel())
models.Add(method.ReturnType);
if (returnType.IsModel())
models.Add(returnType);
}

private static string ReadParameterLocation(ParameterInfo parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>12</LangVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using System.ClientModel.TypeSpec;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using NUnit.Framework;

namespace Azure.CloudMachine.Tests;

public class TdkTests
{
[Test]
public void GenerateIAssistantService()
{
MemoryStream stream = new();
TypeSpecWriter.WriteServer<IAssistantService>(stream);
stream.Position = 0;

BinaryData data = BinaryData.FromStream(stream);
Assert.AreEqual(IAssistantServiceTsp, data.ToString());
}

private static string IAssistantServiceTsp =
"""
import "@typespec/http";
import "@typespec/rest";
import "@azure-tools/typespec-client-generator-core";

@service({
title: "AssistantService",
})

namespace Azure.CloudMachine.Tests;

using TypeSpec.Http;
using TypeSpec.Rest;
using Azure.ClientGenerator.Core;

@client interface AssistantServiceClient {
@put @route("upload") Upload(@header contentType: "application/octet-stream", @body document: bytes) : {
@statusCode statusCode: 200;
@body response : void;
};
@get @route("send") Send(@query message: string) : {
@statusCode statusCode: 200;
@body response : string;
};
}


""";
}

internal interface IAssistantService
{
[HttpPut]
Task UploadAsync(HttpRequest document);
Task<string> SendAsync([FromQuery] string message);
}

internal class FromQueryAttribute : Attribute { }
internal class HttpPutAttribute : Attribute { }