github.com/mailgun/mailgun-go/v3@v3.6.4/ips.go (about) 1 package mailgun 2 3 import "context" 4 5 type ipAddressListResponse struct { 6 TotalCount int `json:"total_count"` 7 Items []string `json:"items"` 8 } 9 10 type IPAddress struct { 11 IP string `json:"ip"` 12 RDNS string `json:"rdns"` 13 Dedicated bool `json:"dedicated"` 14 } 15 16 type okResp struct { 17 ID string `json:"id,omitempty"` 18 Message string `json:"message"` 19 } 20 21 // ListIPS returns a list of IPs assigned to your account 22 func (mg *MailgunImpl) ListIPS(ctx context.Context, dedicated bool) ([]IPAddress, error) { 23 r := newHTTPRequest(generatePublicApiUrl(mg, ipsEndpoint)) 24 r.setClient(mg.Client()) 25 if dedicated { 26 r.addParameter("dedicated", "true") 27 } 28 r.setBasicAuth(basicAuthUser, mg.APIKey()) 29 30 var resp ipAddressListResponse 31 if err := getResponseFromJSON(ctx, r, &resp); err != nil { 32 return nil, err 33 } 34 var result []IPAddress 35 for _, ip := range resp.Items { 36 result = append(result, IPAddress{IP: ip}) 37 } 38 return result, nil 39 } 40 41 // GetIP returns information about the specified IP 42 func (mg *MailgunImpl) GetIP(ctx context.Context, ip string) (IPAddress, error) { 43 r := newHTTPRequest(generatePublicApiUrl(mg, ipsEndpoint) + "/" + ip) 44 r.setClient(mg.Client()) 45 r.setBasicAuth(basicAuthUser, mg.APIKey()) 46 var resp IPAddress 47 err := getResponseFromJSON(ctx, r, &resp) 48 return resp, err 49 } 50 51 // ListDomainIPS returns a list of IPs currently assigned to the specified domain. 52 func (mg *MailgunImpl) ListDomainIPS(ctx context.Context) ([]IPAddress, error) { 53 r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + mg.domain + "/ips") 54 r.setClient(mg.Client()) 55 r.setBasicAuth(basicAuthUser, mg.APIKey()) 56 57 var resp ipAddressListResponse 58 if err := getResponseFromJSON(ctx, r, &resp); err != nil { 59 return nil, err 60 } 61 var result []IPAddress 62 for _, ip := range resp.Items { 63 result = append(result, IPAddress{IP: ip}) 64 } 65 return result, nil 66 } 67 68 // Assign a dedicated IP to the domain specified. 69 func (mg *MailgunImpl) AddDomainIP(ctx context.Context, ip string) error { 70 r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + mg.domain + "/ips") 71 r.setClient(mg.Client()) 72 r.setBasicAuth(basicAuthUser, mg.APIKey()) 73 74 payload := newUrlEncodedPayload() 75 payload.addValue("ip", ip) 76 _, err := makePostRequest(ctx, r, payload) 77 return err 78 } 79 80 // Unassign an IP from the domain specified. 81 func (mg *MailgunImpl) DeleteDomainIP(ctx context.Context, ip string) error { 82 r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + mg.domain + "/ips/" + ip) 83 r.setClient(mg.Client()) 84 r.setBasicAuth(basicAuthUser, mg.APIKey()) 85 _, err := makeDeleteRequest(ctx, r) 86 return err 87 }