mirror of
https://github.com/bestnite/go-igdb.git
synced 2025-04-19 22:25:54 +08:00
37 lines
880 B
Go
37 lines
880 B
Go
package endpoint
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
type Webhooks struct {
|
|
request func(URL string, dataBody any) (*resty.Response, error)
|
|
}
|
|
|
|
func NewWebhooks(request func(URL string, dataBody any) (*resty.Response, error)) *Webhooks {
|
|
return &Webhooks{
|
|
request: request,
|
|
}
|
|
}
|
|
|
|
func (a *Webhooks) Register(endpoint EndpointName, secret, callbackUrl string) error {
|
|
dataBody := url.Values{}
|
|
dataBody.Set("url", callbackUrl)
|
|
dataBody.Set("secret", secret)
|
|
dataBody.Set("method", "update")
|
|
resp, err := a.request(fmt.Sprintf("https://api.igdb.com/v4/%s/webhooks/", endpoint), dataBody.Encode())
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to make request: %s: %w", callbackUrl, err)
|
|
}
|
|
|
|
if resp.StatusCode() == http.StatusOK {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to activate webhook: %s: %s", callbackUrl, resp.String())
|
|
}
|