Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync #3

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions opengemini/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package opengemini

import (
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
)

type requestDetails struct {
queryValues url.Values
header http.Header
body io.Reader
}

func (c *client) updateAuthHeader(method, urlPath string, header http.Header) http.Header {
if c.config.AuthConfig == nil {
return header
}

if methods, ok := noAuthRequired[urlPath]; ok {
if _, methodOk := methods[method]; methodOk {
return header
}
}

if header == nil {
header = make(http.Header)
}

if c.config.AuthConfig.AuthType == AuthTypePassword {
encodeString := c.config.AuthConfig.Username + ":" + c.config.AuthConfig.Password
authorization := "Basic " + base64.StdEncoding.EncodeToString([]byte(encodeString))
header.Set("Authorization", authorization)
}

return header
}

func (c *client) executeHttpGetByIdx(idx int, urlPath string, details requestDetails) (*http.Response, error) {
return c.executeHttpRequestByIdx(idx, http.MethodGet, urlPath, details)
}

func (c *client) executeHttpRequestByIdx(idx int, method, urlPath string, details requestDetails) (*http.Response, error) {
if idx >= len(c.serverUrls) || idx < 0 {
return nil, errors.New("index out of range")
}
serverUrl := c.serverUrls[idx]
return c.executeHttpRequest(method, serverUrl, urlPath, details)
}

func (c *client) executeHttpRequest(method, serverUrl, urlPath string, details requestDetails) (*http.Response, error) {
details.header = c.updateAuthHeader(method, urlPath, details.header)
fullUrl := serverUrl + urlPath
u, err := url.Parse(fullUrl)
if err != nil {
return nil, err
}

if details.queryValues != nil {
u.RawQuery = details.queryValues.Encode()
}

req, err := http.NewRequest(method, u.String(), details.body)
if err != nil {
return nil, err
}

for k, values := range details.header {
for _, v := range values {
req.Header.Add(k, v)
}
}

return c.cli.Do(req)
}
28 changes: 28 additions & 0 deletions opengemini/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package opengemini

import (
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

func TestSetAuthorization(t *testing.T) {
c := client{
config: &Config{
AuthConfig: &AuthConfig{
AuthType: AuthTypePassword,
Username: "test",
Password: "test pwd",
},
},
}

header := c.updateAuthHeader(http.MethodGet, UrlPing, nil)
require.Equal(t, "", header.Get("Authorization"))

header = c.updateAuthHeader(http.MethodOptions, UrlQuery, nil)
require.Equal(t, "", header.Get("Authorization"))

header = c.updateAuthHeader(http.MethodGet, UrlQuery, nil)
require.Equal(t, "Basic dGVzdDp0ZXN0IHB3ZA==", header.Get("Authorization"))
}
3 changes: 1 addition & 2 deletions opengemini/client_ping.go → opengemini/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import "net/http"

// Ping check that status of cluster.
func (c *client) Ping(idx int) error {
serverUrl := c.serverUrls[idx]
resp, err := c.cli.Get(serverUrl + UrlPing)
resp, err := c.executeHttpGetByIdx(idx, UrlPing, requestDetails{})
if err != nil {
return err
}
Expand Down
16 changes: 15 additions & 1 deletion opengemini/client_ping_test.go → opengemini/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestPingSuccess(t *testing.T) {
require.Nil(t, err)
}

func TestPingFail(t *testing.T) {
func TestPingFailForInaccessibleAddress(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Expand All @@ -31,3 +31,17 @@ func TestPingFail(t *testing.T) {
err := c.Ping(1)
require.NotNil(t, err)
}

func TestPingFailForOutOfRangeIndex(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})

err := c.Ping(1)
require.NotNil(t, err)
err = c.Ping(-1)
require.NotNil(t, err)
}
45 changes: 45 additions & 0 deletions opengemini/point.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package opengemini

import "time"

type Point struct {
Measurement string
Precision string
Time time.Time
Tags map[string]string
Fields map[string]interface{}
}

func (p *Point) AddTag(key string, value string) {
if p.Tags == nil {
p.Tags = make(map[string]string)
}
p.Tags[key] = value
}

func (p *Point) AddField(key string, value interface{}) {
if p.Fields == nil {
p.Fields = make(map[string]interface{})
}
p.Fields[key] = value
}

func (p *Point) SetTime(t time.Time) {
p.Time = t
}

func (p *Point) SetPrecision(precision string) {
p.Precision = precision
}

func (p *Point) SetMeasurement(name string) {
p.Measurement = name
}

type BatchPoints struct {
Points []*Point
}

func (bp *BatchPoints) AddPoint(p *Point) {
bp.Points = append(bp.Points, p)
}
13 changes: 13 additions & 0 deletions opengemini/query_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package opengemini

// SeriesResult contains the results of a series query
type SeriesResult struct {
Series []Series `json:"series,omitempty"`
Error string `json:"error,omitempty"`
}

// QueryResult is the top-level struct
type QueryResult struct {
Results []SeriesResult `json:"results,omitempty"`
Error string `json:"error,omitempty"`
}
9 changes: 9 additions & 0 deletions opengemini/series.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package opengemini

// Series defines the structure for series data
type Series struct {
Name string `json:"name,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Columns []string `json:"columns,omitempty"`
Values [][]interface{} `json:"values,omitempty"`
}
22 changes: 21 additions & 1 deletion opengemini/url_const.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package opengemini

const UrlPing = "/ping"
import "net/http"

const (
UrlPing = "/ping"
UrlQuery = "/query"
UrlStatus = "/status"
)

var noAuthRequired = map[string]map[string]struct{}{
UrlPing: {
http.MethodHead: {},
http.MethodGet: {},
},
UrlQuery: {
http.MethodOptions: {},
},
UrlStatus: {
http.MethodHead: {},
http.MethodGet: {},
},
}
Loading