Skip to content

Commit

Permalink
Merge pull request #107 from bonitoo-io/refactor/rename_client_intrer…
Browse files Browse the repository at this point in the history
…face

refactor: Renaming InfluxDBClient -> Client
  • Loading branch information
vlastahajek authored Apr 30, 2020
2 parents 5563857 + 5622f46 commit 1077e4f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0
## Breaking Change
- [#107](https://github.com/influxdata/influxdb-client-go/pull/100) Renamed `InfluxDBClient` interface to `Client`, so the full name `influxdb2.Client` suits better to Go naming conventions

## 1.1.0 [2020-04-24]
### Features
1. [#100](https://github.com/influxdata/influxdb-client-go/pull/100) HTTP request timeout made configurable
Expand Down
38 changes: 19 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
ihttp "github.com/influxdata/influxdb-client-go/internal/http"
)

// InfluxDBClient provides API to communicate with InfluxDBServer
// Client provides API to communicate with InfluxDBServer
// There two APIs for writing, WriteApi and WriteApiBlocking.
// WriteApi provides asynchronous, non-blocking, methods for writing time series data.
// WriteApiBlocking provides blocking methods for writing time series data
type InfluxDBClient interface {
type Client interface {
// WriteApi returns the asynchronous, non-blocking, Write client.
WriteApi(org, bucket string) WriteApi
// WriteApi returns the synchronous, blocking, Write client.
Expand All @@ -51,8 +51,8 @@ type InfluxDBClient interface {
Ready(ctx context.Context) (bool, error)
}

// client implements InfluxDBClient interface
type client struct {
// clientImpl implements Client interface
type clientImpl struct {
serverUrl string
options *Options
writeApis []WriteApi
Expand All @@ -63,35 +63,35 @@ type client struct {
usersApi api.UsersApi
}

// NewClient creates InfluxDBClient for connecting to given serverUrl with provided authentication token, with default options
// NewClient creates Client for connecting to given serverUrl with provided authentication token, with the default options.
// Authentication token can be empty in case of connecting to newly installed InfluxDB server, which has not been set up yet.
// In such case Setup will set authentication token
func NewClient(serverUrl string, authToken string) InfluxDBClient {
func NewClient(serverUrl string, authToken string) Client {
return NewClientWithOptions(serverUrl, authToken, DefaultOptions())
}

// NewClientWithOptions creates InfluxDBClient for connecting to given serverUrl with provided authentication token
// NewClientWithOptions creates Client for connecting to given serverUrl with provided authentication token
// and configured with custom Options
// Authentication token can be empty in case of connecting to newly installed InfluxDB server, which has not been set up yet.
// In such case Setup will set authentication token
func NewClientWithOptions(serverUrl string, authToken string, options *Options) InfluxDBClient {
client := &client{
func NewClientWithOptions(serverUrl string, authToken string, options *Options) Client {
client := &clientImpl{
serverUrl: serverUrl,
options: options,
writeApis: make([]WriteApi, 0, 5),
httpService: ihttp.NewService(serverUrl, "Token "+authToken, options.tlsConfig, options.httpRequestTimeout),
}
return client
}
func (c *client) Options() *Options {
func (c *clientImpl) Options() *Options {
return c.options
}

func (c *client) ServerUrl() string {
func (c *clientImpl) ServerUrl() string {
return c.serverUrl
}

func (c *client) Ready(ctx context.Context) (bool, error) {
func (c *clientImpl) Ready(ctx context.Context) (bool, error) {
readyUrl, err := url.Parse(c.serverUrl)
if err != nil {
return false, err
Expand All @@ -111,28 +111,28 @@ func (c *client) Ready(ctx context.Context) (bool, error) {
return readyRes, nil
}

func (c *client) WriteApi(org, bucket string) WriteApi {
func (c *clientImpl) WriteApi(org, bucket string) WriteApi {
w := newWriteApiImpl(org, bucket, c.httpService, c)
c.writeApis = append(c.writeApis, w)
return w
}

func (c *client) WriteApiBlocking(org, bucket string) WriteApiBlocking {
func (c *clientImpl) WriteApiBlocking(org, bucket string) WriteApiBlocking {
w := newWriteApiBlockingImpl(org, bucket, c.httpService, c)
return w
}

func (c *client) Close() {
func (c *clientImpl) Close() {
for _, w := range c.writeApis {
w.Close()
}
}

func (c *client) QueryApi(org string) QueryApi {
func (c *clientImpl) QueryApi(org string) QueryApi {
return newQueryApi(org, c.httpService, c)
}

func (c *client) AuthorizationsApi() api.AuthorizationsApi {
func (c *clientImpl) AuthorizationsApi() api.AuthorizationsApi {
c.lock.Lock()
defer c.lock.Unlock()
if c.authApi == nil {
Expand All @@ -141,7 +141,7 @@ func (c *client) AuthorizationsApi() api.AuthorizationsApi {
return c.authApi
}

func (c *client) OrganizationsApi() api.OrganizationsApi {
func (c *clientImpl) OrganizationsApi() api.OrganizationsApi {
c.lock.Lock()
defer c.lock.Unlock()
if c.orgApi == nil {
Expand All @@ -150,7 +150,7 @@ func (c *client) OrganizationsApi() api.OrganizationsApi {
return c.orgApi
}

func (c *client) UsersApi() api.UsersApi {
func (c *clientImpl) UsersApi() api.UsersApi {
c.lock.Lock()
defer c.lock.Unlock()
if c.usersApi == nil {
Expand Down
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type QueryApi interface {
Query(ctx context.Context, query string) (*QueryTableResult, error)
}

func newQueryApi(org string, service ihttp.Service, client InfluxDBClient) QueryApi {
func newQueryApi(org string, service ihttp.Service, client Client) QueryApi {
return &queryApiImpl{
org: org,
httpService: service,
Expand All @@ -59,7 +59,7 @@ func newQueryApi(org string, service ihttp.Service, client InfluxDBClient) Query
type queryApiImpl struct {
org string
httpService ihttp.Service
client InfluxDBClient
client Client
url string
lock sync.Mutex
}
Expand Down
12 changes: 7 additions & 5 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"path"
)

func (c *client) Setup(ctx context.Context, username, password, org, bucket string, retentionPeriodHours int) (*domain.OnboardingResponse, error) {
func (c *clientImpl) Setup(ctx context.Context, username, password, org, bucket string, retentionPeriodHours int) (*domain.OnboardingResponse, error) {
if username == "" || password == "" {
return nil, errors.New("a username and password is required for a setup")
}
Expand All @@ -42,11 +42,13 @@ func (c *client) Setup(ctx context.Context, username, password, org, bucket stri
}
u.Path = path.Join(u.Path, "setup")

error := c.httpService.PostRequest(ctx, u.String(), bytes.NewReader(inputData), func(req *http.Request) {
perror := c.httpService.PostRequest(ctx, u.String(), bytes.NewReader(inputData), func(req *http.Request) {
req.Header.Add("Content-Type", "application/json; charset=utf-8")
},
func(resp *http.Response) error {
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
setupResponse := &domain.OnboardingResponse{}
if err := json.NewDecoder(resp.Body).Decode(setupResponse); err != nil {
return err
Expand All @@ -58,8 +60,8 @@ func (c *client) Setup(ctx context.Context, username, password, org, bucket stri
return nil
},
)
if error != nil {
return nil, error
if perror != nil {
return nil, perror
}
return setupResult, nil
}
6 changes: 3 additions & 3 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type writeBuffInfoReq struct {
writeBuffLen int
}

func newWriteApiImpl(org string, bucket string, service http.Service, client InfluxDBClient) *writeApiImpl {
func newWriteApiImpl(org string, bucket string, service http.Service, client Client) *writeApiImpl {
w := &writeApiImpl{
service: newWriteService(org, bucket, service, client),
writeBuffer: make([]string, 0, client.Options().BatchSize()+1),
Expand Down Expand Up @@ -137,7 +137,7 @@ func (w *writeApiImpl) flushBuffer() {
w.writeCh <- batch
// lines = lines[:0]
//}(w.writeBuffer)
//w.writeBuffer = make([]string,0, w.service.client.Options.BatchSize+1)
//w.writeBuffer = make([]string,0, w.service.clientImpl.Options.BatchSize+1)
w.writeBuffer = w.writeBuffer[:0]
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func (w *writeApiImpl) WriteRecord(line string) {
}

func (w *writeApiImpl) WritePoint(point *Point) {
//w.bufferCh <- point.ToLineProtocol(w.service.client.Options().Precision)
//w.bufferCh <- point.ToLineProtocol(w.service.clientImpl.Options().Precision)
line, err := w.service.encodePoints(point)
if err != nil {
logger.Errorf("point encoding error: %s\n", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion writeApiBlocking.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type writeApiBlockingImpl struct {
}

// creates writeApiBlockingImpl for org and bucket with underlying client
func newWriteApiBlockingImpl(org string, bucket string, service http.Service, client InfluxDBClient) *writeApiBlockingImpl {
func newWriteApiBlockingImpl(org string, bucket string, service http.Service, client Client) *writeApiBlockingImpl {
return &writeApiBlockingImpl{service: newWriteService(org, bucket, service, client)}
}

Expand Down
4 changes: 2 additions & 2 deletions writeService.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type writeService struct {
lastWriteAttempt time.Time
retryQueue *queue
lock sync.Mutex
client InfluxDBClient
client Client
}

func newWriteService(org string, bucket string, httpService ihttp.Service, client InfluxDBClient) *writeService {
func newWriteService(org string, bucket string, httpService ihttp.Service, client Client) *writeService {
logger.SetDebugLevel(client.Options().LogLevel())
retryBufferLimit := client.Options().RetryBufferLimit() / client.Options().BatchSize()
if retryBufferLimit == 0 {
Expand Down
10 changes: 5 additions & 5 deletions write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func (t *testHttpService) ReplyError() *ihttp.Error {
return t.replyError
}

func (t *testHttpService) SetAuthorization(authorization string) {
func (t *testHttpService) SetAuthorization(_ string) {

}
func (t *testHttpService) GetRequest(_ context.Context, _ string, _ ihttp.RequestCallback, _ ihttp.ResponseCallback) *ihttp.Error {
return nil
}
func (t *testHttpService) DoHttpRequest(req *http.Request, requestCallback ihttp.RequestCallback, _ ihttp.ResponseCallback) *ihttp.Error {
func (t *testHttpService) DoHttpRequest(_ *http.Request, _ ihttp.RequestCallback, _ ihttp.ResponseCallback) *ihttp.Error {
return nil
}

Expand Down Expand Up @@ -121,11 +121,11 @@ func (t *testHttpService) Lines() []string {
return t.lines
}

func newTestClient() *client {
return &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
func newTestClient() *clientImpl {
return &clientImpl{serverUrl: "http://locahost:4444", options: DefaultOptions()}
}

func newTestService(t *testing.T, client InfluxDBClient) *testHttpService {
func newTestService(t *testing.T, client Client) *testHttpService {
return &testHttpService{
t: t,
options: client.Options(),
Expand Down

0 comments on commit 1077e4f

Please sign in to comment.