Ostrom: add sandbox support (#29751)

This commit is contained in:
andig 2026-05-19 18:25:44 +02:00 • committed by GitHub
parent ca386b72f2
commit 3588040e71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 8 deletions

View file

@ -25,6 +25,7 @@ type Ostrom struct {
*embed *embed
*request.Helper *request.Helper
log *util.Logger log *util.Logger
env string
zip string zip string
contractType string contractType string
cityId int // Required for the Fair tariff types cityId int // Required for the Fair tariff types
@ -42,7 +43,7 @@ func init() {
func ensureContractEx(cid int64, contracts []ostrom.Contract) (ostrom.Contract, error) { func ensureContractEx(cid int64, contracts []ostrom.Contract) (ostrom.Contract, error) {
var zero ostrom.Contract var zero ostrom.Contract
if cid != -1 { if cid != 0 {
// cid defined // cid defined
for _, contract := range contracts { for _, contract := range contracts {
if cid == contract.Id { 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) { func NewOstromFromConfig(other map[string]any) (api.Tariff, error) {
var cc struct { cc := struct {
ClientId string ClientId string
ClientSecret string ClientSecret string
Environment string
Contract int64 Contract int64
}{
Environment: "production",
} }
cc.Contract = -1
if err := util.DecodeOther(other, &cc); err != nil { if err := util.DecodeOther(other, &cc); err != nil {
return nil, err return nil, err
} }
@ -78,6 +81,7 @@ func NewOstromFromConfig(other map[string]any) (api.Tariff, error) {
t := &Ostrom{ t := &Ostrom{
log: log, log: log,
env: cc.Environment,
basic: basic, basic: basic,
Helper: request.NewHelper(log), Helper: request.NewHelper(log),
data: util.NewMonitor[api.Rates](2 * time.Hour), data: util.NewMonitor[api.Rates](2 * time.Hour),
@ -123,10 +127,26 @@ func NewOstromFromConfig(other map[string]any) (api.Tariff, error) {
return t, nil 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) { func (t *Ostrom) getContracts() ([]ostrom.Contract, error) {
var res ostrom.Contracts var res ostrom.Contracts
uri := ostrom.URI_API + "/contracts" uri := t.apiUri("/contracts")
err := t.GetJSON(uri, &res) err := t.GetJSON(uri, &res)
return res.Data, err return res.Data, err
} }
@ -164,7 +184,7 @@ func (t *Ostrom) getFixedPrice() (float64, error) {
} }
func (t *Ostrom) refreshToken() (*oauth2.Token, 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"}} data := url.Values{"grant_type": {"client_credentials"}}
req, _ := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{ req, _ := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{
"Authorization": t.basic, "Authorization": t.basic,
@ -225,7 +245,7 @@ func (t *Ostrom) run(done chan error) {
"zip": {t.zip}, "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 { if err := backoff.Retry(func() error {
return backoffPermanentError(t.GetJSON(uri, &res)) return backoffPermanentError(t.GetJSON(uri, &res))
}, bo()); err != nil { }, bo()); err != nil {

View file

@ -14,8 +14,6 @@ const (
URI_API_SANDBOX = "https://sandbox.ostrom-api.io" URI_API_SANDBOX = "https://sandbox.ostrom-api.io"
URI_GET_CITYID = "https://api.ostrom.de/v1/addresses/cities" URI_GET_CITYID = "https://api.ostrom.de/v1/addresses/cities"
URI_GET_STATIC_PRICE = "https://api.ostrom.de/v1/tariffs/city-id" URI_GET_STATIC_PRICE = "https://api.ostrom.de/v1/tariffs/city-id"
URI_AUTH = URI_AUTH_PRODUCTION
URI_API = URI_API_PRODUCTION
) )
const ( const (