|
1 | 1 | package client |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
6 | 4 | "fmt" |
7 | | - "io" |
8 | | - "io/ioutil" |
9 | 5 | "net" |
10 | 6 | "net/http" |
11 | 7 | "net/url" |
12 | | - "path" |
13 | | - "strconv" |
14 | | - "strings" |
15 | 8 | "time" |
16 | 9 | ) |
17 | 10 |
|
@@ -189,56 +182,7 @@ func (c *LmstfyClient) RespawnDeadLetter(queue string, limit, ttlSecond int64) ( |
189 | 182 | return c.respawnDeadLetter(nil, queue, limit, ttlSecond) |
190 | 183 | } |
191 | 184 |
|
| 185 | +// DeleteDeadLetter deletes the given queue's dead letter |
192 | 186 | func (c *LmstfyClient) DeleteDeadLetter(queue string, limit int64) *APIError { |
193 | | - if limit <= 0 { |
194 | | - return &APIError{ |
195 | | - Type: RequestErr, |
196 | | - Reason: "limit should be > 0", |
197 | | - } |
198 | | - } |
199 | | - query := url.Values{} |
200 | | - query.Add("limit", strconv.FormatInt(limit, 10)) |
201 | | - req, err := c.getReq(http.MethodDelete, path.Join(queue, "deadletter"), query, nil) |
202 | | - if err != nil { |
203 | | - return &APIError{ |
204 | | - Type: RequestErr, |
205 | | - Reason: err.Error(), |
206 | | - } |
207 | | - } |
208 | | - resp, err := c.httpCli.Do(req) |
209 | | - if err != nil { |
210 | | - return &APIError{ |
211 | | - Type: RequestErr, |
212 | | - Reason: err.Error(), |
213 | | - } |
214 | | - } |
215 | | - defer resp.Body.Close() |
216 | | - if resp.StatusCode != http.StatusNoContent { |
217 | | - return &APIError{ |
218 | | - Type: ResponseErr, |
219 | | - Reason: parseResponseError(resp), |
220 | | - RequestID: resp.Header.Get("X-Request-ID"), |
221 | | - } |
222 | | - } |
223 | | - return nil |
224 | | -} |
225 | | - |
226 | | -func discardResponseBody(resp io.ReadCloser) { |
227 | | - // discard response body, to make this connection reusable in the http connection pool |
228 | | - ioutil.ReadAll(resp) |
229 | | -} |
230 | | - |
231 | | -func parseResponseError(resp *http.Response) string { |
232 | | - respBytes, err := ioutil.ReadAll(resp.Body) |
233 | | - if err != nil { |
234 | | - return fmt.Sprintf("Invalid response: %s", err) |
235 | | - } |
236 | | - var errData struct { |
237 | | - Error string `json:"error"` |
238 | | - } |
239 | | - err = json.Unmarshal(respBytes, &errData) |
240 | | - if err != nil { |
241 | | - return fmt.Sprintf("Invalid JSON: %s", err) |
242 | | - } |
243 | | - return fmt.Sprintf("[%d]%s", resp.StatusCode, errData.Error) |
| 187 | + return c.deleteDeadLetter(nil, queue, limit) |
244 | 188 | } |
0 commit comments