-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from bonitoo-io/doc/apidoc_examples
docs: Added API doc examples
- Loading branch information
Showing
9 changed files
with
289 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters