From 85a6fd72f99262e76530b359a3f752f9dfb8e605 Mon Sep 17 00:00:00 2001 From: vlastahajek Date: Fri, 24 Apr 2020 16:49:51 +0200 Subject: [PATCH] doc: Added examples to API doc, copyright --- CHANGELOG.md | 3 + api/authorizations.go | 4 ++ api/doc.go | 139 +++++++++++++++++++++++++++++++++++++ api/organizations.go | 6 +- api/users.go | 4 ++ domain/utils.go | 4 ++ examples_test.go | 122 ++++++++++++++++++++++++++++++++ internal/http/service.go | 4 ++ internal/http/userAgent.go | 4 ++ 9 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 api/doc.go create mode 100644 examples_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 39ff5c82..46bd7b15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ 1. [#99](https://github.com/influxdata/influxdb-client-go/pull/99) Organizations API and Users API 1. [#96](https://github.com/influxdata/influxdb-client-go/pull/96) Authorization API +### Doc +1. [#101](https://github.com/influxdata/influxdb-client-go/pull/101) Added examples to API doc + ## 1.0.0 [2020-04-01] ### Core diff --git a/api/authorizations.go b/api/authorizations.go index 922ab8c9..7088d696 100644 --- a/api/authorizations.go +++ b/api/authorizations.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package api import ( diff --git a/api/doc.go b/api/doc.go new file mode 100644 index 00000000..be6f5dbe --- /dev/null +++ b/api/doc.go @@ -0,0 +1,139 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + +// Package api provides clients for InfluxDB server APIs +// +// Examples +// +// Users API +// +// // Create influxdb client +// client := influxdb2.NewClient("http://localhost:9999", "my-token") +// +// // Find organization +// org, err := client.OrganizationsApi().FindOrganizationByName(context.Background(), "my-org") +// if err != nil { +// panic(err) +// } +// +// // Get users API client +// usersApi := client.UsersApi() +// +// // Create new user +// user, err := usersApi.CreateUserWithName(context.Background(), "user-01") +// if err != nil { +// panic(err) +// } +// +// // Set user password +// err = usersApi.UpdateUserPassword(context.Background(), user, "pass-at-least-8-chars") +// if err != nil { +// panic(err) +// } +// +// // Add user to organization +// _, err = client.OrganizationsApi().AddMember(context.Background(), org, user) +// if err != nil { +// panic(err) +// } +// +// Organizations API +// +// // Create influxdb client +// client := influxdb2.NewClient("http://localhost:9999", "my-token") +// +// // Get Organizations API client +// orgApi := client.OrganizationsApi() +// +// // Create new organization +// org, err := orgApi.CreateOrganizationWithName(context.Background(), "org-2") +// if err != nil { +// panic(err) +// } +// +// orgDescription := "My second org " +// org.Description = &orgDescription +// +// org, err = orgApi.UpdateOrganization(context.Background(), org) +// if err != nil { +// panic(err) +// } +// +// // Find user to set owner +// user, err := client.UsersApi().FindUserByName(context.Background(), "user-01") +// if err != nil { +// panic(err) +// } +// +// // Add another owner (first owner is the one who create organization +// _, err = orgApi.AddOwner(context.Background(), org, user) +// if err != nil { +// panic(err) +// } +// +// // Create new user to add to org +// newUser, err := client.UsersApi().CreateUserWithName(context.Background(), "user-02") +// if err != nil { +// panic(err) +// } +// +// // Add new user to organization +// _, err = orgApi.AddMember(context.Background(), org, newUser) +// if err != nil { +// panic(err) +// } +// +// Authorizations API +// +// // Create influxdb client +// client := influxdb2.NewClient("http://localhost:9999", "my-token") +// +// // Find user to grant permission +// user, err := client.UsersApi().FindUserByName(context.Background(), "user-01") +// if err != nil { +// panic(err) +// } +// +// // Find organization +// org, err := client.OrganizationsApi().FindOrganizationByName(context.Background(), "my-org") +// if err != nil { +// panic(err) +// } +// +// // create write permission for buckets +// permissionWrite := &domain.Permission{ +// Action: domain.PermissionActionWrite, +// Resource: domain.Resource{ +// Type: domain.ResourceTypeBuckets, +// }, +// } +// +// // create read permission for buckets +// permissionRead := &domain.Permission{ +// Action: domain.PermissionActionRead, +// Resource: domain.Resource{ +// Type: domain.ResourceTypeBuckets, +// }, +// } +// +// // group permissions +// permissions := []domain.Permission{*permissionWrite, *permissionRead} +// +// // create authorization object using info above +// auth := &domain.Authorization{ +// OrgID: org.Id, +// Permissions: &permissions, +// User: &user.Name, +// UserID: user.Id, +// } +// +// // grant permission and create token +// authCreated, err := client.AuthorizationsApi().CreateAuthorization(context.Background(), auth) +// if err != nil { +// panic(err) +// } +// +// // Use token +// fmt.Println("Token: ", *authCreated.Token) +package api diff --git a/api/organizations.go b/api/organizations.go index 11de27e1..f031e1df 100644 --- a/api/organizations.go +++ b/api/organizations.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package api import ( @@ -10,7 +14,7 @@ import ( type OrganizationsApi interface { // GetOrganizations returns all organizations GetOrganizations(ctx context.Context) (*[]domain.Organization, error) - // FindOrganizationByName returns an organization found using orgNme + // FindOrganizationByName returns an organization found using orgName FindOrganizationByName(ctx context.Context, orgName string) (*domain.Organization, error) // FindOrganizationById returns an organization found using orgId FindOrganizationById(ctx context.Context, orgId string) (*domain.Organization, error) diff --git a/api/users.go b/api/users.go index 393bda43..20b6590e 100644 --- a/api/users.go +++ b/api/users.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package api import ( diff --git a/domain/utils.go b/domain/utils.go index 589a912e..106376dd 100644 --- a/domain/utils.go +++ b/domain/utils.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package domain import ihttp "github.com/influxdata/influxdb-client-go/internal/http" diff --git a/examples_test.go b/examples_test.go new file mode 100644 index 00000000..c6635059 --- /dev/null +++ b/examples_test.go @@ -0,0 +1,122 @@ +package influxdb2_test + +import ( + "context" + "fmt" + "math/rand" + "time" + + "github.com/influxdata/influxdb-client-go" +) + +func ExampleWriteApiBlocking() { + // Create client + client := influxdb2.NewClient("http://localhost:9999", "my-token") + // Get blocking write client + writeApi := client.WriteApiBlocking("my-org", "my-bucket") + // write some points + for i := 0; i < 100; i++ { + // create data point + p := influxdb2.NewPoint( + "system", + map[string]string{ + "id": fmt.Sprintf("rack_%v", i%10), + "vendor": "AWS", + "hostname": fmt.Sprintf("host_%v", i%100), + }, + map[string]interface{}{ + "temperature": rand.Float64() * 80.0, + "disk_free": rand.Float64() * 1000.0, + "disk_total": (i/10 + 1) * 1000000, + "mem_total": (i/100 + 1) * 10000000, + "mem_free": rand.Uint64(), + }, + time.Now()) + // write synchronously + err := writeApi.WritePoint(context.Background(), p) + if err != nil { + panic(err) + } + } + // Ensures background processes finishes + client.Close() +} + +func ExampleWriteApi() { + // Create client and set batch size to 20 + client := influxdb2.NewClientWithOptions("http://localhost:9999", "my-token", + influxdb2.DefaultOptions().SetBatchSize(20)) + // Get non-blocking write client + writeApi := client.WriteApi("my-org", "my-bucket") + // write some points + for i := 0; i < 100; i++ { + // create point + p := influxdb2.NewPoint( + "system", + map[string]string{ + "id": fmt.Sprintf("rack_%v", i%10), + "vendor": "AWS", + "hostname": fmt.Sprintf("host_%v", i%100), + }, + map[string]interface{}{ + "temperature": rand.Float64() * 80.0, + "disk_free": rand.Float64() * 1000.0, + "disk_total": (i/10 + 1) * 1000000, + "mem_total": (i/100 + 1) * 10000000, + "mem_free": rand.Uint64(), + }, + time.Now()) + // write asynchronously + writeApi.WritePoint(p) + } + // Force all unwritten data to be sent + writeApi.Flush() + // Ensures background processes finishes + client.Close() +} + +func ExampleQueryApi_query() { + // Create client + client := influxdb2.NewClient("http://localhost:9999", "my-token") + // Get query client + queryApi := client.QueryApi("my-org") + // get QueryTableResult + result, err := queryApi.Query(context.Background(), `from(bucket:"my-bucket")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`) + if err == nil { + // Iterate over query response + for result.Next() { + // Notice when group key has changed + if result.TableChanged() { + fmt.Printf("table: %s\n", result.TableMetadata().String()) + } + // Access data + fmt.Printf("value: %v\n", result.Record().Value()) + } + // check for an error + if result.Err() != nil { + fmt.Printf("query parsing error: %s\n", result.Err().Error()) + } + } else { + panic(err) + } + // Ensures background processes finishes + client.Close() +} + +func ExampleQueryApi_queryRaw() { + // Create client + client := influxdb2.NewClient("http://localhost:9999", "my-token") + // Get query client + queryApi := client.QueryApi("my-org") + // Query and get complete result as a string + // Use default dialect + result, err := queryApi.QueryRaw(context.Background(), `from(bucket:"my-bucket")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`, influxdb2.DefaultDialect()) + if err == nil { + fmt.Println("QueryResult:") + fmt.Println(result) + } else { + panic(err) + } + // Ensures background processes finishes + client.Close() +} diff --git a/internal/http/service.go b/internal/http/service.go index 46ccd69f..f8993b0d 100644 --- a/internal/http/service.go +++ b/internal/http/service.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package http import ( diff --git a/internal/http/userAgent.go b/internal/http/userAgent.go index c3163d76..0b69cca6 100644 --- a/internal/http/userAgent.go +++ b/internal/http/userAgent.go @@ -1,3 +1,7 @@ +// Copyright 2020 InfluxData, Inc. All rights reserved. +// Use of this source code is governed by MIT +// license that can be found in the LICENSE file. + package http // Keeps once created User-Agent string