Documentation ¶
Index ¶
- type Client
- type Evaluator
- type ListOptions
- type Option
- type RulesetService
- func (s *RulesetService) Eval(ctx context.Context, path string, params rule.Params) (*regula.EvalResult, error)
- func (s *RulesetService) EvalVersion(ctx context.Context, path, version string, params rule.Params) (*regula.EvalResult, error)
- func (s *RulesetService) List(ctx context.Context, prefix string, opt *ListOptions) (*api.Rulesets, error)
- func (s *RulesetService) Put(ctx context.Context, path string, rs *regula.Ruleset) (*api.Ruleset, error)
- func (s *RulesetService) Watch(ctx context.Context, prefix string, revision string) <-chan WatchResponse
- type WatchResponse
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { Logger zerolog.Logger WatchRetryDelay time.Duration // Time between failed watch requests. Defaults to 1s. RetryDelay time.Duration // Time between failed requests retries. Defaults to 1s. Retries int // Number of retries on retriable errors. Headers map[string]string Rulesets *RulesetService // contains filtered or unexported fields }
A Client manages communication with the Rules Engine API using HTTP.
type Evaluator ¶
type Evaluator struct { *regula.RulesetBuffer // contains filtered or unexported fields }
Evaluator can cache rulesets in memory and can be passed to a regula.Engine to evaluate rulesets without network round trips. If required, it can watch the server for changes and update its local cache.
Example (WithWatch) ¶
package main import ( "context" "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/api/client" ) var addr string func main() { cli, err := client.New(addr) if err != nil { log.Fatal(err) } ev, err := client.NewEvaluator(context.Background(), cli, "prefix", true) if err != nil { log.Fatal(err) } // stopping the watcher defer ev.Close() ng := regula.NewEngine(ev) str, res, err := ng.GetString(context.Background(), "some/path", regula.Params{ "id": "123", }) if err != nil { log.Fatal(err) } fmt.Println(str) fmt.Println(res.Version) }
Output:
Example (WithoutWatch) ¶
package main import ( "context" "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/api/client" ) var addr string func main() { cli, err := client.New(addr) if err != nil { log.Fatal(err) } ev, err := client.NewEvaluator(context.Background(), cli, "prefix", false) if err != nil { log.Fatal(err) } ng := regula.NewEngine(ev) str, res, err := ng.GetString(context.Background(), "some/path", regula.Params{ "id": "123", }) if err != nil { log.Fatal(err) } fmt.Println(str) fmt.Println(res.Version) }
Output:
func NewEvaluator ¶
func NewEvaluator(ctx context.Context, client *Client, prefix string, watch bool) (*Evaluator, error)
NewEvaluator uses the given client to fetch a list of rulesets starting with the given prefix and returns an evaluator that holds the results in memory. If watch is true, the evaluator will watch for changes on the server and automatically update the underlying RulesetBuffer. If watch is set to true, the Close method must always be called to gracefully close the watcher.
type ListOptions ¶
ListOptions contains pagination options.
type Option ¶
Option allows Client customization.
func HTTPClient ¶
HTTPClient specifies the http client to be used.
type RulesetService ¶
type RulesetService struct {
// contains filtered or unexported fields
}
RulesetService handles communication with the ruleset related methods of the Regula API.
func (*RulesetService) Eval ¶
func (s *RulesetService) Eval(ctx context.Context, path string, params rule.Params) (*regula.EvalResult, error)
Eval evaluates the given ruleset with the given params. It implements the regula.Evaluator interface and thus can be passed to the regula.Engine.
Example ¶
package main import ( "context" "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/api/client" ) func main() { c, err := client.New("http://127.0.0.1:5331") if err != nil { log.Fatal(err) } resp, err := c.Rulesets.Eval(context.Background(), "path/to/ruleset", regula.Params{ "foo": "bar", "baz": int64(42), }) if err != nil { log.Fatal(err) } fmt.Println(resp.Value.Data) fmt.Println(resp.Value.Type) fmt.Println(resp.Version) }
Output:
func (*RulesetService) EvalVersion ¶
func (s *RulesetService) EvalVersion(ctx context.Context, path, version string, params rule.Params) (*regula.EvalResult, error)
EvalVersion evaluates the given ruleset version with the given params. It implements the regula.Evaluator interface and thus can be passed to the regula.Engine.
Example ¶
package main import ( "context" "fmt" "log" "github.com/heetch/regula" "github.com/heetch/regula/api/client" ) func main() { c, err := client.New("http://127.0.0.1:5331") if err != nil { log.Fatal(err) } resp, err := c.Rulesets.EvalVersion(context.Background(), "path/to/ruleset", "xyzabc", regula.Params{ "foo": "bar", "baz": int64(42), }) if err != nil { log.Fatal(err) } fmt.Println(resp.Value.Data) fmt.Println(resp.Value.Type) fmt.Println(resp.Version) }
Output:
func (*RulesetService) List ¶
func (s *RulesetService) List(ctx context.Context, prefix string, opt *ListOptions) (*api.Rulesets, error)
List fetches all the rulesets starting with the given prefix.
Example ¶
package main import ( "context" "log" "github.com/heetch/regula/api/client" ) func main() { c, err := client.New("http://127.0.0.1:5331") if err != nil { log.Fatal(err) } list, err := c.Rulesets.List(context.Background(), "prefix", nil) if err != nil { log.Fatal(err) } for _, e := range list.Rulesets { e.Ruleset.Eval(nil) } }
Output:
Example (WithPagination) ¶
package main import ( "context" "log" "github.com/heetch/regula/api/client" ) func main() { c, err := client.New("http://127.0.0.1:5331") if err != nil { log.Fatal(err) } list, err := c.Rulesets.List(context.Background(), "prefix", &client.ListOptions{ Limit: 20, }) if err != nil { log.Fatal(err) } for _, e := range list.Rulesets { e.Ruleset.Eval(nil) } for list.Continue != "" { list, err = c.Rulesets.List(context.Background(), "prefix", &client.ListOptions{ Limit: 20, Continue: list.Continue, }) if err != nil { log.Fatal(err) } for _, e := range list.Rulesets { e.Ruleset.Eval(nil) } } }
Output:
func (*RulesetService) Put ¶
func (s *RulesetService) Put(ctx context.Context, path string, rs *regula.Ruleset) (*api.Ruleset, error)
Put creates a ruleset version on the given path.
func (*RulesetService) Watch ¶
func (s *RulesetService) Watch(ctx context.Context, prefix string, revision string) <-chan WatchResponse
Watch watchs the given path for changes and sends the events in the returned channel. If revision is empty it will start to watch for changes occuring from the moment the request is performed, otherwise it will watch for any changes occured from the given revision. The given context must be used to stop the watcher.
type WatchResponse ¶
WatchResponse contains a list of events occured on a group of rulesets. If an error occurs during the watching, the Err field will be populated.