github.com/mailgun/mailgun-go/v3@v3.6.4/unsubscribes.go (about) 1 package mailgun 2 3 import ( 4 "context" 5 "strconv" 6 ) 7 8 type Unsubscribe struct { 9 CreatedAt RFC2822Time `json:"created_at"` 10 Tags []string `json:"tags"` 11 ID string `json:"id"` 12 Address string `json:"address"` 13 } 14 15 type unsubscribesResponse struct { 16 Paging Paging `json:"paging"` 17 Items []Unsubscribe `json:"items"` 18 } 19 20 // Fetches the list of unsubscribes 21 func (mg *MailgunImpl) ListUnsubscribes(opts *ListOptions) *UnsubscribesIterator { 22 r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint)) 23 r.setClient(mg.Client()) 24 r.setBasicAuth(basicAuthUser, mg.APIKey()) 25 if opts != nil { 26 if opts.Limit != 0 { 27 r.addParameter("limit", strconv.Itoa(opts.Limit)) 28 } 29 } 30 url, err := r.generateUrlWithParameters() 31 return &UnsubscribesIterator{ 32 mg: mg, 33 unsubscribesResponse: unsubscribesResponse{Paging: Paging{Next: url, First: url}}, 34 err: err, 35 } 36 } 37 38 type UnsubscribesIterator struct { 39 unsubscribesResponse 40 mg Mailgun 41 err error 42 } 43 44 // If an error occurred during iteration `Err()` will return non nil 45 func (ci *UnsubscribesIterator) Err() error { 46 return ci.err 47 } 48 49 // Next retrieves the next page of items from the api. Returns false when there 50 // no more pages to retrieve or if there was an error. Use `.Err()` to retrieve 51 // the error 52 func (ci *UnsubscribesIterator) Next(ctx context.Context, items *[]Unsubscribe) bool { 53 if ci.err != nil { 54 return false 55 } 56 ci.err = ci.fetch(ctx, ci.Paging.Next) 57 if ci.err != nil { 58 return false 59 } 60 cpy := make([]Unsubscribe, len(ci.Items)) 61 copy(cpy, ci.Items) 62 *items = cpy 63 if len(ci.Items) == 0 { 64 return false 65 } 66 return true 67 } 68 69 // First retrieves the first page of items from the api. Returns false if there 70 // was an error. It also sets the iterator object to the first page. 71 // Use `.Err()` to retrieve the error. 72 func (ci *UnsubscribesIterator) First(ctx context.Context, items *[]Unsubscribe) bool { 73 if ci.err != nil { 74 return false 75 } 76 ci.err = ci.fetch(ctx, ci.Paging.First) 77 if ci.err != nil { 78 return false 79 } 80 cpy := make([]Unsubscribe, len(ci.Items)) 81 copy(cpy, ci.Items) 82 *items = cpy 83 return true 84 } 85 86 // Last retrieves the last page of items from the api. 87 // Calling Last() is invalid unless you first call First() or Next() 88 // Returns false if there was an error. It also sets the iterator object 89 // to the last page. Use `.Err()` to retrieve the error. 90 func (ci *UnsubscribesIterator) Last(ctx context.Context, items *[]Unsubscribe) bool { 91 if ci.err != nil { 92 return false 93 } 94 ci.err = ci.fetch(ctx, ci.Paging.Last) 95 if ci.err != nil { 96 return false 97 } 98 cpy := make([]Unsubscribe, len(ci.Items)) 99 copy(cpy, ci.Items) 100 *items = cpy 101 return true 102 } 103 104 // Previous retrieves the previous page of items from the api. Returns false when there 105 // no more pages to retrieve or if there was an error. Use `.Err()` to retrieve 106 // the error if any 107 func (ci *UnsubscribesIterator) Previous(ctx context.Context, items *[]Unsubscribe) bool { 108 if ci.err != nil { 109 return false 110 } 111 if ci.Paging.Previous == "" { 112 return false 113 } 114 ci.err = ci.fetch(ctx, ci.Paging.Previous) 115 if ci.err != nil { 116 return false 117 } 118 cpy := make([]Unsubscribe, len(ci.Items)) 119 copy(cpy, ci.Items) 120 *items = cpy 121 if len(ci.Items) == 0 { 122 return false 123 } 124 return true 125 } 126 127 func (ci *UnsubscribesIterator) fetch(ctx context.Context, url string) error { 128 r := newHTTPRequest(url) 129 r.setClient(ci.mg.Client()) 130 r.setBasicAuth(basicAuthUser, ci.mg.APIKey()) 131 132 return getResponseFromJSON(ctx, r, &ci.unsubscribesResponse) 133 } 134 135 // Retreives a single unsubscribe record. Can be used to check if a given address is present in the list of unsubscribed users. 136 func (mg *MailgunImpl) GetUnsubscribe(ctx context.Context, address string) (Unsubscribe, error) { 137 r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address)) 138 r.setClient(mg.Client()) 139 r.setBasicAuth(basicAuthUser, mg.APIKey()) 140 141 envelope := Unsubscribe{} 142 err := getResponseFromJSON(ctx, r, &envelope) 143 144 return envelope, err 145 } 146 147 // Unsubscribe adds an e-mail address to the domain's unsubscription table. 148 func (mg *MailgunImpl) CreateUnsubscribe(ctx context.Context, address, tag string) error { 149 r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint)) 150 r.setClient(mg.Client()) 151 r.setBasicAuth(basicAuthUser, mg.APIKey()) 152 p := newUrlEncodedPayload() 153 p.addValue("address", address) 154 p.addValue("tag", tag) 155 _, err := makePostRequest(ctx, r, p) 156 return err 157 } 158 159 // DeleteUnsubscribe removes the e-mail address given from the domain's unsubscription table. 160 // If passing in an ID (discoverable from, e.g., ListUnsubscribes()), the e-mail address associated 161 // with the given ID will be removed. 162 func (mg *MailgunImpl) DeleteUnsubscribe(ctx context.Context, address string) error { 163 r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address)) 164 r.setClient(mg.Client()) 165 r.setBasicAuth(basicAuthUser, mg.APIKey()) 166 _, err := makeDeleteRequest(ctx, r) 167 return err 168 } 169 170 // DeleteUnsubscribeWithTag removes the e-mail address given from the domain's unsubscription table with a matching tag. 171 // If passing in an ID (discoverable from, e.g., ListUnsubscribes()), the e-mail address associated 172 // with the given ID will be removed. 173 func (mg *MailgunImpl) DeleteUnsubscribeWithTag(ctx context.Context, a, t string) error { 174 r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a)) 175 r.setClient(mg.Client()) 176 r.setBasicAuth(basicAuthUser, mg.APIKey()) 177 r.addParameter("tag", t) 178 _, err := makeDeleteRequest(ctx, r) 179 return err 180 }