-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsqs.go
276 lines (227 loc) · 6.17 KB
/
sqs.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// goamz - Go packages to interact with the Amazon Web Services.
//
// https://wiki.ubuntu.com/goamz
//
// Copyright (c) 2011 Memeo Inc.
//
// Written by Prudhvi Krishna Surapaneni <[email protected]>
//
package sqs
import (
"http"
"xml"
"url"
"os"
"time"
"strconv"
"launchpad.net/goamz/aws"
)
// The SQS type encapsulates operation with an SQS region.
type SQS struct {
aws.Auth
aws.Region
private byte // Reserve the right of using private data.
}
func New(auth aws.Auth, region aws.Region) *SQS {
return &SQS{auth, region, 0}
}
type Queue struct {
*SQS
Url string
}
type CreateQueueResponse struct {
QueueUrl string `xml:"CreateQueueResult>QueueUrl"`
ResponseMetadata
}
type ListQueuesResponse struct {
QueueUrl []string `xml:"ListQueuesResult>QueueUrl"`
ResponseMetadata
}
type DeleteMessageResponse struct {
ResponseMetadata
}
type DeleteQueueResponse struct {
ResponseMetadata
}
type SendMessageResponse struct {
MD5 string `xml:"SendMessageResult>MD5OfMessageBody"`
Id string `xml:"SendMessageResult>MessageId"`
ResponseMetadata
}
type ReceiveMessageResponse struct {
Messages []Message `xml:"ReceiveMessageResult>Message"`
ResponseMetadata
}
type Message struct {
MessageId string `xml:"MessageId"`
Body string `xml:"Body"`
MD5OfBody string `xml:"MD5OfBody"`
ReceiptHandle string `xml:"ReceiptHandle"`
Attribute []Attribute `xml:"Attribute"`
}
type Attribute struct {
Name string `xml:"ReceiveMessageResult>Message>Attribute>Name"`
Value string `xml:"ReceiveMessageResult>Message>Attribute>Value"`
}
type ChangeMessageVisibilityResponse struct {
ResponseMetadata
}
type GetQueueAttributesResponse struct {
Attributes []Attribute `xml:"GetQueueAttributesResult>Attribute"`
ResponseMetadata
}
type ResponseMetadata struct {
RequestId string
BoxUsage float64
}
type Error struct {
StatusCode int
Code string
Message string
RequestId string
}
func (err *Error) String() string {
return err.Message
}
type xmlErrors struct {
RequestId string
Errors []Error `xml:"Errors>Error"`
}
func (s *SQS) CreateQueue(queueName string) (*Queue, os.Error) {
return s.CreateQueueWithTimeout(queueName, 30)
}
func (s *SQS) CreateQueueWithTimeout(queueName string, timeout int) (q *Queue, err os.Error) {
resp, err := s.newQueue(queueName, timeout)
if err != nil {
return nil, err
}
q = &Queue{s, resp.QueueUrl}
return
}
func (s *SQS) QueueFromArn(queueUrl string) (q *Queue) {
q = &Queue{s, queueUrl}
return
}
func (s *SQS) newQueue(queueName string, timeout int) (resp *CreateQueueResponse, err os.Error) {
resp = &CreateQueueResponse{}
params := makeParams("CreateQueue")
params["QueueName"] = queueName
params["DefaultVisibilityTimeout"] = strconv.Itoa(timeout)
err = s.query("", params, resp)
return
}
func (s *SQS) ListQueues(QueueNamePrefix string) (resp *ListQueuesResponse, err os.Error) {
resp = &ListQueuesResponse{}
params := makeParams("ListQueues")
if QueueNamePrefix != "" {
params["QueueNamePrefix"] = QueueNamePrefix
}
err = s.query("", params, resp)
return
}
func (q *Queue) Delete() (resp *DeleteQueueResponse, err os.Error) {
resp = &DeleteQueueResponse{}
params := makeParams("DeleteQueue")
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) SendMessage(MessageBody string) (resp *SendMessageResponse, err os.Error) {
resp = &SendMessageResponse{}
params := makeParams("SendMessage")
params["MessageBody"] = MessageBody
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) ReceiveMessage(MaxNumberOfMessages, VisibilityTimeout int) (resp *ReceiveMessageResponse, err os.Error) {
resp = &ReceiveMessageResponse{}
params := makeParams("ReceiveMessage")
params["AttributeName"] = "All"
params["MaxNumberOfMessages"] = strconv.Itoa(MaxNumberOfMessages)
params["VisibilityTimeout"] = strconv.Itoa(VisibilityTimeout)
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) ChangeMessageVisibility(M *Message, VisibilityTimeout int) (resp *ChangeMessageVisibilityResponse, err os.Error) {
resp = &ChangeMessageVisibilityResponse{}
params := makeParams("ChangeMessageVisibility")
params["VisibilityTimeout"] = strconv.Itoa(VisibilityTimeout)
params["ReceiptHandle"] = M.ReceiptHandle
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) GetQueueAttributes(A string) (resp *GetQueueAttributesResponse, err os.Error) {
resp = &GetQueueAttributesResponse{}
params := makeParams("GetQueueAttributes")
params["AttributeName"] = A
err = q.SQS.query(q.Url, params, resp)
return
}
func (q *Queue) DeleteMessage(M *Message) (resp *DeleteMessageResponse, err os.Error) {
resp = &DeleteMessageResponse{}
params := makeParams("DeleteMessage")
params["ReceiptHandle"] = M.ReceiptHandle
err = q.SQS.query(q.Url, params, resp)
return
}
func (s *SQS) query(queueUrl string, params map[string]string, resp interface{}) os.Error {
params["Timestamp"] = time.UTC().Format(time.RFC3339)
var url_ *url.URL
var err os.Error
var path string
if queueUrl != "" {
url_, err = url.Parse(queueUrl)
path = queueUrl[len(s.Region.SQSEndpoint):]
} else {
url_, err = url.Parse(s.Region.SQSEndpoint)
path = "/"
}
if err != nil {
return err
}
//url_, err := url.Parse(s.Region.SQSEndpoint)
//if err != nil {
// return err
//}
sign(s.Auth, "GET", path, params, url_.Host)
url_.RawQuery = multimap(params).Encode()
r, err := http.Get(url_.String())
if err != nil {
return err
}
defer r.Body.Close()
//dump, _ := http.DumpResponse(r, true)
//println("DUMP:\n", string(dump))
//return nil
if r.StatusCode != 200 {
return buildError(r)
}
err = xml.Unmarshal(r.Body, resp)
return err
}
func buildError(r *http.Response) os.Error {
errors := xmlErrors{}
xml.Unmarshal(r.Body, &errors)
var err Error
if len(errors.Errors) > 0 {
err = errors.Errors[0]
}
err.RequestId = errors.RequestId
err.StatusCode = r.StatusCode
if err.Message == "" {
err.Message = r.Status
}
return &err
}
func makeParams(action string) map[string]string {
params := make(map[string]string)
params["Action"] = action
return params
}
func multimap(p map[string]string) url.Values {
q := make(url.Values, len(p))
for k, v := range p {
q[k] = []string{v}
}
return q
}