Ostrom: add sandbox support (#29751)
This commit is contained in:
parent
ca386b72f2
commit
3588040e71
2 changed files with 26 additions and 8 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue