Skip to content

Commit

Permalink
Auto discover policy handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mielek committed Nov 22, 2024
1 parent a765313 commit 85d3905
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 92 deletions.
61 changes: 16 additions & 45 deletions src/Core/Compiling/CSharpPolicyCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Reflection;
using System.Xml.Linq;

using Azure.ApiManagement.PolicyToolkit.Authoring;
Expand All @@ -22,51 +23,21 @@ public class CSharpPolicyCompiler
public CSharpPolicyCompiler(ClassDeclarationSyntax document)
{
_document = document;
var invStatement = new ExpressionStatementCompiler([
new AuthenticationBasicCompiler(),
new AuthenticationCertificateCompiler(),
new AuthenticationManagedIdentityCompiler(),
new BaseCompiler(),
new CacheLookupCompiler(),
new CacheLookupValueCompiler(),
new CacheStoreCompiler(),
new CacheStoreValueCompiler(),
new CacheRemoveValueCompiler(),
new CheckHeaderCompiler(),
new CorsCompiler(),
new ForwardRequestCompiler(),
new InlinePolicyCompiler(),
new IpFilterCompiler(),
new JsonToXmlCompiler(),
new JsonPCompiler(),
new MockResponseCompiler(),
new QuotaCompiler(),
new RateLimitByKeyCompiler(),
new RateLimitCompiler(),
new ReturnResponseCompiler(),
new RewriteUriCompiler(),
new SendRequestCompiler(),
new SetBackendServiceCompiler(),
new SetBodyCompiler(),
SetHeaderCompiler.AppendCompiler,
SetHeaderCompiler.RemoveCompiler,
SetHeaderCompiler.SetCompiler,
SetHeaderCompiler.SetIfNotExistCompiler,
new SetMethodCompiler(),
SetQueryParameterCompiler.AppendCompiler,
SetQueryParameterCompiler.RemoveCompiler,
SetQueryParameterCompiler.SetCompiler,
SetQueryParameterCompiler.SetIfNotExistCompiler,
new SetVariableCompiler(),
new ValidateJwtCompiler(),
new EmitMetricCompiler(),
EmitTokenMetricCompiler.Llm,
EmitTokenMetricCompiler.AzureOpenAi,
SemanticCacheStoreCompiler.Llm,
SemanticCacheStoreCompiler.AzureOpenAi,
SemanticCacheLookupCompiler.Llm,
SemanticCacheLookupCompiler.AzureOpenAi,
]);
var handlers = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type =>
type is
{
IsClass: true,
IsAbstract: false,
IsPublic: true,
Namespace: "Azure.ApiManagement.PolicyToolkit.Compiling.Policy"
}
&& typeof(IMethodPolicyHandler).IsAssignableFrom(type))
.Select(t => Activator.CreateInstance(t) as IMethodPolicyHandler)
.Where(h => h is not null)!
.ToArray<IMethodPolicyHandler>();
var invStatement = new ExpressionStatementCompiler(handlers);
var loc = new LocalDeclarationStatementCompiler([
new AuthenticationManageIdentityReturnValueCompiler()
]);
Expand Down
17 changes: 8 additions & 9 deletions src/Core/Compiling/Policy/EmitTokenMetricCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@

namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class EmitTokenMetricCompiler : IMethodPolicyHandler
{
public static IMethodPolicyHandler Llm =>
new EmitTokenMetricCompiler("llm-emit-token-metric", nameof(IInboundContext.LlmEmitTokenMetric));
public class LlmEmitTokenMetricCompiler()
: BaseEmitTokenMetricCompiler("llm-emit-token-metric", nameof(IInboundContext.LlmEmitTokenMetric));

public static IMethodPolicyHandler AzureOpenAi =>
new EmitTokenMetricCompiler("azure-openai-emit-token-metric",
nameof(IInboundContext.AzureOpenAiEmitTokenMetric));
public class AzureOpenAiEmitTokenMetricCompiler()
: BaseEmitTokenMetricCompiler("azure-openai-emit-token-metric", nameof(IInboundContext.AzureOpenAiEmitTokenMetric));

public abstract class BaseEmitTokenMetricCompiler : IMethodPolicyHandler
{
private readonly string _policyName;
public string MethodName { get; }

private EmitTokenMetricCompiler(string policyName, string methodName)
protected BaseEmitTokenMetricCompiler(string policyName, string methodName)
{
this._policyName = policyName;
MethodName = methodName;
Expand Down Expand Up @@ -85,7 +84,7 @@ public void Handle(ICompilationContext context, InvocationExpressionSyntax node)
dimensionElement.AddAttribute(result, nameof(MetricDimensionConfig.Value), "value");
element.Add(dimensionElement);
}

context.AddPolicy(element);
}
}
15 changes: 8 additions & 7 deletions src/Core/Compiling/Policy/SemanticCacheLookupCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@

namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class SemanticCacheLookupCompiler : IMethodPolicyHandler
{
public static IMethodPolicyHandler Llm => new SemanticCacheLookupCompiler(
nameof(IInboundContext.LlmSemanticCacheLookup), "llm-semantic-cache-lookup");
public class LlmSemanticCacheLookupCompiler()
: BaseSemanticCacheLookupCompiler(nameof(IInboundContext.LlmSemanticCacheLookup), "llm-semantic-cache-lookup");

public static IMethodPolicyHandler AzureOpenAi => new SemanticCacheLookupCompiler(
nameof(IInboundContext.AzureOpenAiSemanticCacheLookup), "azure-openai-semantic-cache-lookup");
public class AzureOpenAiSemanticCacheLookupCompiler()
: BaseSemanticCacheLookupCompiler(nameof(IInboundContext.AzureOpenAiSemanticCacheLookup),
"azure-openai-semantic-cache-lookup");

public abstract class BaseSemanticCacheLookupCompiler : IMethodPolicyHandler
{
private readonly string _policyName;
public string MethodName { get; }

SemanticCacheLookupCompiler(string methodName, string policyName)
protected BaseSemanticCacheLookupCompiler(string methodName, string policyName)
{
MethodName = methodName;
_policyName = policyName;
Expand Down
16 changes: 8 additions & 8 deletions src/Core/Compiling/Policy/SemanticCacheStoreCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@

namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class SemanticCacheStoreCompiler : IMethodPolicyHandler
{
public static IMethodPolicyHandler Llm =>
new SemanticCacheStoreCompiler(nameof(IOutboundContext.LlmSemanticCacheStore), "llm-semantic-cache-store");
public class LlmSemanticCacheStoreCompiler()
: BaseSemanticCacheStoreCompiler(nameof(IOutboundContext.LlmSemanticCacheStore), "llm-semantic-cache-store");

public static IMethodPolicyHandler AzureOpenAi =>
new SemanticCacheStoreCompiler(nameof(IOutboundContext.AzureOpenAiSemanticCacheStore),
"azure-openai-semantic-cache-store");
public class AzureOpenAiSemanticCacheStoreCompiler()
: BaseSemanticCacheStoreCompiler(nameof(IOutboundContext.AzureOpenAiSemanticCacheStore),
"azure-openai-semantic-cache-store");

public abstract class BaseSemanticCacheStoreCompiler : IMethodPolicyHandler
{
private readonly string _policyName;
public string MethodName { get; }

private SemanticCacheStoreCompiler(string methodName, string policyName)
protected BaseSemanticCacheStoreCompiler(string methodName, string policyName)
{
MethodName = methodName;
_policyName = policyName;
Expand Down
20 changes: 8 additions & 12 deletions src/Core/Compiling/Policy/SetHeaderCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@

namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class SetHeaderCompiler : IMethodPolicyHandler
{
public static SetHeaderCompiler AppendCompiler =>
new SetHeaderCompiler(nameof(IInboundContext.AppendHeader), "append");
public class AppendHeaderCompiler() : BaseSetHeaderCompiler(nameof(IInboundContext.AppendHeader), "append");

public static SetHeaderCompiler SetCompiler =>
new SetHeaderCompiler(nameof(IInboundContext.SetHeader), "override");
public class SetHeaderCompiler() : BaseSetHeaderCompiler(nameof(IInboundContext.SetHeader), "override");

public static SetHeaderCompiler SetIfNotExistCompiler =>
new SetHeaderCompiler(nameof(IInboundContext.SetHeaderIfNotExist), "skip");
public class SetHeaderIfNotExistCompiler() : BaseSetHeaderCompiler(nameof(IInboundContext.SetHeaderIfNotExist), "skip");

public static SetHeaderCompiler RemoveCompiler =>
new SetHeaderCompiler(nameof(IInboundContext.RemoveHeader), "delete");
public class RemoveHeaderCompiler() : BaseSetHeaderCompiler(nameof(IInboundContext.RemoveHeader), "delete");

readonly string _type;
public abstract class BaseSetHeaderCompiler : IMethodPolicyHandler
{
private readonly string _type;

private SetHeaderCompiler(string methodName, string type)
protected BaseSetHeaderCompiler(string methodName, string type)
{
MethodName = methodName;
_type = type;
Expand Down
22 changes: 11 additions & 11 deletions src/Core/Compiling/Policy/SetQueryParameterCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@

namespace Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class SetQueryParameterCompiler : IMethodPolicyHandler
{
public static SetQueryParameterCompiler AppendCompiler =>
new SetQueryParameterCompiler(nameof(IInboundContext.AppendQueryParameter), "append");
public class AppendQueryParameterCompiler()
: BaseSetQueryParameterCompiler(nameof(IInboundContext.AppendQueryParameter), "append");

public static SetQueryParameterCompiler SetCompiler =>
new SetQueryParameterCompiler(nameof(IInboundContext.SetQueryParameter), "override");
public class SetQueryParameterCompiler()
: BaseSetQueryParameterCompiler(nameof(IInboundContext.SetQueryParameter), "override");

public static SetQueryParameterCompiler SetIfNotExistCompiler =>
new SetQueryParameterCompiler(nameof(IInboundContext.SetQueryParameterIfNotExist), "skip");
public class SetIfNotExistQueryParameterCompiler()
: BaseSetQueryParameterCompiler(nameof(IInboundContext.SetQueryParameterIfNotExist), "skip");

public static SetQueryParameterCompiler RemoveCompiler =>
new SetQueryParameterCompiler(nameof(IInboundContext.RemoveQueryParameter), "delete");
public class RemoveQueryParameterCompiler()
: BaseSetQueryParameterCompiler(nameof(IInboundContext.RemoveQueryParameter), "delete");

public abstract class BaseSetQueryParameterCompiler : IMethodPolicyHandler
{
readonly string _type;

private SetQueryParameterCompiler(string methodName, string type)
protected BaseSetQueryParameterCompiler(string methodName, string type)
{
MethodName = methodName;
_type = type;
Expand Down

0 comments on commit 85d3905

Please sign in to comment.