-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathip_addresses.go
More file actions
141 lines (118 loc) · 3.86 KB
/
ip_addresses.go
File metadata and controls
141 lines (118 loc) · 3.86 KB
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
package sendgrid
import (
"context"
"fmt"
"net/url"
)
type IPAddress struct {
IP string `json:"ip,omitempty"`
Pools []string `json:"pools,omitempty"`
Warmup bool `json:"warmup,omitempty"`
StartDate int64 `json:"start_date,omitempty"`
Subusers []string `json:"subusers,omitempty"`
Rdns string `json:"rdns,omitempty"`
AssignedAt int64 `json:"assigned_at,omitempty"`
Whitelabeled bool `json:"whitelabeled,omitempty"`
}
type InputGetIPAddresses struct {
IP string `url:"ip,omitempty"`
ExcludeWhitelabels bool `url:"exclude_whitelabels,omitempty"`
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Subuser string `url:"subuser,omitempty"`
SortByDirection string `url:"sort_by_direction,omitempty"`
}
// see: https://www.twilio.com/docs/sendgrid/api-reference/ip-address/retrieve-all-ip-addresses
func (c *Client) GetIPAddresses(ctx context.Context, input *InputGetIPAddresses) ([]*IPAddress, error) {
u, _ := url.Parse("/ips")
q := u.Query()
if input.IP != "" {
q.Set("ip", input.IP)
}
if input.ExcludeWhitelabels {
q.Set("exclude_whitelabels", fmt.Sprintf("%t", input.ExcludeWhitelabels))
}
if input.Limit != 0 {
q.Set("limit", fmt.Sprintf("%d", input.Limit))
}
if input.Offset != 0 {
q.Set("offset", fmt.Sprintf("%d", input.Offset))
}
if input.Subuser != "" {
q.Set("subuser", input.Subuser)
}
if input.SortByDirection != "" {
q.Set("sort_by_direction", input.SortByDirection)
}
u.RawQuery = q.Encode()
req, err := c.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
var ips []*IPAddress
if err := c.Do(ctx, req, &ips); err != nil {
return nil, err
}
return ips, nil
}
type AssignedIPAddress struct {
IP string `json:"ip,omitempty"`
Pools []string `json:"pools,omitempty"`
Warmup bool `json:"warmup,omitempty"`
StartDate int64 `json:"start_date,omitempty"`
}
// see: https://www.twilio.com/docs/sendgrid/api-reference/ip-address/retrieve-all-assigned-ips
func (c *Client) GetAssignedIPAddresses(ctx context.Context) ([]*AssignedIPAddress, error) {
path := "/ips/assigned"
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
var ips []*AssignedIPAddress
if err := c.Do(ctx, req, &ips); err != nil {
return nil, err
}
return ips, nil
}
type RemainingIPCount struct {
Remaining int `json:"remaining,omitempty"`
Period string `json:"period,omitempty"`
PricePerIP int `json:"price_per_ip,omitempty"`
}
type OutputGetRemainingIPCount struct {
Results []*RemainingIPCount `json:"results,omitempty"`
}
// see: https://www.twilio.com/docs/sendgrid/api-reference/ip-address/get-remaining-ips-count
func (c *Client) GetRemainingIPCount(ctx context.Context) (*OutputGetRemainingIPCount, error) {
req, err := c.NewRequest("GET", "/ips/remaining", nil)
if err != nil {
return nil, err
}
r := new(OutputGetRemainingIPCount)
if err := c.Do(ctx, req, r); err != nil {
return nil, err
}
return r, nil
}
type OutputGetIPAddress struct {
IP string `json:"ip,omitempty"`
Subusers []string `json:"subusers,omitempty"`
Rdns string `json:"rdns,omitempty"`
Pools []string `json:"pools,omitempty"`
Warmup bool `json:"warmup,omitempty"`
StartDate int64 `json:"start_date,omitempty"`
Whitelabeled bool `json:"whitelabeled,omitempty"`
}
// see: https://www.twilio.com/docs/sendgrid/api-reference/ip-address/retrieve-all-ip-pools-an-ip-address-belongs-to
func (c *Client) GetIPAddress(ctx context.Context, ip string) (*OutputGetIPAddress, error) {
path := fmt.Sprintf("/ips/%s", url.QueryEscape(ip))
req, err := c.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
r := new(OutputGetIPAddress)
if err := c.Do(ctx, req, r); err != nil {
return nil, err
}
return r, nil
}