forked from carlosdp/twiliogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
96 lines (73 loc) · 2.11 KB
/
call.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
package twiliogo
import (
"encoding/json"
"net/url"
)
type Call struct {
Sid string `json:"sid"`
ParentCallSid string `json:"parent_call_sid"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
AccountSid string `json:"account_sid"`
To string `json:"to"`
From string `json:"from"`
PhoneNumberSid string `json:"phone_number_sid"`
Status string `json:"status"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Duration string `json:"duration"`
Price string `json:"price"`
PriceUnit string `json:"price_unit"`
Direction string `json:"direction"`
AnsweredBy string `json:"answered_by"`
ForwardedFrom string `json:"forwarded_from"`
CallerName string `json:"caller_name"`
Uri string `json:"uri"`
}
func NewCall(client Client, from, to string, callback Optional, optionals ...Optional) (*Call, error) {
var call *Call
params := url.Values{}
params.Set("From", from)
params.Set("To", to)
callbackType, callbackParam := callback.GetParam()
params.Set(callbackType, callbackParam)
for _, optional := range optionals {
param, value := optional.GetParam()
params.Set(param, value)
}
res, err := client.post(params, client.RootUrl()+"/Calls.json")
if err != nil {
return nil, err
}
call = new(Call)
err = json.Unmarshal(res, call)
return call, err
}
func GetCall(client Client, sid string) (*Call, error) {
var call *Call
res, err := client.get(url.Values{}, client.RootUrl()+"/Calls/"+sid+".json")
if err != nil {
return nil, err
}
call = new(Call)
err = json.Unmarshal(res, call)
return call, err
}
func (call *Call) Update(client Client, optionals ...Optional) error {
var tempCall *Call
params := url.Values{}
for _, optional := range optionals {
param, value := optional.GetParam()
params.Set(param, value)
}
res, err := client.post(params, client.RootUrl()+"/Calls/"+call.Sid+".json")
if err != nil {
return err
}
tempCall = new(Call)
err = json.Unmarshal(res, tempCall)
if err == nil {
call = tempCall
}
return err
}