-
Notifications
You must be signed in to change notification settings - Fork 21
/
peek.go
85 lines (69 loc) · 2.01 KB
/
peek.go
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
package messages
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
type PeekInput struct {
// NumberOfMessages specifies the (maximum) number of messages that should be peak'd from the front of the queue.
// This can be a maximum of 32.
NumberOfMessages int
}
// Peek retrieves one or more messages from the front of the queue, but doesn't alter the visibility of the messages
func (c Client) Peek(ctx context.Context, queueName string, input PeekInput) (resp QueueMessagesListResponse, err error) {
if queueName == "" {
return resp, fmt.Errorf("`queueName` cannot be an empty string")
}
if strings.ToLower(queueName) != queueName {
return resp, fmt.Errorf("`queueName` must be a lower-cased string")
}
if input.NumberOfMessages < 1 || input.NumberOfMessages > 32 {
return resp, fmt.Errorf("`input.NumberOfMessages` must be between 1 and 32")
}
opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
http.StatusOK,
},
HttpMethod: http.MethodGet,
OptionsObject: peekOptions{
numberOfMessages: input.NumberOfMessages,
},
Path: fmt.Sprintf("/%s/messages", queueName),
}
req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
err = fmt.Errorf("building request: %+v", err)
return
}
resp.HttpResponse, err = req.Execute(ctx)
if err != nil {
err = fmt.Errorf("executing request: %+v", err)
return
}
if resp.HttpResponse != nil {
if err = resp.HttpResponse.Unmarshal(&resp); err != nil {
return resp, fmt.Errorf("unmarshalling response: %+v", err)
}
}
return
}
type peekOptions struct {
numberOfMessages int
}
func (p peekOptions) ToHeaders() *client.Headers {
return nil
}
func (p peekOptions) ToOData() *odata.Query {
return nil
}
func (p peekOptions) ToQuery() *client.QueryParams {
out := &client.QueryParams{}
out.Append("numofmessages", strconv.Itoa(p.numberOfMessages))
out.Append("peekonly", "true")
return out
}