-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
53 lines (44 loc) · 1001 Bytes
/
client.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
package novapost
import (
"encoding/xml"
"net/http"
)
const (
JSONUrl = "https://api.novaposhta.ua/v2.0/json/"
XMLUrl = "https://api.novaposhta.ua/v2.0/xml/"
)
type Client struct {
apiKey string
HTTPClient *http.Client
Url string
Marshaler func(any) ([]byte, error)
Unmarshaler func([]byte, any) error
}
type Option func(*Client)
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) {
c.HTTPClient = httpClient
}
}
func WithURL(url string) Option {
return func(c *Client) {
c.Url = url
}
}
func WithMarshaler(marshaler func(any) ([]byte, error)) Option {
return func(c *Client) {
c.Marshaler = marshaler
}
}
func WithUnmarshaler(unmarshaler func([]byte, any) error) Option {
return func(c *Client) {
c.Unmarshaler = unmarshaler
}
}
func NewClient(apiKey string, options ...Option) *Client {
c := &Client{apiKey, &http.Client{}, XMLUrl, xml.Marshal, xml.Unmarshal}
for _, option := range options {
option(c)
}
return c
}