mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
681541f41b
adds possibilities to cap authenticated requests and execution seconds of actions on a defined intervall
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
)
|
|
|
|
// ReportUsage calls notification hooks and emits the notified events
|
|
func (c *Commands) ReportUsage(ctx context.Context, dueNotifications []*quota.NotifiedEvent) error {
|
|
for _, notification := range dueNotifications {
|
|
|
|
if err := notify(ctx, notification); err != nil {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if _, err := c.eventstore.Push(ctx, notification); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func notify(ctx context.Context, notification *quota.NotifiedEvent) error {
|
|
payload, err := json.Marshal(notification)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, notification.CallURL, bytes.NewReader(payload))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = resp.Body.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return fmt.Errorf("calling url %s returned %s", notification.CallURL, resp.Status)
|
|
}
|
|
|
|
return nil
|
|
}
|