-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.go
59 lines (46 loc) · 1.13 KB
/
delete.go
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
package kutt
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type DeleteParams struct {
ID string `json:"id"`
Domain *string `json:"domain"`
}
type DeleteOption func(*DeleteParams)
func WithDomain(v string) DeleteOption {
return func(p *DeleteParams) {
p.Domain = &v
}
}
func (cli *Client) Delete(ID string, opts ...DeleteOption) error {
path := "/api/url/deleteurl"
reqURL := cli.BaseURL + path
payload := &DeleteParams{
ID: ID,
}
for _, opt := range opts {
opt(payload)
}
jsonBytes, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal json: %w", err)
}
body := strings.NewReader(string(jsonBytes))
req, err := http.NewRequest(http.MethodPost, reqURL, body)
if err != nil {
return fmt.Errorf("create HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := cli.do(req)
if err != nil {
return fmt.Errorf("do HTTP request: %w", err)
}
defer resp.Body.Close()
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
return fmt.Errorf("HTTP response: %w", cli.error(resp.StatusCode, resp.Body))
}
return nil
}