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

introduce the abstraction for tokencredential type and keycredential type #5231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
43847f1
refactor test cases
ArcturusZhang Dec 2, 2024
66d4083
rename and move back a test
ArcturusZhang Dec 3, 2024
7958346
clean up empty line
ArcturusZhang Dec 3, 2024
fbf3333
Merge remote-tracking branch 'origin/main' into separate-clientprovid…
ArcturusZhang Dec 3, 2024
77c6afe
move another test case
ArcturusZhang Dec 3, 2024
fa0f3f4
Merge branch 'main' into separate-clientprovidertests
ArcturusZhang Dec 3, 2024
725b8c4
Merge branch 'main' into separate-clientprovidertests
ArcturusZhang Dec 3, 2024
7a63acd
refactor test cases
ArcturusZhang Dec 4, 2024
8839bf1
change the order of test methods back
ArcturusZhang Dec 4, 2024
9c59c44
more effort to reduce the amount of changes
ArcturusZhang Dec 4, 2024
385c4b9
implement the feature of tokencredential
ArcturusZhang Dec 4, 2024
cf3a23e
refine those test cases
ArcturusZhang Dec 4, 2024
3f2275f
remove two deprecated test case classes
ArcturusZhang Dec 4, 2024
647fc57
recombine those two classes
ArcturusZhang Dec 4, 2024
e873771
rearrange the test methods
ArcturusZhang Dec 4, 2024
2b0c9ca
more rearrangement
ArcturusZhang Dec 4, 2024
19dad66
more rearrangement
ArcturusZhang Dec 4, 2024
3cde6e8
rename a class
ArcturusZhang Dec 4, 2024
84cc360
resolve comments
ArcturusZhang Dec 9, 2024
7b83443
resolve more comments
ArcturusZhang Dec 9, 2024
5fed955
move those utils back to clientprovidertests
ArcturusZhang Dec 9, 2024
c0c6140
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 9, 2024
02854c5
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 9, 2024
ef37aef
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 11, 2024
ce7b1ef
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 11, 2024
9329357
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 12, 2024
d13b4da
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 12, 2024
6f61a7e
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 12, 2024
ed8838d
fix problematic modifiers on fields
ArcturusZhang Dec 12, 2024
5184c60
fix test cases
ArcturusZhang Dec 12, 2024
73aaddd
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 13, 2024
8e025ce
fix issues in ctor body
ArcturusZhang Dec 13, 2024
631b9aa
add test cases to validate the body of ctors
ArcturusZhang Dec 13, 2024
8aaa45f
Merge branch 'main' into abstraction-for-credentials
ArcturusZhang Dec 16, 2024
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 @@ -30,8 +30,6 @@ internal static class ScmKnownParameters
public static readonly ParameterProvider Data = new("data", FormattableStringHelpers.Empty, typeof(BinaryData));
public static ParameterProvider ClientOptions(CSharpType clientOptionsType)
=> new("options", $"The options for configuring the client.", clientOptionsType.WithNullable(true), initializationValue: New.Instance(clientOptionsType.WithNullable(true)));
public static readonly ParameterProvider KeyAuth = new("keyCredential", $"The token credential to copy", ClientModelPlugin.Instance.TypeFactory.KeyCredentialType);
public static readonly ParameterProvider MatchConditionsParameter = new("matchConditions", $"The content to send as the request conditions of the request.", ClientModelPlugin.Instance.TypeFactory.MatchConditionsType, DefaultOf(ClientModelPlugin.Instance.TypeFactory.MatchConditionsType));
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
public static readonly ParameterProvider OptionalRequestOptions = new(
ClientModelPlugin.Instance.TypeFactory.HttpRequestOptionsApi.ParameterName,
$"The request options, which can override default behaviors of the client pipeline on a per-call basis.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract record ClientPipelineApi : ScopedApi, IClientPipelineApi
public abstract CSharpType ClientPipelineType { get; }
public abstract CSharpType ClientPipelineOptionsType { get; }
public abstract CSharpType PipelinePolicyType { get; }
public abstract CSharpType? KeyCredentialType { get; }
public abstract CSharpType? TokenCredentialType { get; }

protected ClientPipelineApi(Type type, ValueExpression original) : base(type, original)
{
Expand All @@ -26,7 +28,8 @@ protected ClientPipelineApi(Type type, ValueExpression original) : base(type, or

public abstract ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies);

public abstract ValueExpression AuthorizationPolicy(params ValueExpression[] arguments);
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
public abstract ValueExpression KeyAuthorizationPolicy(ValueExpression credential, ValueExpression headerName, ValueExpression? keyPrefix = null);
public abstract ValueExpression TokenAuthorizationPolicy(ValueExpression credential, ValueExpression scopes);
public abstract ClientPipelineApi FromExpression(ValueExpression expression);
public abstract ClientPipelineApi ToExpression();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public interface IClientPipelineApi : IExpressionApi<ClientPipelineApi>
CSharpType ClientPipelineType { get; }
CSharpType ClientPipelineOptionsType { get; }
CSharpType PipelinePolicyType { get; }

CSharpType? KeyCredentialType { get; }
CSharpType? TokenCredentialType { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Statements;
using Microsoft.Generator.CSharp.Snippets;
using Microsoft.Generator.CSharp.Statements;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Microsoft.Generator.CSharp.ClientModel.Providers
Expand All @@ -25,6 +27,10 @@ public ClientPipelineProvider(ValueExpression original) : base(typeof(ClientPipe

public override CSharpType PipelinePolicyType => typeof(PipelinePolicy);

public override CSharpType KeyCredentialType => typeof(ApiKeyCredential);

public override CSharpType? TokenCredentialType => null; // Scm library does not support token credentials yet.

public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Static<ClientPipeline>().Invoke(nameof(ClientPipeline.Create), [options, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType), perRetryPolicies, New.Array(ClientModelPlugin.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType)]).As<ClientPipeline>();

Expand All @@ -34,8 +40,18 @@ public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptio
public override ClientPipelineApi FromExpression(ValueExpression expression)
=> new ClientPipelineProvider(expression);

public override ValueExpression AuthorizationPolicy(params ValueExpression[] arguments)
=> Static<ApiKeyAuthenticationPolicy>().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As<ApiKeyAuthenticationPolicy>();
public override ValueExpression KeyAuthorizationPolicy(ValueExpression credential, ValueExpression headerName, ValueExpression? keyPrefix = null)
{
ValueExpression[] arguments = keyPrefix == null ? [credential, headerName] : [credential, headerName, keyPrefix];
return Static<ApiKeyAuthenticationPolicy>().Invoke(nameof(ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy), arguments).As<ApiKeyAuthenticationPolicy>();
}

public override ValueExpression TokenAuthorizationPolicy(ValueExpression credential, ValueExpression scopes)
{
// Scm library does not support token credentials yet. The throw here is intentional.
// For a plugin that supports token credentials, they could override this implementation as well as the above TokenCredentialType property.
throw new NotImplementedException();
}

public override ClientPipelineApi ToExpression() => this;

Expand Down
Loading
Loading