-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaapi_options.go
73 lines (62 loc) · 1.79 KB
/
aapi_options.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
package emailvalidator
import (
"net/url"
)
// AbstractAPIOption specifies options for AbstractAPI client
type AbstractAPIOption struct {
baseURL *url.URL
apiVersion string
rate AARate
blocking bool
}
// AbstractAPIOptionFunc is a function type for setting AbstractAPI client options
type AbstractAPIOptionFunc func(*AbstractAPIOption) error
// WithAbstractAPIRate is used to specific rate limit on http client
func WithAbstractAPIRate(rate AARate) AbstractAPIOptionFunc {
return func(opts *AbstractAPIOption) error {
opts.rate = rate
return nil
}
}
// WithAbstractAPIBlocking is used to direct rateLimiter to wait until rate limit interval ends, if rate limit is reached.
func WithAbstractAPIBlocking() AbstractAPIOptionFunc {
return func(opts *AbstractAPIOption) error {
opts.blocking = true
return nil
}
}
// WithAbstractAPIBaseURL is used to set base url of abstract api service
func WithAbstractAPIBaseURL(url *url.URL) AbstractAPIOptionFunc {
return func(opts *AbstractAPIOption) error {
if url == nil || url.String() == "" {
return ErrEmptyBaseURL
}
opts.baseURL = url
return nil
}
}
// WithAbstractAPIVersion is used to set API version of abstract api
func WithAbstractAPIVersion(version string) AbstractAPIOptionFunc {
return func(opts *AbstractAPIOption) error {
if version == "" {
return ErrEmptyAPIVersion
}
opts.apiVersion = version
return nil
}
}
func parseAbstractAPIOptions(options ...AbstractAPIOptionFunc) (*AbstractAPIOption, error) {
defAPIURL, _ := url.Parse(defaultAbstractAPIURL)
opts := &AbstractAPIOption{
rate: AbstractAPIFree,
blocking: false,
baseURL: defAPIURL,
apiVersion: defaultAbstractAPIVersion,
}
for _, o := range options {
if err := o(opts); err != nil {
return nil, err
}
}
return opts, nil
}