| | | 1 | | using Intentum.Core.Intents; |
| | | 2 | | |
| | | 3 | | namespace Intentum.Runtime.Policy; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Fluent builder for creating IntentPolicy instances with a more readable API. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class IntentPolicyBuilder |
| | | 9 | | { |
| | 41 | 10 | | private readonly IntentPolicy _policy = new(); |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds a rule that allows when the condition is met. |
| | | 14 | | /// </summary> |
| | | 15 | | public IntentPolicyBuilder Allow(string name, Func<Intent, bool> condition) |
| | 21 | 16 | | { |
| | 21 | 17 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.Allow)); |
| | 21 | 18 | | return this; |
| | 21 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Adds a rule that observes when the condition is met. |
| | | 23 | | /// </summary> |
| | | 24 | | public IntentPolicyBuilder Observe(string name, Func<Intent, bool> condition) |
| | 7 | 25 | | { |
| | 7 | 26 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.Observe)); |
| | 7 | 27 | | return this; |
| | 7 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Adds a rule that warns when the condition is met. |
| | | 32 | | /// </summary> |
| | | 33 | | public IntentPolicyBuilder Warn(string name, Func<Intent, bool> condition) |
| | 8 | 34 | | { |
| | 8 | 35 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.Warn)); |
| | 8 | 36 | | return this; |
| | 8 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Adds a rule that blocks when the condition is met. |
| | | 41 | | /// </summary> |
| | | 42 | | public IntentPolicyBuilder Block(string name, Func<Intent, bool> condition) |
| | 16 | 43 | | { |
| | 16 | 44 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.Block)); |
| | 16 | 45 | | return this; |
| | 16 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Adds a rule that escalates when the condition is met. |
| | | 50 | | /// </summary> |
| | | 51 | | public IntentPolicyBuilder Escalate(string name, Func<Intent, bool> condition) |
| | 3 | 52 | | { |
| | 3 | 53 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.Escalate)); |
| | 3 | 54 | | return this; |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Adds a rule that requires authentication when the condition is met. |
| | | 59 | | /// </summary> |
| | | 60 | | public IntentPolicyBuilder RequireAuth(string name, Func<Intent, bool> condition) |
| | 2 | 61 | | { |
| | 2 | 62 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.RequireAuth)); |
| | 2 | 63 | | return this; |
| | 2 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Adds a rule that applies rate limiting when the condition is met. |
| | | 68 | | /// </summary> |
| | | 69 | | public IntentPolicyBuilder RateLimit(string name, Func<Intent, bool> condition) |
| | 4 | 70 | | { |
| | 4 | 71 | | _policy.AddRule(new PolicyRule(name, condition, PolicyDecision.RateLimit)); |
| | 4 | 72 | | return this; |
| | 4 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Adds a custom rule with a specific decision. |
| | | 77 | | /// </summary> |
| | | 78 | | public IntentPolicyBuilder Rule(string name, Func<Intent, bool> condition, PolicyDecision decision) |
| | 2 | 79 | | { |
| | 2 | 80 | | _policy.AddRule(new PolicyRule(name, condition, decision)); |
| | 2 | 81 | | return this; |
| | 2 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Builds and returns the IntentPolicy instance. |
| | | 86 | | /// </summary> |
| | | 87 | | public IntentPolicy Build() |
| | 41 | 88 | | { |
| | 41 | 89 | | return _policy; |
| | 41 | 90 | | } |
| | | 91 | | } |