forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_session.go
189 lines (147 loc) · 5.21 KB
/
proxy_session.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
// Package gotwilio is a library for interacting with http://www.twilio.com/ API.
package gotwilio
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// https://www.twilio.com/docs/proxy/api/pb-proxy-session
type ProxySessionRequest struct {
Status string // default: open, other values: closed optional
UniqueName string // optional
TTL int // default: 0 (No TTL) optional
DateExpiry time.Time // optional
Mode string // default: voice-and-message, other values: voice-only, message-only optional
}
type ProxySession struct {
Sid string `json:"sid"`
Status string `json:"status"`
UniqueName string `json:"unique_name"`
ClosedReason interface{} `json:"closed_reason"`
DateEnded interface{} `json:"date_ended"`
TTL int `json:"ttl"`
DateExpiry interface{} `json:"date_expiry"`
AccountSid string `json:"account_sid"`
DateUpdated time.Time `json:"date_updated"`
Mode string `json:"mode"`
DateLastInteraction interface{} `json:"date_last_interaction"`
URL string `json:"url"`
DateCreated time.Time `json:"date_created"`
DateStarted time.Time `json:"date_started"`
ServiceSid string `json:"service_sid"`
Links struct {
Participants string `json:"participants"`
Interactions string `json:"interactions"`
} `json:"links"`
// internal attribute
twilio *Twilio
}
// Create a new Twilio Service
func (twilio *Twilio) NewProxySession(serviceID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions")
res, err := twilio.post(proxySessionFormValues(req), twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = json.Unmarshal(responseBody, 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 response, exception, err
}
response = new(ProxySession)
err = json.Unmarshal(responseBody, response)
// Pass connection
response.twilio = twilio
return response, exception, err
}
func (twilio *Twilio) GetProxySession(serviceID, sessionID string) (response *ProxySession, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID)
res, err := twilio.get(twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, 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 response, exception, err
}
response = new(ProxySession)
err = json.Unmarshal(responseBody, response)
// Pass connection
response.twilio = twilio
return response, exception, err
}
func (twilio *Twilio) UpdateProxySession(serviceID, sessionID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID)
res, err := twilio.post(proxySessionFormValues(req), twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, 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 response, exception, err
}
response = new(ProxySession)
err = json.Unmarshal(responseBody, response)
// Pass connection
response.twilio = twilio
return response, exception, err
}
func (twilio *Twilio) DeleteProxySession(serviceID, sessionID string) (exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID)
res, err := twilio.delete(twilioUrl)
if err != nil {
return exception, err
}
respBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusNoContent {
exc := new(Exception)
err = json.Unmarshal(respBody, exc)
return exc, err
}
return nil, nil
}
// Form values initialization
func proxySessionFormValues(req ProxySessionRequest) url.Values {
formValues := url.Values{}
formValues.Set("UniqueName", req.UniqueName)
if req.Status != "" {
formValues.Set("Status", req.Status)
}
formValues.Set("Ttl", fmt.Sprintf("%d", req.TTL))
if !req.DateExpiry.IsZero() {
formValues.Set("DateExpiry", req.DateExpiry.Format("20060101"))
}
if req.Mode != "" {
formValues.Set("Mode", req.Mode)
}
return formValues
}