// ReadWebhook gets a single webhook based on its ID.
327857
func ReadWebhook(c *messagebird.Client, id string) (*Webhook, error) {
	webhook := &Webhook{}
	if err := request(c, webhook, http.MethodGet, webhooksPath+"/"+id, nil); err != nil {
		return nil, err
	}

	return webhook, nil
}
619311
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {

	if config.Certificate == nil {
		v := url.Values{}
		v.Add("url", config.URL.String())
		if config.MaxConnections != 0 {
			v.Add("max_connections", strconv.Itoa(config.MaxConnections))
		}

		return bot.MakeRequest("setWebhook", v)
	}

	params := make(map[string]string)
	params["url"] = config.URL.String()
	if config.MaxConnections != 0 {
		params["max_connections"] = strconv.Itoa(config.MaxConnections)
	}

	resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate)
	if err != nil {
		return APIResponse{}, err
	}

	return resp, nil
}
349107
func (s *Service) AddOnWebhookDelete(ctx context.Context, addOnIdentity string, appWebhookIdentity string) (*AddOnWebhookDeleteResult, error) {
	var addOnWebhook AddOnWebhookDeleteResult
	return &addOnWebhook, s.Delete(ctx, &addOnWebhook, fmt.Sprintf("/addons/%v/webhooks/%v", addOnIdentity, appWebhookIdentity))
}
1039397
func ValidateWebhookThrottleConfig(c *auditregistration.WebhookThrottleConfig, fldPath *field.Path) field.ErrorList {
	var allErrs field.ErrorList
	if c.QPS != nil && *c.QPS <= 0 {
		allErrs = append(allErrs, field.Invalid(fldPath.Child("qps"), c.QPS, "qps must be a positive number"))
	}
	if c.Burst != nil && *c.Burst <= 0 {
		allErrs = append(allErrs, field.Invalid(fldPath.Child("burst"), c.Burst, "burst must be a positive number"))
	}
	return allErrs
}
904485
func (s *Service) AddOnWebhookInfo(ctx context.Context, addOnIdentity string, appWebhookIdentity string) (*AddOnWebhookInfoResult, error) {
	var addOnWebhook AddOnWebhookInfoResult
	return &addOnWebhook, s.Get(ctx, &addOnWebhook, fmt.Sprintf("/addons/%v/webhooks/%v", addOnIdentity, appWebhookIdentity), nil, nil)
}
1307728
func NewWebhookTarget(id string, args WebhookArgs) *WebhookTarget {
	return &WebhookTarget{
		id:   event.TargetID{ID: id, Name: "webhook"},
		args: args,
		httpClient: &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{RootCAs: args.RootCAs},
				DialContext: (&net.Dialer{
					Timeout:   5 * time.Second,
					KeepAlive: 5 * time.Second,
				}).DialContext,
				TLSHandshakeTimeout:   3 * time.Second,
				ResponseHeaderTimeout: 3 * time.Second,
				ExpectContinueTimeout: 2 * time.Second,
			},
		},
	}
}
1211520
func UpdateWebhook(w *Webhook) error {
	_, err := x.Id(w.ID).AllCols().Update(w)
	return err
}
677436
func (c oauthClient) Webhook(webhookID uint) (wl.Webhook, error) {
	allWebhooks, err := c.Webhooks()
	for _, w := range allWebhooks {
		if w.ID == webhookID {
			return w, err
		}
	}

	return wl.Webhook{}, fmt.Errorf("webhook not found")
}
1162394
func CreateWebHook(client *messagebird.Client, url, token string) (*Webhook, error) {
	data := struct {
		URL   string `json:"url"`
		Token string `json:"token,omitempty"`
	}{
		URL:   url,
		Token: token,
	}
	var resp struct {
		Data []Webhook `json:"data"`
	}
	if err := client.Request(&resp, http.MethodPost, apiRoot+"/webhooks/", data); err != nil {
		return nil, err
	}
	return &resp.Data[0], nil
}
891046
func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
	webhooks := make([]*Webhook, 0, 5)
	return webhooks, e.Where("repo_id = ?", repoID).And("is_active = ?", true).Find(&webhooks)
}
796977
func (c oauthClient) Webhooks() ([]wl.Webhook, error) {
	lists, err := c.Lists()
	if err != nil {
		return nil, err
	}

	listCount := len(lists)
	c.logger.Debug(
		"webhooks",
		map[string]interface{}{"listCount": listCount},
	)

	webhooksChan := make(chan []wl.Webhook, listCount)
	idErrChan := make(chan idErr, listCount)
	for _, l := range lists {
		go func(list wl.List) {
			c.logger.Debug(
				"webhooks - getting webhooks for list",
				map[string]interface{}{"listID": list.ID},
			)
			webhooks, err := c.WebhooksForListID(list.ID)
			idErrChan <- idErr{idType: "list", id: list.ID, err: err}
			webhooksChan <- webhooks
		}(l)
	}

	e := multiIDErr{}
	for i := 0; i < listCount; i++ {
		idErr := <-idErrChan
		if idErr.err != nil {
			c.logger.Debug(
				"webhooks - error received getting webhooks for list",
				map[string]interface{}{"listID": idErr.id, "err": err},
			)
			e.addError(idErr)
		}
	}

	totalWebhooks := []wl.Webhook{}
	for i := 0; i < listCount; i++ {
		webhooks := <-webhooksChan
		totalWebhooks = append(totalWebhooks, webhooks...)
	}

	if len(e.errors()) > 0 {
		return totalWebhooks, e
	}

	return totalWebhooks, nil
}
345975
func (s *Service) AppWebhookList(ctx context.Context, appIdentity string, lr *ListRange) (AppWebhookListResult, error) {
	var appWebhook AppWebhookListResult
	return appWebhook, s.Get(ctx, &appWebhook, fmt.Sprintf("/apps/%v/webhooks", appIdentity), nil, lr)
}
1043029
func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
	u, _ := url.Parse(link)

	return WebhookConfig{
		URL:         u,
		Certificate: file,
	}
}
1337600
func (c oauthClient) DeleteWebhook(webhook wl.Webhook) error {
	url := fmt.Sprintf(
		"%s/webhooks/%d",
		c.apiURL,
		webhook.ID,
	)

	req, err := c.newDeleteRequest(url)
	if err != nil {
		return err
	}

	resp, err := c.do(req)
	if err != nil {
		return err
	}

	if resp.StatusCode != http.StatusNoContent {
		return fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusNoContent)
	}

	return nil
}
1281686
func deleteWebhook(bean *Webhook) (err error) {
	sess := x.NewSession()
	defer sess.Close()
	if err = sess.Begin(); err != nil {
		return err
	}

	if _, err = sess.Delete(bean); err != nil {
		return err
	} else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
		return err
	}

	return sess.Commit()
}
1320464
func (s *WebhooksService) ListWebhooks(accountID string, _ *ListOptions) (*webhooksResponse, error) {
	path := versioned(webhookPath(accountID, 0))
	webhooksResponse := &webhooksResponse{}

	resp, err := s.client.get(path, webhooksResponse)
	if err != nil {
		return webhooksResponse, err
	}

	webhooksResponse.HttpResponse = resp
	return webhooksResponse, nil
}
802366
func (s *Session) ChannelWebhooks(channelID string) (st []*Webhook, err error) {

	body, err := s.RequestWithBucketID("GET", EndpointChannelWebhooks(channelID), nil, EndpointChannelWebhooks(channelID))
	if err != nil {
		return
	}

	err = unmarshal(body, &st)

	return
}