diff --git a/tariff/ostrom.go b/tariff/ostrom.go index 4f4e582a2..f71b69376 100644 --- a/tariff/ostrom.go +++ b/tariff/ostrom.go @@ -25,6 +25,7 @@ type Ostrom struct { *embed *request.Helper log *util.Logger + env string zip string contractType string cityId int // Required for the Fair tariff types @@ -42,7 +43,7 @@ func init() { func ensureContractEx(cid int64, contracts []ostrom.Contract) (ostrom.Contract, error) { var zero ostrom.Contract - if cid != -1 { + if cid != 0 { // cid defined for _, contract := range contracts { if cid == contract.Id { @@ -58,13 +59,15 @@ func ensureContractEx(cid int64, contracts []ostrom.Contract) (ostrom.Contract, } func NewOstromFromConfig(other map[string]any) (api.Tariff, error) { - var cc struct { + cc := struct { ClientId string ClientSecret string + Environment string Contract int64 + }{ + Environment: "production", } - cc.Contract = -1 if err := util.DecodeOther(other, &cc); err != nil { return nil, err } @@ -78,6 +81,7 @@ func NewOstromFromConfig(other map[string]any) (api.Tariff, error) { t := &Ostrom{ log: log, + env: cc.Environment, basic: basic, Helper: request.NewHelper(log), data: util.NewMonitor[api.Rates](2 * time.Hour), @@ -123,10 +127,26 @@ func NewOstromFromConfig(other map[string]any) (api.Tariff, error) { return t, nil } +func (t *Ostrom) apiUri(path string) string { + uri := ostrom.URI_API_PRODUCTION + if t.env == "sandbox" { + uri = ostrom.URI_API_SANDBOX + } + return uri + path +} + +func (t *Ostrom) authUri(path string) string { + uri := ostrom.URI_AUTH_PRODUCTION + if t.env == "sandbox" { + uri = ostrom.URI_AUTH_SANDBOX + } + return uri + path +} + func (t *Ostrom) getContracts() ([]ostrom.Contract, error) { var res ostrom.Contracts - uri := ostrom.URI_API + "/contracts" + uri := t.apiUri("/contracts") err := t.GetJSON(uri, &res) return res.Data, err } @@ -164,7 +184,7 @@ func (t *Ostrom) getFixedPrice() (float64, error) { } func (t *Ostrom) refreshToken() (*oauth2.Token, error) { - uri := ostrom.URI_AUTH + "/oauth2/token" + uri := t.authUri("/oauth2/token") data := url.Values{"grant_type": {"client_credentials"}} req, _ := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{ "Authorization": t.basic, @@ -225,7 +245,7 @@ func (t *Ostrom) run(done chan error) { "zip": {t.zip}, } - uri := fmt.Sprintf("%s/spot-prices?%s", ostrom.URI_API, params.Encode()) + uri := t.apiUri(fmt.Sprintf("/spot-prices?%s", params.Encode())) if err := backoff.Retry(func() error { return backoffPermanentError(t.GetJSON(uri, &res)) }, bo()); err != nil { diff --git a/tariff/ostrom/api.go b/tariff/ostrom/api.go index 02dfa1813..e487f2279 100644 --- a/tariff/ostrom/api.go +++ b/tariff/ostrom/api.go @@ -14,8 +14,6 @@ const ( URI_API_SANDBOX = "https://sandbox.ostrom-api.io" URI_GET_CITYID = "https://api.ostrom.de/v1/addresses/cities" URI_GET_STATIC_PRICE = "https://api.ostrom.de/v1/tariffs/city-id" - URI_AUTH = URI_AUTH_PRODUCTION - URI_API = URI_API_PRODUCTION ) const (