kie

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: Apache-2.0 Imports: 13 Imported by: 2

README

kie client

the rest client of https://github.com/apache/servicecomb-kie

Usage

new client

	c, _ := kie.NewClient(kie.Config{
		Endpoint: "http://127.0.0.1:30110",
	})

create key value

	kv := kie.KVRequest{
		Key:       "app.properties",
		Status:    "enabled",
		Labels:    map[string]string{"service": "client"},
		Value:     "timeout: 1s",
		ValueType: "text",
	}
	result, err := c.Create(context.TODO(), kv, kie.WithProject("client_test"))

update

	result, err := c.Create(context.TODO(), kv, kie.WithProject("client_test"))
	assert.NoError(t, err)
	kv := kie.KVRequest{
		Status:    "enabled",
		Value:     "timeout: 2s",
	}
	kv.ID = result.ID
	_, err = c.Put(context.TODO(), kv, kie.WithProject("client_test"))

long polling key values

		go func() {
			kvs, _, err = c.List(context.TODO(),
				kie.WithLabels(map[string]string{"service": "client"}),
				kie.WithGetProject("client_test"),
				kie.WithWait("10s"))
			assert.NoError(t, err)
			assert.Equal(t, "timeout: 2s", kvs.Data[0].Value)
		}()
		kv := kie.KVRequest{
			ID:     result.ID,
			Key:    "app.properties",
			Labels: map[string]string{"service": "client"},
			Value:  "timeout: 2s",
		}
		_, err := c.Put(context.TODO(), kv, kie.WithProject("client_test"))
		assert.NoError(t, err)

delete

err = c.Delete(context.TODO(), kv.ID, kie.WithProject("client_test"))

Documentation

Index

Constants

View Source
const (
	QueryParamQ      = "q"
	QueryByLabelsCon = "&"
	QueryParamMatch  = "match"
	QueryParamKeyID  = "kv_id"
)

match mode

View Source
const (
	HeaderRevision    = "X-Kie-Revision"
	HeaderContentType = "Content-Type"
)

http headers

View Source
const (
	APIPathKV = "kie/kv"

	MsgOpFailed = "operation failed"
	FmtOpFailed = "operate failed [%s], http status [%s], body [%s]"
	APIFmt      = "%s/%s/%s/%s/%s"
)

const

View Source
const (
	ContentTypeJSON = "application/json"
)

ContentType

Variables

View Source
var (
	ErrKeyNotExist = errors.New("can not find value")
	ErrIDEmpty     = errors.New("id is empty")
	ErrNoChanges   = errors.New("kv has not been changed since last polling")
)

client errors

Functions

func ReadBody

func ReadBody(resp *http.Response) []byte

ReadBody read body from the from the response

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the servicecomb kie rest client. it is concurrency safe

func NewClient

func NewClient(config Config) (*Client, error)

NewClient create a client

func (*Client) Create

func (c *Client) Create(ctx context.Context, kv KVRequest, opts ...OpOption) (*KVDoc, error)

Create create value of a key

func (*Client) CurrentRevision

func (c *Client) CurrentRevision() int

CurrentRevision return the current local revision of kie, which is updated during the last polling request

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, kvIDs string, opts ...OpOption) error

Delete remove kv

func (*Client) Get

func (c *Client) Get(ctx context.Context, kvID string, opts ...GetOption) (*KVDoc, error)

Get get value of a key by id

func (*Client) List

func (c *Client) List(ctx context.Context, opts ...GetOption) (*KVResponse, int, error)

List get value of a key

func (*Client) Put

func (c *Client) Put(ctx context.Context, kv KVRequest, opts ...OpOption) (*KVDoc, error)

Put update value of a key

type Config

type Config struct {
	Endpoint      string
	DefaultLabels map[string]string
	VerifyPeer    bool //TODO make it works, now just keep it false
	HTTPOptions   *httpclient.Options
}

Config is the config of client

type DeleteBody

type DeleteBody struct {
	IDs []string `json:"ids"`
}

type GetOption

type GetOption func(*GetOptions)

GetOption is the functional option of client func

func WithExact

func WithExact() GetOption

WithExact means label exact match

func WithGetProject

func WithGetProject(project string) GetOption

WithGetProject query keys with certain project

func WithKey

func WithKey(k string) GetOption

WithKey query keys with certain key

func WithLabels

func WithLabels(l ...map[string]string) GetOption

WithLabels query kv by labels

func WithRevision

func WithRevision(revision int) GetOption

WithRevision query keys with certain revision

func WithWait

func WithWait(duration string) GetOption

WithWait is for long polling,format is 1s,2m

type GetOptions

type GetOptions struct {
	Labels   []map[string]string
	Project  string
	Key      string
	Wait     string
	Exact    bool
	Revision string
}

GetOptions is the options of client func

type KVDoc

type KVDoc struct {
	ID             string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
	Key            string `json:"key" yaml:"key"`
	Value          string `json:"value,omitempty" yaml:"value,omitempty"`
	ValueType      string `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty"` //ini,json,text,yaml,properties
	Checker        string `json:"check,omitempty" yaml:"check,omitempty"`                                       //python script
	CreateRevision int64  `json:"create_revision,omitempty" bson:"create_revision," yaml:"create_revision,omitempty"`
	UpdateRevision int64  `json:"update_revision,omitempty" bson:"update_revision," yaml:"update_revision,omitempty"`
	Project        string `json:"project,omitempty" yaml:"project,omitempty"`
	Status         string `json:"status,omitempty" yaml:"status,omitempty"`
	CreatTime      int64  `json:"create_time,omitempty" yaml:"create_time,omitempty"`
	UpdateTime     int64  `json:"update_time,omitempty" yaml:"update_time,omitempty"`

	Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` //redundant
	Domain string            `json:"domain,omitempty" yaml:"domain,omitempty"` //redundant

}

KVDoc is database struct to store kv

type KVRequest

type KVRequest struct {
	ID        string            `json:"id" yaml:"id"`
	Key       string            `json:"key" yaml:"key"`
	Value     string            `json:"value,omitempty" yaml:"value,omitempty"`
	Status    string            `json:"status,omitempty" yaml:"status,omitempty"`
	ValueType string            `json:"value_type,omitempty" bson:"value_type,omitempty" yaml:"value_type,omitempty"` //ini,json,text,yaml,properties
	Checker   string            `json:"check,omitempty" yaml:"check,omitempty"`                                       //python script
	Labels    map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`                                     //redundant
}

KVRequest is http request body

type KVResponse

type KVResponse struct {
	LabelDoc *LabelDocResponse `json:"label,omitempty"`
	Total    int               `json:"total,omitempty"`
	Data     []*KVDoc          `json:"data,omitempty"`
}

KVResponse represents the key value list

type LabelDocResponse

type LabelDocResponse struct {
	Labels map[string]string `json:"labels,omitempty"`
}

LabelDocResponse is label struct

type OpOption

type OpOption func(*OpOptions)

OpOption is the functional option of client func

func WithProject

func WithProject(project string) OpOption

WithProject set project to param

type OpOptions

type OpOptions struct {
	Project string
}

OpOptions is the options of client func

Jump to

Keyboard shortcuts

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