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

docs: Added API doc examples #101

Merged
merged 1 commit into from
Apr 24, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions api/authorizations.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
139 changes: 139 additions & 0 deletions api/doc.go
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion api/organizations.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions api/users.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions domain/utils.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
122 changes: 122 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 4 additions & 0 deletions internal/http/service.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions internal/http/userAgent.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down