Skip to content

Commit

Permalink
added support for basic async APIs (#46974)
Browse files Browse the repository at this point in the history
* added support for basic async APIs

* PR feedback

* removed dependency on deprecated package
  • Loading branch information
KrzysztofCwalina authored Nov 5, 2024
1 parent 9e4950e commit ea5cb40
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
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
67 changes: 67 additions & 0 deletions sdk/provisioning/Azure.Provisioning.CloudMachine/tests/TdkTests.cs
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 { }

0 comments on commit ea5cb40

Please sign in to comment.