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

[feature] implement client parameter apiversion #4206

Closed
chunyu3 opened this issue Aug 19, 2024 · 1 comment · Fixed by #4527
Closed

[feature] implement client parameter apiversion #4206

chunyu3 opened this issue Aug 19, 2024 · 1 comment · Fixed by #4527
Assignees
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Comments

@chunyu3
Copy link
Contributor

chunyu3 commented Aug 19, 2024

When the service is versioned, the client will has apiVersion field. Each operation which has apiVersion parameter should use the client apiVersion parameter directly and the operation signature should not include the apiVersion parameter.

@chunyu3 chunyu3 added emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp and removed needs-area labels Aug 19, 2024
@chunyu3
Copy link
Contributor Author

chunyu3 commented Aug 20, 2024

typespec:

@versioned(Versions)
@service({
    title: "hello world"
})
@doc("This is a sample typespec project.")
@server(
    "{firstTestTypeSpecUrl}",
    "Endpoint Service",
    {
        firstTestTypeSpecUrl: string,
    }
)
@useAuth(
    OAuth2Auth<[AuthFlow]> | ApiKeyAuth<ApiKeyLocation.header, "x-ms-api-key">
)
namespace FirstTestTypeSpec;

@doc("The auth flow model")
model AuthFlow {
    type: OAuth2FlowType.clientCredentials;
    tokenUrl: "https://api.example.com/oauth2/token";
    refreshUrl: "https://api.example.com/oauth2/refresh";
    scopes: ["https://api.example.com/.default"];
}

enum Versions {
    @useDependency(Azure.Core.Versions.v1_0_Preview_1)
    "2022-05-15-preview"
  }
@route("/helloWithApiVersion")
    @doc("Return hi again")
    @get
    @convenientAPI(true)
    op hello(
        @header p1: string,
        @query apiVersion: string
    ):void;

    @route("/helloWithOutApiVersion")
    @doc("Return hi again")
    @get
    @convenientAPI(true)
    op helloAgain(
        @header p1: string,
    ):void;

Generated Code:
THe apiVersion will be the Client parameter

public partial class FirstTestTypeSpecClient
    {
        private const string AuthorizationHeader = "x-ms-api-key";
        private readonly AzureKeyCredential _keyCredential;
        private static readonly string[] AuthorizationScopes = new string[] { "https://api.example.com/.default" };
        private readonly TokenCredential _tokenCredential;
        private readonly HttpPipeline _pipeline;
        private readonly Uri _endpoint;
        private readonly string _apiVersion;
      ......
}

The operation which has apiVersion parameter should use the client apiVersion parameter directly and the operation signature should not include the apiVersion parameter.

public virtual async Task<Response> HelloAsync(string p1, RequestContext context = null)
{

}
 internal HttpMessage CreateHelloRequest(string p1, RequestContext context)
        {
            var message = _pipeline.CreateMessage(context, ResponseClassifier204);
            var request = message.Request;
            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();
            uri.Reset(_endpoint);
            uri.AppendPath("/helloWithApiVersion", false);
            uri.AppendQuery("apiVersion", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("p1", p1);
            return message;
        }

@chunyu3 chunyu3 self-assigned this Sep 25, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 10, 2024
Fix #4206

- When the service is versioned, the client will has apiVersion field. 
- Each operation which has apiVersion parameter should use the client
apiVersion parameter directly and the operation signature should not
include the apiVersion parameter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant