-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·115 lines (93 loc) · 3.24 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package goEpexSpot
import (
"context"
"net/http"
"time"
"github.com/timebis/goEpexSpot/internal/clients/rte/token"
"github.com/timebis/goEpexSpot/internal/clients/rte/swagger"
errors "github.com/pkg/errors"
)
type EpexSpotDayAhead struct {
// Start date for data requested
StartDate time.Time `json:"start_date,omitempty"`
// End date for requested data
EndDate time.Time `json:"end_date,omitempty"`
// Date updated
UpdatedDate time.Time `json:"updated_date,omitempty"`
Values []EpexSpotResult `json:"values,omitempty"`
}
type EpexSpotResult struct {
// Start time interval
StartDate time.Time `json:"start_date,omitempty"`
// End time interval
EndDate time.Time `json:"end_date,omitempty"`
// Volume of the electricity market (in MW)
Value_mw float32 `json:"value,omitempty"`
// Price (in €/MWh)
Price_eur_per_mwh float32 `json:"price,omitempty"`
}
// AuthOptions holds authentication details
type AuthOptions struct {
Username string
Password string
BearerToken string
}
type Country string
const (
France Country = "FR"
)
func GetEpexSpot(countryCode Country, authOpts ...AuthOptions) (data EpexSpotDayAhead, err error) {
var auth AuthOptions
if len(authOpts) > 0 {
auth = authOpts[0]
}
switch countryCode {
case France:
return rte(auth)
default:
return data, errors.New("country code not supported")
}
}
func rte(auth AuthOptions) (epexSpotDayAhead EpexSpotDayAhead, err error) {
// check if the auth options are valid given rte API
if auth.BearerToken == "" && (auth.Username == "" || auth.Password == "") {
return EpexSpotDayAhead{}, errors.New("BearerToken or {Username, Password} must be provided for french API")
}
bearerToken := auth.BearerToken
// Fetch the bearer token if not provided
if auth.BearerToken == "" {
basicAuthUsername := auth.Username
basicAuthPassword := auth.Password
bearerToken, err = token.FetchBearerToken(basicAuthUsername, basicAuthPassword)
if err != nil {
return epexSpotDayAhead, errors.Wrap(err, "Error while fetching bearer token")
}
}
// Create a new API client configuration
cfg := swagger.NewConfiguration()
cfg.HTTPClient = &http.Client{}
cfg.AddDefaultHeader("Authorization", "Bearer "+bearerToken) // Add the Authorization header with the Bearer token
client := swagger.NewAPIClient(cfg)
frenchData, resp, err := client.DefaultApi.GetFrancePowerExchanges(context.Background())
_ = resp
if err != nil {
return epexSpotDayAhead, errors.Wrap(err, "Error while fetching data from RTE API")
}
// cast the response to the expected format
epexSpotDayAhead = convertFenchResultToStandartResult(frenchData)
return epexSpotDayAhead, err
}
func convertFenchResultToStandartResult(frenchData swagger.RespData) (standartData EpexSpotDayAhead) {
standartData.StartDate = frenchData.FrancePowerExchange[0].StartDate
standartData.EndDate = frenchData.FrancePowerExchange[0].EndDate
standartData.UpdatedDate = frenchData.FrancePowerExchange[0].UpdatedDate
for _, value := range frenchData.FrancePowerExchange[0].Values {
standartData.Values = append(standartData.Values, EpexSpotResult{
StartDate: value.StartDate,
EndDate: value.EndDate,
Value_mw: value.Value,
Price_eur_per_mwh: value.Price,
})
}
return standartData
}