This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathclient.go
More file actions
126 lines (103 loc) · 3.4 KB
/
client.go
File metadata and controls
126 lines (103 loc) · 3.4 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package coder
import (
"context"
"errors"
"net/http"
"net/url"
"time"
"golang.org/x/xerrors"
)
// ensure that DefaultClient implements Client.
var _ = Client(&DefaultClient{})
// Me is the user ID of the authenticated user.
const Me = "me"
// ClientOptions contains options for the Coder SDK Client.
type ClientOptions struct {
// BaseURL is the root URL of the Coder installation (required).
BaseURL *url.URL
// Client is the http.Client to use for requests (optional).
//
// If omitted, the http.DefaultClient will be used.
HTTPClient *http.Client
// Token is the API Token used to authenticate (optional).
//
// If Token is provided, the DefaultClient will use it to authenticate.
// If it is not provided, the client requires another type of
// credential, such as an Email/Password pair.
Token string
// Email used to authenticate with Coder.
//
// If you supply an Email and Password pair, NewClient will exchange
// these credentials for a Token during initialization. This is only
// applicable for the built-in authentication provider. The client will
// not retain these credentials in memory after NewClient returns.
Email string
// Password used to authenticate with Coder.
//
// If you supply an Email and Password pair, NewClient will exchange
// these credentials for a Token during initialization. This is only
// applicable for the built-in authentication provider. The client will
// not retain these credentials in memory after NewClient returns.
Password string
}
// NewClient creates a new default Coder SDK client.
func NewClient(opts ClientOptions) (*DefaultClient, error) {
httpClient := opts.HTTPClient
if httpClient == nil {
httpClient = http.DefaultClient
}
if opts.BaseURL == nil {
return nil, errors.New("the BaseURL parameter is required")
}
token := opts.Token
if token == "" {
if opts.Email == "" || opts.Password == "" {
return nil, errors.New("either an API Token or email/password pair are required")
}
// Exchange the username/password for a token.
// We apply a default timeout of 5 seconds here.
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
resp, err := LoginWithPassword(ctx, httpClient, opts.BaseURL, &LoginRequest{
Email: opts.Email,
Password: opts.Password,
})
if err != nil {
return nil, xerrors.Errorf("failed to login with email/password: %w", err)
}
token = resp.SessionToken
if token == "" {
return nil, errors.New("server returned an empty session token")
}
}
// TODO: add basic validation to make sure the token looks OK.
client := &DefaultClient{
baseURL: opts.BaseURL,
httpClient: httpClient,
token: token,
}
return client, nil
}
// DefaultClient is the default implementation of the coder.Client
// interface.
//
// The empty value is meaningless and the fields are unexported;
// use NewClient to create an instance.
type DefaultClient struct {
// baseURL is the URL (scheme, hostname/IP address, port,
// path prefix of the Coder installation)
baseURL *url.URL
// httpClient is the http.Client used to issue requests.
httpClient *http.Client
// token is the API Token credential.
token string
}
// Token returns the API Token used to authenticate.
func (c *DefaultClient) Token() string {
return c.token
}
// BaseURL returns the BaseURL configured for this Client.
func (c *DefaultClient) BaseURL() url.URL {
return *c.baseURL
}