forked from plivo/plivo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplivoclient.go
100 lines (85 loc) · 2.86 KB
/
plivoclient.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
package plivo
import (
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"time"
)
const baseUrlString = "https://api.plivo.com/"
const baseRequestString = "/v1/Account/%s/"
type Client struct {
BaseClient
Messages *MessageService
Accounts *AccountService
Subaccounts *SubaccountService
Applications *ApplicationService
Endpoints *EndpointService
Numbers *NumberService
PhoneNumbers *PhoneNumberService
Pricing *PricingService // TODO Rename?
Recordings *RecordingService
Calls *CallService
LiveCalls *LiveCallService
QueuedCalls *QueuedCallService
Conferences *ConferenceService
CallFeedback *CallFeedbackService
Powerpack *PowerpackService
Media *MediaService
}
/*
To set a proxy for all requests, configure the Transport for the HttpClient passed in:
&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL("http//your.proxy.here"),
},
}
Similarly, to configure the timeout, set it on the HttpClient passed in:
&http.Client{
Timeout: time.Minute,
}
*/
func NewClient(authId, authToken string, options *ClientOptions) (client *Client, err error) {
client = &Client{}
if len(authId) == 0 {
authId = os.Getenv("PLIVO_AUTH_ID")
}
if len(authToken) == 0 {
authToken = os.Getenv("PLIVO_AUTH_TOKEN")
}
client.AuthId = authId
client.AuthToken = authToken
client.userAgent = fmt.Sprintf("%s/%s (Go: %s)", "plivo-go", sdkVersion, runtime.Version())
baseUrl, err := url.Parse(baseUrlString) // Todo: handle error case?
client.BaseUrl = baseUrl
client.httpClient = &http.Client{
Timeout: time.Minute,
}
if options.HttpClient != nil {
client.httpClient = options.HttpClient
}
client.Messages = &MessageService{client: client}
client.Accounts = &AccountService{client: client}
client.Subaccounts = &SubaccountService{client: client}
client.Applications = &ApplicationService{client: client}
client.Endpoints = &EndpointService{client: client}
client.Numbers = &NumberService{client: client}
client.PhoneNumbers = &PhoneNumberService{client: client}
client.Pricing = &PricingService{client: client}
client.Recordings = &RecordingService{client: client}
client.Calls = &CallService{client: client}
client.LiveCalls = &LiveCallService{client: client}
client.QueuedCalls = &QueuedCallService{client: client}
client.Conferences = &ConferenceService{client: client}
client.CallFeedback = &CallFeedbackService{client: client}
client.Powerpack = &PowerpackService{client: client}
client.Media = &MediaService{client: client}
return
}
func (client *Client) NewRequest(method string, params interface{}, formatString string,
formatParams ...interface{}) (*http.Request, error) {
formatParams = append([]interface{}{client.AuthId}, formatParams...)
formatString = fmt.Sprintf("%s/%s", "%s", formatString)
return client.BaseClient.NewRequest(method, params, baseRequestString, formatString, formatParams...)
}