This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession.go
77 lines (68 loc) · 2.06 KB
/
session.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
67
68
69
70
71
72
73
74
75
76
77
package improvmx
import (
"fmt"
"occult.work/doze"
)
type Session struct {
client *doze.Client
Credentials *CredentialEndpoint
Account *AccountEndpoint
Domains *DomainEndpoint
Aliases *AliasEndpoint
}
type SessionOption func(*Session) error
func New(token string, options ...SessionOption) (*Session, error) {
session := &Session{}
session.client = doze.NewClient().
SetAuthToken(fmt.Sprintf("api:%s", token)).
SetAuthScheme("Basic").
SetBaseURL(BaseURLv3).
SetError(&Error{})
for _, option := range options {
if error := option(session); error != nil {
return nil, error
}
}
session.Credentials = (*CredentialEndpoint)(session.client)
session.Account = (*AccountEndpoint)(session.client)
session.Domains = (*DomainEndpoint)(session.client)
session.Aliases = (*AliasEndpoint)(session.client)
return session, nil
}
// Sets the session client directly. This is provided for extreme user needs.
// However, the resty.Client.SetError type is still set to Error, and the host
// url is set toe BaseURLv3. Use WithBaseURL after calling WithClient if
// overriding the Host URL is required.
func WithClient(client *doze.Client) SessionOption {
return func(session *Session) error {
if client == nil {
return fmt.Errorf("WithClient was passed a nil value")
}
client.SetError(&Error{}).SetBaseURL(BaseURLv3)
session.client = client
return nil
}
}
// Sets the User-Agent to the given string on the session. By default, this is
// unset.
func WithUserAgent(agent string) SessionOption {
return func(session *Session) error {
session.client.SetUserAgent(agent)
return nil
}
}
// Sets the Base URL to query. This is typically BaseURLv3, however for both
// testing and possible future sandboxing support, this option is provided.
func WithBaseURL(url string) SessionOption {
return func(session *Session) error {
session.client.SetBaseURL(url)
return nil
}
}
// Enables the debug mode on the Session's internal http client.
func WithDebug() SessionOption {
return func(session *Session) error {
session.client.SetDebug()
return nil
}
}