-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
61 lines (49 loc) · 1.48 KB
/
client.go
File metadata and controls
61 lines (49 loc) · 1.48 KB
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
package api
import (
"context"
"net/url"
"github.com/codesphere-cloud/cs-go/api/openapi_client"
)
type Client struct {
ctx context.Context
api *openapi_client.APIClient
}
type Configuration struct {
BaseUrl *url.URL
Token string
}
func (c Configuration) GetApiUrl() *url.URL {
if c.BaseUrl != nil {
return c.BaseUrl
}
// url.Parse() won't return an error on this static string,
// hence it's safe to ignore it.
defaultUrl, _ := url.Parse("https://codesphere.com/api")
return defaultUrl
}
func NewClient(ctx context.Context, opts Configuration) *Client {
cfg := openapi_client.NewConfiguration()
cfg.Servers = []openapi_client.ServerConfiguration{{
URL: opts.BaseUrl.String(),
}}
return &Client{
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: openapi_client.NewAPIClient(cfg),
}
}
func (c *Client) ListDataCenters() ([]DataCenter, error) {
datacenters, _, err := c.api.MetadataAPI.MetadataGetDatacenters(c.ctx).Execute()
return datacenters, err
}
func (c *Client) ListWorkspacePlans() ([]WorkspacePlan, error) {
plans, _, err := c.api.MetadataAPI.MetadataGetWorkspacePlans(c.ctx).Execute()
return plans, err
}
func (c *Client) ListTeams() ([]Team, error) {
teams, _, err := c.api.TeamsAPI.TeamsListTeams(c.ctx).Execute()
return teams, err
}
func (c *Client) ListWorkspaces(teamId int) ([]Workspace, error) {
workspaces, _, err := c.api.WorkspacesAPI.WorkspacesListWorkspaces(c.ctx, float32(teamId)).Execute()
return workspaces, err
}