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 (115 lines) | stat: -rw-r--r-- 3,490 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package claims

import (
	"github.com/gophercloud/gophercloud"
)

// CreateOptsBuilder Builder.
type CreateOptsBuilder interface {
	ToClaimCreateRequest() (map[string]interface{}, string, error)
}

// CreateOpts params to be used with Create.
type CreateOpts struct {
	// Sets the TTL for the claim. When the claim expires un-deleted messages will be able to be claimed again.
	TTL int `json:"ttl,omitempty"`

	// Sets the Grace period for the claimed messages. The server extends the lifetime of claimed messages
	// to be at least as long as the lifetime of the claim itself, plus the specified grace period.
	Grace int `json:"grace,omitempty"`

	// Set the limit of messages returned by create.
	Limit int `q:"limit" json:"-"`
}

// ToClaimCreateRequest assembles a body and URL for a Create request based on
// the contents of a CreateOpts.
func (opts CreateOpts) ToClaimCreateRequest() (map[string]interface{}, string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return nil, q.String(), err
	}

	b, err := gophercloud.BuildRequestBody(opts, "")
	if err != nil {
		return b, "", err
	}
	return b, q.String(), err
}

// Create creates a Claim that claims messages on a specified queue.
func Create(client *gophercloud.ServiceClient, queueName string, opts CreateOptsBuilder) (r CreateResult) {
	b, q, err := opts.ToClaimCreateRequest()
	if err != nil {
		r.Err = err
		return
	}

	url := createURL(client, queueName)
	if q != "" {
		url += q
	}

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

// Get queries the specified claim for the specified queue.
func Get(client *gophercloud.ServiceClient, queueName string, claimID string) (r GetResult) {
	resp, err := client.Get(getURL(client, queueName, claimID), &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
	ToClaimUpdateMap() (map[string]interface{}, error)
}

// UpdateOpts implements UpdateOpts.
type UpdateOpts struct {
	// Update the TTL for the specified Claim.
	TTL int `json:"ttl,omitempty"`

	// Update the grace period for Messages in a specified Claim.
	Grace int `json:"grace,omitempty"`
}

// ToClaimUpdateMap assembles a request body based on the contents of
// UpdateOpts.
func (opts UpdateOpts) ToClaimUpdateMap() (map[string]interface{}, error) {
	b, err := gophercloud.BuildRequestBody(opts, "")
	if err != nil {
		return nil, err
	}
	return b, nil
}

// Update will update the options for a specified claim.
func Update(client *gophercloud.ServiceClient, queueName string, claimID string, opts UpdateOptsBuilder) (r UpdateResult) {
	b, err := opts.ToClaimUpdateMap()
	if err != nil {
		r.Err = err
		return r
	}
	resp, err := client.Patch(updateURL(client, queueName, claimID), &b, nil, &gophercloud.RequestOpts{
		OkCodes: []int{204},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// Delete will delete a Claim for a specified Queue.
func Delete(client *gophercloud.ServiceClient, queueName string, claimID string) (r DeleteResult) {
	resp, err := client.Delete(deleteURL(client, queueName, claimID), &gophercloud.RequestOpts{
		OkCodes: []int{204},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}