-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory.go
More file actions
41 lines (32 loc) · 1.04 KB
/
factory.go
File metadata and controls
41 lines (32 loc) · 1.04 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
package mailcow
import (
"errors"
"go.bnck.me/mailcow/internal/transport"
"net/http"
)
// New creates a new Client with username, password and context
func New(apiHost, apiKey string) (*Client, error) {
return NewWithOptions(WithHost(apiHost), WithAPIKey(apiKey))
}
// NewWithClient creates a new Client with a given http.Client
func NewWithClient(httpClient *http.Client, apiHost, apiKey string) (*Client, error) {
return NewWithOptions(WithHost(apiHost), WithHTTPClient(httpClient), WithAPIKey(apiKey))
}
// NewWithOptions creates a new Client with given options
func NewWithOptions(options ...ClientOption) (*Client, error) {
c := &Client{}
// always create a base transport it can be overwritten with options
c.transport = transport.New("")
// run given options
for _, option := range options {
option(c)
}
// check if there are credentials
if !c.transport.HasCredentials() {
return nil, errors.New("no api credentials supplied")
}
if c.transport.BaseURL == "" {
return nil, errors.New("no api host supplied")
}
return c, nil
}