-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelestio.go
66 lines (52 loc) · 1.1 KB
/
elestio.go
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
package elestio
import (
"errors"
"fmt"
"net/http"
)
const (
BaseURLV1 = "https://api.elest.io"
)
type Client struct {
BaseURL string
HTTPClient *http.Client
Email string
ApiKey string
jwt string
Project *ProjectHandler
Service *ServiceHandler
LoadBalancer *LoadBalancerHandler
}
func NewClient(email, apiKey string) (*Client, error) {
if email == "" {
return nil, errors.New("email is required")
}
if apiKey == "" {
return nil, errors.New("api key is required")
}
client := Client{
BaseURL: BaseURLV1,
HTTPClient: &http.Client{},
Email: email,
ApiKey: apiKey,
}
if err := client.signIn(); err != nil {
return nil, fmt.Errorf("failed to sign in: %s", err)
}
client.init()
return &client, nil
}
func NewUnsignedClient() *Client {
client := Client{
BaseURL: BaseURLV1,
HTTPClient: &http.Client{},
}
client.init()
return &client
}
// init sets up all the handlers.
func (c *Client) init() {
c.Project = &ProjectHandler{client: c}
c.Service = &ServiceHandler{client: c}
c.LoadBalancer = &LoadBalancerHandler{client: c}
}