-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
83 lines (75 loc) · 1.77 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gorte
import (
"net/url"
"strings"
"github.com/dhia-gharsallaoui/gorte/log"
"github.com/hashicorp/go-retryablehttp"
)
const (
defaultBaseURL = "https://digital.iservices.rte-france.com/"
)
type ClientConfig struct {
Logger log.Logger
Key string
BaseURL string `default:"https://digital.iservices.rte-france.com/"`
}
type Client struct {
logger log.Logger
client *retryablehttp.Client
baseURL *url.URL
config ClientConfig
token *AuthToken
Market *market
Consumption *consumption
Partners *partners
Generation *generation
Exchanges *exchanges
}
func NewClient(config ClientConfig) (*Client, error) {
var logger log.Logger
if config.Logger != nil {
logger = config.Logger
} else {
logger = log.NewLogger(&log.LoggerConfiguration{
Verbosity: log.Debug,
})
}
if config.Key == "" {
logger.Fatal("can't connect without the RTE token in Base 64 format. to get one subscribe to the API")
}
c := Client{logger: logger}
var err error
if config.BaseURL == "" {
config.BaseURL = defaultBaseURL
}
c.baseURL, err = setURL(config.BaseURL)
if err != nil {
return nil, err
}
c.config = config
c.client = retryablehttp.NewClient()
c.client.Logger = log.NewHTTPLogger(logger)
c.client.RetryMax = 10
token, err := c.newToken()
if err != nil {
return nil, err
}
c.token = token
c.Market = &market{client: &c}
c.Consumption = &consumption{client: &c}
c.Partners = &partners{client: &c}
c.Generation = &generation{client: &c}
c.Exchanges = &exchanges{client: &c}
logger.Info("client was successfully created!")
return &c, nil
}
func setURL(urlStr string) (*url.URL, error) {
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
url, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
return url, nil
}