/
kanthor.go
46 lines (40 loc) · 1.29 KB
/
kanthor.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
package kanthorgo
import (
"fmt"
"net/http"
"github.com/kanthorlabs/common/idx"
"github.com/kanthorlabs/common/passport/utils"
"github.com/kanthorlabs/kanthor-go/internal/openapi"
"github.com/kanthorlabs/kanthor-go/internal/project"
)
func New(credentials string, options ...Option) (*Client, error) {
authorization := utils.SchemeBasic + credentials
creds, err := utils.ParseBasicCredentials(authorization)
if err != nil {
return nil, err
}
conf := openapi.NewConfiguration()
// set host that is associated with the credentials
conf.Host = project.Host(creds.Region)
// set scheme automatically based on the host
conf.Scheme = "https"
conf.AddDefaultHeader("Authorization", authorization)
conf.AddDefaultHeader("User-Agent", fmt.Sprintf("Kanthor/%s", project.Version()))
conf.Middleware = func(r *http.Request) {
r.Header.Set("Idempotency-Key", idx.New("ik"))
}
// allow user override default value with their configuration
for _, configure := range options {
configure(conf)
}
api := openapi.NewAPIClient(conf)
return &Client{
Version: project.Version(),
Configuration: conf,
Account: &Account{api: api},
Application: &Application{api: api},
Endpoint: &Endpoint{api: api},
Route: &Route{api: api},
Message: &Message{api: api},
}, nil
}