|
| 1 | +package number |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + messagebird "github.com/messagebird/go-rest-api" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + // apiRoot is the absolute URL of the Numbers API. |
| 14 | + apiRoot = "https://numbers.messagebird.com/v1" |
| 15 | + |
| 16 | + // pathNumbers is the path for the Numbers resource, relative to apiRoot. |
| 17 | + // and path. |
| 18 | + pathNumbers = "phone-numbers" |
| 19 | + |
| 20 | + // pathNumbersAvailable is the path for the Search Number resource, relative to apiRoot. |
| 21 | + pathNumbersAvailable = "available-phone-numbers" |
| 22 | +) |
| 23 | + |
| 24 | +// Number represents a specific phone number. |
| 25 | +type Number struct { |
| 26 | + Number string |
| 27 | + Country string |
| 28 | + Region string |
| 29 | + Locality string |
| 30 | + Features []string |
| 31 | + Tags []string |
| 32 | + Type string |
| 33 | + Status string |
| 34 | +} |
| 35 | + |
| 36 | +// NumberList provide a list of all purchased phone numbers. |
| 37 | +type NumberList struct { |
| 38 | + Offset int |
| 39 | + Limit int |
| 40 | + Count int |
| 41 | + TotalCount int |
| 42 | + Items []*Number |
| 43 | +} |
| 44 | + |
| 45 | +// NumberSearchingList provide a list of all phone numbers. |
| 46 | +// that are available for purchase. |
| 47 | +type NumberSearchingList struct { |
| 48 | + Items []*Number |
| 49 | + Limit int |
| 50 | + Count int |
| 51 | +} |
| 52 | + |
| 53 | +// NumberListParams can be used to set query params in List(). |
| 54 | +type NumberListParams struct { |
| 55 | + Limit int |
| 56 | + Offset int |
| 57 | + Number string |
| 58 | + Country string |
| 59 | + Region string |
| 60 | + Locality string |
| 61 | + Features []string |
| 62 | + Type string |
| 63 | + Status string |
| 64 | + SearchPattern NumberPattern |
| 65 | +} |
| 66 | + |
| 67 | +// NumberUpdateRequest can be used to set tags update. |
| 68 | +type NumberUpdateRequest struct { |
| 69 | + Tags []string `json:"tags"` |
| 70 | +} |
| 71 | + |
| 72 | +// NumberPurchaseRequest can be used to purchase a number. |
| 73 | +type NumberPurchaseRequest struct { |
| 74 | + Number string `json:"number"` |
| 75 | + Country string `json:"countryCode"` |
| 76 | + BillingIntervalMonths int `json:"billingIntervalMonths"` |
| 77 | +} |
| 78 | + |
| 79 | +type NumberPattern string |
| 80 | + |
| 81 | +const ( |
| 82 | + // NumberPatternStart force phone numbers to start with the provided fragment. |
| 83 | + NumberPatternStart NumberPattern = "start" |
| 84 | + |
| 85 | + // NumberPatternEnd phone numbers can be somewhere within the provided fragment. |
| 86 | + NumberPatternEnd NumberPattern = "end" |
| 87 | + |
| 88 | + // NumberPatternAnyWhere force phone numbers to end with the provided fragment. |
| 89 | + NumberPatternAnyWhere NumberPattern = "anywhere" |
| 90 | +) |
| 91 | + |
| 92 | +// request does the exact same thing as Client.Request. It does, however, |
| 93 | +// prefix the path with the Numbers API's root. This ensures the client |
| 94 | +// doesn't "handle" this for us: by default, it uses the REST API. |
| 95 | +func request(c *messagebird.Client, v interface{}, method, path string, data interface{}) error { |
| 96 | + return c.Request(v, method, fmt.Sprintf("%s/%s", apiRoot, path), data) |
| 97 | +} |
| 98 | + |
| 99 | +// List get all purchased phone numbers |
| 100 | +func List(c *messagebird.Client, listParams *NumberListParams) (*NumberList, error) { |
| 101 | + uri := getpath(listParams, pathNumbers) |
| 102 | + |
| 103 | + numberList := &NumberList{} |
| 104 | + if err := request(c, numberList, http.MethodGet, uri, nil); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + return numberList, nil |
| 108 | +} |
| 109 | + |
| 110 | +// Search for phone numbers available for purchase, countryCode needs to be in Alpha-2 country code (example: NL) |
| 111 | +func Search(c *messagebird.Client, countryCode string, listParams *NumberListParams) (*NumberSearchingList, error) { |
| 112 | + uri := getpath(listParams, pathNumbersAvailable+"/"+countryCode) |
| 113 | + |
| 114 | + numberList := &NumberSearchingList{} |
| 115 | + if err := request(c, numberList, http.MethodGet, uri, nil); err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + return numberList, nil |
| 120 | +} |
| 121 | + |
| 122 | +// Read get a purchased phone number |
| 123 | +func Read(c *messagebird.Client, phoneNumber string) (*Number, error) { |
| 124 | + if len(phoneNumber) < 5 { |
| 125 | + return nil, fmt.Errorf("a phoneNumber is too short") |
| 126 | + } |
| 127 | + |
| 128 | + uri := fmt.Sprintf("%s/%s", pathNumbers, phoneNumber) |
| 129 | + |
| 130 | + number := &Number{} |
| 131 | + if err := request(c, number, http.MethodGet, uri, nil); err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + return number, nil |
| 136 | +} |
| 137 | + |
| 138 | +// Delete a purchased phone number |
| 139 | +func Delete(c *messagebird.Client, phoneNumber string) error { |
| 140 | + uri := fmt.Sprintf("%s/%s", pathNumbers, phoneNumber) |
| 141 | + return request(c, nil, http.MethodDelete, uri, nil) |
| 142 | +} |
| 143 | + |
| 144 | +// Update updates a purchased phone number. |
| 145 | +// Only updating *tags* is supported at the moment. |
| 146 | +func Update(c *messagebird.Client, phoneNumber string, numberUpdateRequest *NumberUpdateRequest) (*Number, error) { |
| 147 | + uri := fmt.Sprintf("%s/%s", pathNumbers, phoneNumber) |
| 148 | + |
| 149 | + number := &Number{} |
| 150 | + if err := request(c, number, http.MethodPatch, uri, numberUpdateRequest); err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + return number, nil |
| 155 | +} |
| 156 | + |
| 157 | +// Purchases purchases a phone number. |
| 158 | +func Purchase(c *messagebird.Client, numberPurchaseRequest *NumberPurchaseRequest) (*Number, error) { |
| 159 | + |
| 160 | + number := &Number{} |
| 161 | + if err := request(c, number, http.MethodPost, pathNumbers, numberPurchaseRequest); err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + |
| 165 | + return number, nil |
| 166 | +} |
| 167 | + |
| 168 | +// GetPath get the full path for the request |
| 169 | +func getpath(listParams *NumberListParams, path string) string { |
| 170 | + params := paramsForMessageList(listParams) |
| 171 | + return fmt.Sprintf("%s?%s", path, params.Encode()) |
| 172 | +} |
| 173 | + |
| 174 | +// paramsForMessageList build query params |
| 175 | +func paramsForMessageList(params *NumberListParams) *url.Values { |
| 176 | + urlParams := &url.Values{} |
| 177 | + |
| 178 | + if params == nil { |
| 179 | + return urlParams |
| 180 | + } |
| 181 | + |
| 182 | + if len(params.Features) > 0 { |
| 183 | + paramsForArrays("features", params.Features, urlParams) |
| 184 | + } |
| 185 | + |
| 186 | + if params.Type != "" { |
| 187 | + urlParams.Set("type", params.Type) |
| 188 | + } |
| 189 | + |
| 190 | + if params.Number != "" { |
| 191 | + urlParams.Set("number", params.Number) |
| 192 | + } |
| 193 | + if params.Country != "" { |
| 194 | + urlParams.Set("country", params.Country) |
| 195 | + } |
| 196 | + if params.Limit != 0 { |
| 197 | + urlParams.Set("limit", strconv.Itoa(params.Limit)) |
| 198 | + } |
| 199 | + |
| 200 | + if params.SearchPattern != "" { |
| 201 | + urlParams.Set("search_pattern", string(params.SearchPattern)) |
| 202 | + } |
| 203 | + |
| 204 | + if params.Offset != 0 { |
| 205 | + urlParams.Set("offset", strconv.Itoa(params.Offset)) |
| 206 | + } |
| 207 | + |
| 208 | + return urlParams |
| 209 | +} |
| 210 | + |
| 211 | +// paramsForArrays build query for array params |
| 212 | +func paramsForArrays(field string, values []string, urlParams *url.Values) { |
| 213 | + for _, value := range values { |
| 214 | + urlParams.Add(field, value) |
| 215 | + } |
| 216 | +} |
0 commit comments