client

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Index

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.

func New

func New(baseURL string, opts ...Option) (*Client, error)

New creates an HTTP client that uses a base url to communicate with the api server.

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.

func (*Evaluator) Close

func (e *Evaluator) Close() error

Close stops the watcher if running.

type ListOptions

type ListOptions struct {
	Limit    int
	Continue string
}

ListOptions contains pagination options.

type Option

type Option func(*Client) error

Option allows Client customization.

func HTTPClient

func HTTPClient(httpClient *http.Client) Option

HTTPClient specifies the http client to be used.

func Header(k, v string) Option

Header adds a key value pair to the headers sent on each request.

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

type WatchResponse struct {
	Events *api.Events
	Err    error
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL