-
Notifications
You must be signed in to change notification settings - Fork 135
/
queue.go
52 lines (41 loc) · 1.29 KB
/
queue.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
package gotwilio
import (
"context"
"encoding/json"
"net/http"
"net/url"
)
const (
ErrorQueueAlreadyExists ExceptionCode = 22003
)
type QueueResponse struct {
Sid string `json:"sid"`
FriendlyName string `json:"friendly_name"`
MaxSize int `json:"max_size"`
}
func (twilio *Twilio) CreateQueue(friendlyName string) (*QueueResponse, *Exception, error) {
return twilio.CreateQueueWithContext(context.Background(), friendlyName)
}
func (twilio *Twilio) CreateQueueWithContext(ctx context.Context, friendlyName string) (*QueueResponse, *Exception, error) {
var queueResponse *QueueResponse
var exception *Exception
twilioUrl := twilio.buildUrl("Queues.json")
formValues := url.Values{}
formValues.Set("FriendlyName", friendlyName)
res, err := twilio.post(ctx, formValues, twilioUrl)
if err != nil {
return queueResponse, exception, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = decoder.Decode(exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return queueResponse, exception, err
}
queueResponse = new(QueueResponse)
err = decoder.Decode(queueResponse)
return queueResponse, exception, err
}