Browse files

Modify Push to return message ID

This matches the README example.

Also, use the new array-based POST format. (Forgot about this one before.)
  • Loading branch information...
1 parent d0c69ed commit df1bd004cda00cd377a876c4678cd0592fd20ddc @edsrzf edsrzf committed Feb 7, 2012
Showing with 21 additions and 6 deletions.
  1. +17 −5 ironmq.go
  2. +4 −1 ironmq_test.go
View
22 ironmq.go
@@ -124,18 +124,30 @@ func (q *Queue) Get() (*Message, os.Error) {
// Push adds a message to the end of the queue using IronMQ's defaults:
// timeout - 60 seconds
// delay - none
-func (q *Queue) Push(msg string) os.Error {
+func (q *Queue) Push(msg string) (id string, err os.Error) {
return q.PushMsg(&Message{Body: msg})
}
// PushMsg adds a message to the end of the queue using the fields of msg as
// parameters. msg.Id is ignored.
-func (q *Queue) PushMsg(msg *Message) os.Error {
- data, err := json.Marshal(msg)
+func (q *Queue) PushMsg(msg *Message) (id string, err os.Error) {
+ msgs := struct {
+ Messages []*Message `json:"messages"`
+ }{
+ []*Message{msg},
+ }
+ data, err := json.Marshal(msgs)
if err != nil {
- return err
+ return "", err
+ }
+ var resp struct {
+ IDs []string `json:"ids"`
+ }
+ err = q.Client.req("POST", "queues/"+q.name+"/messages", data, &resp)
+ if err != nil {
+ return "", err
}
- return q.Client.req("POST", "queues/"+q.name+"/messages", data, nil)
+ return resp.IDs[0], nil
}
type Message struct {
View
5 ironmq_test.go
@@ -28,10 +28,13 @@ func TestFunctionality(t *testing.T) {
}
const body = "Hello, IronMQ!"
- err = queue.Push(body)
+ id, err := queue.Push(body)
if err != nil {
t.Fatalf("queue.Push: error isn't nil: %s", err)
}
+ if len(id) == 0 {
+ t.Fatal("queue.Push: no ID returned")
+ }
qi, err := queue.Info()
if err != nil {

0 comments on commit df1bd00

Please sign in to comment.