casdoor/object/webhook_util.go

53 lines
1.4 KiB
Go
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-11-07 23:53:17 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"net/http"
"strings"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/util"
2023-08-19 23:15:02 +08:00
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
2021-11-07 23:53:17 +08:00
)
2023-08-19 23:15:02 +08:00
func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *User) error {
2021-11-07 23:53:17 +08:00
client := &http.Client{}
2023-08-19 23:15:02 +08:00
type RecordEx struct {
casvisorsdk.Record
ExtendedUser *User `xorm:"-" json:"extendedUser"`
}
recordEx := &RecordEx{
Record: *record,
ExtendedUser: extendedUser,
}
body := strings.NewReader(util.StructToJson(recordEx))
2021-11-07 23:53:17 +08:00
2021-12-26 19:56:02 +08:00
req, err := http.NewRequest(webhook.Method, webhook.Url, body)
2021-11-07 23:53:17 +08:00
if err != nil {
return err
}
req.Header.Set("Content-Type", webhook.ContentType)
2021-12-26 20:40:13 +08:00
for _, header := range webhook.Headers {
req.Header.Set(header.Name, header.Value)
}
2021-11-07 23:53:17 +08:00
_, err = client.Do(req)
return err
}