Skip to content

Commit

Permalink
feat: support new OpenGemini client with config
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <[email protected]>
  • Loading branch information
Chenxulin97 committed Nov 16, 2023
1 parent 2a41447 commit 4a07b8c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
46 changes: 46 additions & 0 deletions opengemini/client.go
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
package opengemini

import (
"crypto/tls"
)

var _, _ = NewClient(&Config{})

type Client interface {
}

type Config struct {
AddressList []*Address
AuthConfig *AuthConfig
BatchConfig *BatchConfig
GzipEnabled bool
TlsConfig *tls.Config
}

type Address struct {
Host string
Port int
}

type AuthType int

const (
Password AuthType = iota
Token
)

type AuthConfig struct {
AuthType AuthType
Username string
Password string
Token string
}

type BatchConfig struct {
BatchEnabled bool
BatchInterval int
BatchSize int
}

func NewClient(config *Config) (Client, error) {
return newClient(config)
}
36 changes: 36 additions & 0 deletions opengemini/client_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package opengemini

import "errors"

type client struct {
config *Config
}

func newClient(c *Config) (Client, error) {
if len(c.AddressList) == 0 {
return nil, errors.New("must have at least one address")
}
if c.AuthConfig.AuthType == Token && len(c.AuthConfig.Token) == 0 {
return nil, errors.New("invalid auth config due to empty token")
}
if c.AuthConfig.AuthType == Password {
if len(c.AuthConfig.Username) == 0 {
return nil, errors.New("invalid auth config due to empty username")
}
if len(c.AuthConfig.Password) == 0 {
return nil, errors.New("invalid auth config due to empty password")
}
}
if c.BatchConfig.BatchEnabled {
if c.BatchConfig.BatchInterval <= 0 {
return nil, errors.New("batch enabled, batch interval must be great than 0")
}
if c.BatchConfig.BatchSize <= 0 {
return nil, errors.New("batch enabled, batch size must be great than 0")
}
}
client := &client{
config: c,
}
return client, nil
}

0 comments on commit 4a07b8c

Please sign in to comment.