File: requests.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (54 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package webhooks

import (
	"fmt"
	"net/url"

	"github.com/gophercloud/gophercloud"
)

// TriggerOpts represents options used for triggering an action
type TriggerOpts struct {
	V      string `q:"V" required:"true"`
	Params map[string]string
}

// TriggerOptsBuilder Query string builder interface for webhooks
type TriggerOptsBuilder interface {
	ToWebhookTriggerQuery() (string, error)
}

// Query string builder for webhooks
func (opts TriggerOpts) ToWebhookTriggerQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	params := q.Query()

	for k, v := range opts.Params {
		params.Add(k, v)
	}

	q = &url.URL{RawQuery: params.Encode()}
	return q.String(), err
}

// Trigger an action represented by a webhook.
func Trigger(client *gophercloud.ServiceClient, id string, opts TriggerOptsBuilder) (r TriggerResult) {
	url := triggerURL(client, id)
	if opts != nil {
		query, err := opts.ToWebhookTriggerQuery()
		if err != nil {
			r.Err = err
			return
		}
		url += query
	} else {
		r.Err = fmt.Errorf("Must contain V for TriggerOpt")
		return
	}

	resp, err := client.Post(url, nil, &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}