Path: blob/main/components/public-api/go/client/client.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package client56import (7"errors"8"fmt"9"net/http"1011"github.com/bufbuild/connect-go"12gitpod_experimental_v1connect "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"13)1415type Gitpod struct {16cfg *options1718Workspaces gitpod_experimental_v1connect.WorkspacesServiceClient19Editors gitpod_experimental_v1connect.EditorServiceClient20Teams gitpod_experimental_v1connect.TeamsServiceClient21Projects gitpod_experimental_v1connect.ProjectsServiceClient22PersonalAccessTokens gitpod_experimental_v1connect.TokensServiceClient23IdentityProvider gitpod_experimental_v1connect.IdentityProviderServiceClient24User gitpod_experimental_v1connect.UserServiceClient25}2627func New(options ...Option) (*Gitpod, error) {28opts, err := evaluateOptions(defaultOptions(), options...)29if err != nil {30return nil, fmt.Errorf("failed to evaluate client options: %w", err)31}3233if opts.credentials == "" {34return nil, errors.New("no authentication credentials specified")35}3637client := opts.client38url := opts.url3940serviceOpts := []connect.ClientOption{41connect.WithInterceptors(42AuthorizationInterceptor(opts.credentials),43),44}4546return &Gitpod{47cfg: opts,48Teams: gitpod_experimental_v1connect.NewTeamsServiceClient(client, url, serviceOpts...),49Projects: gitpod_experimental_v1connect.NewProjectsServiceClient(client, url, serviceOpts...),50PersonalAccessTokens: gitpod_experimental_v1connect.NewTokensServiceClient(client, url, serviceOpts...),51Workspaces: gitpod_experimental_v1connect.NewWorkspacesServiceClient(client, url, serviceOpts...),52Editors: gitpod_experimental_v1connect.NewEditorServiceClient(client, url, serviceOpts...),53IdentityProvider: gitpod_experimental_v1connect.NewIdentityProviderServiceClient(client, url, serviceOpts...),54User: gitpod_experimental_v1connect.NewUserServiceClient(client, url, serviceOpts...),55}, nil56}5758type Option func(opts *options) error5960func WithURL(url string) Option {61return func(opts *options) error {62opts.url = url63return nil64}65}6667func WithCredentials(token string) Option {68return func(opts *options) error {69opts.credentials = token70return nil71}72}7374func WithHTTPClient(client *http.Client) Option {75return func(opts *options) error {76opts.client = client77return nil78}79}8081type options struct {82url string83client *http.Client84credentials string85}8687func defaultOptions() *options {88return &options{89url: "https://api.gitpod.io",90client: http.DefaultClient,91}92}9394func evaluateOptions(base *options, opts ...Option) (*options, error) {95for _, opt := range opts {96if err := opt(base); err != nil {97return nil, fmt.Errorf("failed to evaluate options: %w", err)98}99}100101return base, nil102}103104105