Skip to content

Commit

Permalink
Merge pull request #101 from bonitoo-io/doc/apidoc_examples
Browse files Browse the repository at this point in the history
docs: Added API doc examples
  • Loading branch information
vlastahajek authored Apr 24, 2020
2 parents f473a8d + 85a6fd7 commit 4ca9d67
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 1 deletion.
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

0 comments on commit 4ca9d67

Please sign in to comment.