forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
76 lines (63 loc) · 2.09 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package influxdb2_test
import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/domain"
)
func ExampleClient_newClient() {
// Create a new client using an InfluxDB server base URL and an authentication token
client := influxdb2.NewClient("http://localhost:9999", "my-token")
// Always close client at the end
defer client.Close()
}
func ExampleClient_newClientWithOptions() {
// Create a new client using an InfluxDB server base URL and an authentication token
// Create client and set batch size to 20
client := influxdb2.NewClientWithOptions("http://localhost:9999", "my-token",
influxdb2.DefaultOptions().SetBatchSize(20))
// Always close client at the end
defer client.Close()
}
func ExampleClient_customServerAPICall() {
// Create a new client using an InfluxDB server base URL and empty token
client := influxdb2.NewClient("http://localhost:9999", "my-token")
// Always close client at the end
defer client.Close()
// Get generated client for server API calls
apiClient := domain.NewClientWithResponses(client.HTTPService())
// Get an organization that will own task
org, err := client.OrganizationsAPI().FindOrganizationByName(context.Background(), "my-org")
if err != nil {
//return err
panic(err)
}
// Basic task properties
taskDescription := "Example task"
taskFlux := `option task = {
name: "My task",
every: 1h
}
from(bucket:"my-bucket") |> range(start: -1m) |> last()`
taskStatus := domain.TaskStatusTypeActive
// Create TaskCreateRequest object
taskRequest := domain.TaskCreateRequest{
Org: &org.Name,
OrgID: org.Id,
Description: &taskDescription,
Flux: taskFlux,
Status: &taskStatus,
}
// Issue an API call
resp, err := apiClient.PostTasksWithResponse(context.Background(), &domain.PostTasksParams{}, domain.PostTasksJSONRequestBody(taskRequest))
if err != nil {
panic(err)
}
// Always check generated response errors
if resp.JSONDefault != nil {
panic(resp.JSONDefault.Message)
}
// Use API call result
task := resp.JSON201
fmt.Println("Created task: ", task.Name)
}