Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api/go/client/client.go
2500 views
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 client
6
7
import (
8
"errors"
9
"fmt"
10
"net/http"
11
12
"github.com/bufbuild/connect-go"
13
gitpod_experimental_v1connect "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
14
)
15
16
type Gitpod struct {
17
cfg *options
18
19
Workspaces gitpod_experimental_v1connect.WorkspacesServiceClient
20
Editors gitpod_experimental_v1connect.EditorServiceClient
21
Teams gitpod_experimental_v1connect.TeamsServiceClient
22
Projects gitpod_experimental_v1connect.ProjectsServiceClient
23
PersonalAccessTokens gitpod_experimental_v1connect.TokensServiceClient
24
IdentityProvider gitpod_experimental_v1connect.IdentityProviderServiceClient
25
User gitpod_experimental_v1connect.UserServiceClient
26
}
27
28
func New(options ...Option) (*Gitpod, error) {
29
opts, err := evaluateOptions(defaultOptions(), options...)
30
if err != nil {
31
return nil, fmt.Errorf("failed to evaluate client options: %w", err)
32
}
33
34
if opts.credentials == "" {
35
return nil, errors.New("no authentication credentials specified")
36
}
37
38
client := opts.client
39
url := opts.url
40
41
serviceOpts := []connect.ClientOption{
42
connect.WithInterceptors(
43
AuthorizationInterceptor(opts.credentials),
44
),
45
}
46
47
return &Gitpod{
48
cfg: opts,
49
Teams: gitpod_experimental_v1connect.NewTeamsServiceClient(client, url, serviceOpts...),
50
Projects: gitpod_experimental_v1connect.NewProjectsServiceClient(client, url, serviceOpts...),
51
PersonalAccessTokens: gitpod_experimental_v1connect.NewTokensServiceClient(client, url, serviceOpts...),
52
Workspaces: gitpod_experimental_v1connect.NewWorkspacesServiceClient(client, url, serviceOpts...),
53
Editors: gitpod_experimental_v1connect.NewEditorServiceClient(client, url, serviceOpts...),
54
IdentityProvider: gitpod_experimental_v1connect.NewIdentityProviderServiceClient(client, url, serviceOpts...),
55
User: gitpod_experimental_v1connect.NewUserServiceClient(client, url, serviceOpts...),
56
}, nil
57
}
58
59
type Option func(opts *options) error
60
61
func WithURL(url string) Option {
62
return func(opts *options) error {
63
opts.url = url
64
return nil
65
}
66
}
67
68
func WithCredentials(token string) Option {
69
return func(opts *options) error {
70
opts.credentials = token
71
return nil
72
}
73
}
74
75
func WithHTTPClient(client *http.Client) Option {
76
return func(opts *options) error {
77
opts.client = client
78
return nil
79
}
80
}
81
82
type options struct {
83
url string
84
client *http.Client
85
credentials string
86
}
87
88
func defaultOptions() *options {
89
return &options{
90
url: "https://api.gitpod.io",
91
client: http.DefaultClient,
92
}
93
}
94
95
func evaluateOptions(base *options, opts ...Option) (*options, error) {
96
for _, opt := range opts {
97
if err := opt(base); err != nil {
98
return nil, fmt.Errorf("failed to evaluate options: %w", err)
99
}
100
}
101
102
return base, nil
103
}
104
105