forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpricing.go
100 lines (80 loc) · 2.66 KB
/
pricing.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 gotwilio
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type PricingResponse struct {
Country string `json:"country"`
ISOCountry string `json:"iso_country"`
PriceUnit string `json:"price_unit"`
OutboundSMSPrices []OutboundSMSPrice `json:"outbound_sms_prices"`
InboundSMSPrice []Price `json:"inbound_sms_prices"`
Url string `json:"uri"`
}
type OutboundSMSPrice struct {
MCC string `json:"mcc"`
MNC string `json:"mnc"`
Carrier string `json:"carrier"`
Prices []Price `json:"prices"`
}
type Price struct {
NumberType string `json:"number_type"`
BasePrice float64 `json:"base_price,string"`
CurrentPrice float64 `json:"current_price,string"`
}
type PricingList struct {
Meta Meta `json:"meta"`
Countries []Country `json:"countries"`
}
type Country struct {
Country string `json:"country"`
IsoCountry string `json:"iso_country"`
Url string `json:"url"`
}
func (twilio *Twilio) GetPricing(countryISO string) (pricingResponse *PricingResponse, exception *Exception, err error) {
pricingUrl := twilio.PricingUrl + "/Messaging/Countries/" + strings.ToUpper(countryISO)
res, err := twilio.get(pricingUrl)
if err != nil {
return pricingResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return pricingResponse, 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 pricingResponse, exception, err
}
pricingResponse = new(PricingResponse)
err = json.Unmarshal(responseBody, pricingResponse)
return pricingResponse, exception, err
}
func (twilio *Twilio) GetCountriesPricing() (pricingList *PricingList, exception *Exception, err error) {
pricingUrl := twilio.PricingUrl + "/Messaging/Countries"
pricingUrl = setPageSize(pricingUrl, 250)
res, err := twilio.get(pricingUrl)
if err != nil {
return pricingList, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return pricingList, 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 pricingList, exception, err
}
pricingList = new(PricingList)
err = json.Unmarshal(responseBody, pricingList)
return pricingList, exception, err
}