Skip to content

Commit 5570678

Browse files
easyCZroboquat
authored andcommitted
examples
1 parent 938466d commit 5570678

File tree

4 files changed

+89
-24
lines changed

4 files changed

+89
-24
lines changed

components/public-api/go/README.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@ go get -u github.com/gitpod-io/gitpod/components/public-api/go
77
```
88

99
```golang
10-
1110
import (
12-
"github.com/bufbuild/connect-go"
11+
"context"
12+
"fmt"
13+
"os"
14+
"time"
1315

14-
gitpod_experimental_v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
15-
gitpod_experimental_v1connect "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
16+
"github.com/bufbuild/connect-go"
17+
"github.com/gitpod-io/gitpod/components/public-api/go/client"
18+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
1619
)
1720

18-
// Define an interceptor to attach credentials onto outgoing requests
19-
interceptor := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
20-
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
21-
if req.Spec().IsClient {
22-
// Send a token with client requests.
23-
req.Header().Set("Authorization", "Bearer your-access-token")
24-
}
25-
26-
return next(ctx, req)
27-
})
28-
})
29-
30-
// Construct a new client to interact with Gitpod
31-
client := gitpod_experimental_v1connect.NewTeamsServiceClient(http.DefaultClient, "https://api.gitpod.io", connect.WithInterceptors(
32-
inteceptor,
33-
))
34-
35-
// Use the client to retreive teams
36-
response, err := client.ListTeams(context.Background(), gitpod_experimental_v1connect.NewRequest(&gitpod_experimental_v1.ListTeamsRequest{}))
21+
func ExampleListTeams() {
22+
token := "gitpod_pat_example.personal-access-token"
23+
24+
gitpod, err := client.New(client.WithCredentials(token))
25+
if err != nil {
26+
fmt.Fprintf(os.Stderr, "Failed to construct gitpod client %v", err)
27+
return
28+
}
29+
30+
response, err := gitpod.Teams.ListTeams(context.Background(), connect.NewRequest(&v1.ListTeamsRequest{}))
31+
if err != nil {
32+
fmt.Fprintf(os.Stderr, "Failed to list teams %v", err)
33+
return
34+
}
35+
36+
fmt.Fprintf(os.Stdout, "Retrieved teams %v", response.Msg.GetTeams())
37+
}
3738
```
39+
40+
For more examples, see [examples](./examples) directory.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package examples
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"github.com/bufbuild/connect-go"
11+
"github.com/gitpod-io/gitpod/components/public-api/go/client"
12+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
13+
"os"
14+
)
15+
16+
func ExampleClient() {
17+
token := "gitpod_pat_example.personal-access-token"
18+
gitpod, err := client.New(client.WithCredentials(token))
19+
if err != nil {
20+
fmt.Fprintf(os.Stderr, "Failed to construct gitpod client %v", err)
21+
return
22+
}
23+
24+
// use the gitpod client to access resources
25+
gitpod.Teams.ListTeams(context.Background(), connect.NewRequest(&v1.ListTeamsRequest{}))
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package examples
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"github.com/bufbuild/connect-go"
11+
"github.com/gitpod-io/gitpod/components/public-api/go/client"
12+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
13+
"os"
14+
"time"
15+
)
16+
17+
func ExampleListTeams() {
18+
token := "gitpod_pat_example.personal-access-token"
19+
20+
gitpod, err := client.New(client.WithCredentials(token))
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "Failed to construct gitpod client %v", err)
23+
return
24+
}
25+
26+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
27+
defer cancel()
28+
29+
response, err := gitpod.Teams.ListTeams(ctx, connect.NewRequest(&v1.ListTeamsRequest{}))
30+
if err != nil {
31+
fmt.Fprintf(os.Stderr, "Failed to list teams %v", err)
32+
return
33+
}
34+
35+
fmt.Fprintf(os.Stdout, "Retrieved teams %v", response.Msg.GetTeams())
36+
}

components/public-api/go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/bufbuild/connect-go v1.0.0
77
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
8+
github.com/stretchr/testify v1.7.0
89
google.golang.org/grpc v1.49.0
910
google.golang.org/protobuf v1.28.1
1011
)
@@ -27,7 +28,6 @@ require (
2728
github.com/prometheus/procfs v0.8.0 // indirect
2829
github.com/sirupsen/logrus v1.8.1 // indirect
2930
github.com/slok/go-http-metrics v0.10.0 // indirect
30-
github.com/stretchr/testify v1.7.0 // indirect
3131
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
3232
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
3333
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect

0 commit comments

Comments
 (0)