-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks_endpoints.go
159 lines (143 loc) · 4.79 KB
/
links_endpoints.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package rebrandly
import (
"context"
"errors"
"fmt"
"net/http"
)
// LinksGet returns the list of links
func (c *Controller) LinksGet(filters *LinksFilters) (links Links, err error) {
return c.LinksGetCtx(context.Background(), filters)
}
// LinksGetCtx returns the list of links
func (c *Controller) LinksGetCtx(ctx context.Context, filters *LinksFilters) (links Links, err error) {
query, err := convertStructToURLQuery(filters)
if err != nil {
err = fmt.Errorf("can't convert filters to query params: %v", err)
return
}
url := *templateURL
url.Path += "/links"
url.RawQuery = query.Encode()
err = c.request(ctx, "GET", url, nil, &links, []int{http.StatusNotFound})
return
}
// LinksGetAll returns the list of links
func (c *Controller) LinksGetAll(filters *LinksFilters) (links Links, err error) {
return c.LinksGetAllCtx(context.Background(), filters)
}
// LinksGetAllCtx recursively retreive all the links (not limited by max)
func (c *Controller) LinksGetAllCtx(ctx context.Context, filters *LinksFilters) (links Links, err error) {
query, err := convertStructToURLQuery(filters)
if err != nil {
err = fmt.Errorf("can't convert filters to query params: %v", err)
return
}
url := *templateURL
url.Path += "/links"
// Loop thru all pages
var page int
for {
page++
if page != 1 {
query.Set("last", links[len(links)-1].ID)
}
url.RawQuery = query.Encode()
var pageLinks Links
if err = c.request(ctx, "GET", url, nil, &pageLinks, []int{http.StatusNotFound}); err != nil {
err = fmt.Errorf("error at page %d: %w", page, err)
return
}
links = append(links, pageLinks...)
if len(pageLinks) == 0 {
break
}
}
return
}
// LinksGetByID returns the link details of link id.
func (c *Controller) LinksGetByID(id string) (link Link, err error) {
return c.LinksGetByIDCtx(context.Background(), id)
}
// LinksGetByIDCtx returns the link details of link id.
func (c *Controller) LinksGetByIDCtx(ctx context.Context, id string) (link Link, err error) {
url := *templateURL
url.Path += fmt.Sprintf("/links/%s", id)
err = c.request(ctx, "GET", url, nil, &link, []int{http.StatusNotFound})
return
}
// LinksCount returns the number of links
func (c *Controller) LinksCount(filters *LinksCountFilters) (nbLinks int, err error) {
return c.LinksCountCtx(context.Background(), filters)
}
// LinksCountCtx returns the number of links
func (c *Controller) LinksCountCtx(ctx context.Context, filters *LinksCountFilters) (nbLinks int, err error) {
query, err := convertStructToURLQuery(filters)
if err != nil {
err = fmt.Errorf("can't convert filters to query params: %v", err)
return
}
var resp domainCountResponse
url := *templateURL
url.Path += "/links/count"
url.RawQuery = query.Encode()
if err = c.request(ctx, "GET", url, nil, &resp, nil); err != nil {
return
}
nbLinks = resp.Count
return
}
// Links creation with GET won't supported as this is pure evil to create a ressource with GET
// LinksCreate creates a link
func (c *Controller) LinksCreate(payload LinkCreationPayload) (link Link, err error) {
return c.LinksCreateCtx(context.Background(), payload)
}
// LinksCreateCtx creates a link
func (c *Controller) LinksCreateCtx(ctx context.Context, payload LinkCreationPayload) (link Link, err error) {
if payload.Destination == "" {
err = errors.New("destination can't be empty")
return
}
url := *templateURL
url.Path += "/links"
err = c.request(ctx, "POST", url, payload, &link, []int{http.StatusForbidden, http.StatusNotFound})
return
}
// LinksUpdate allows to update a link title and destination by its id.
func (c *Controller) LinksUpdate(id string, payload LinkUpdatePayload) (link Link, err error) {
return c.LinksUpdateCtx(context.Background(), id, payload)
}
// LinksUpdateCtx allows to update a link title and destination by its id.
func (c *Controller) LinksUpdateCtx(ctx context.Context, id string, payload LinkUpdatePayload) (link Link, err error) {
if id == "" {
err = errors.New("id can't be empty")
return
}
if payload.Title == "" {
err = errors.New("title can't be empty")
return
}
if payload.Destination == "" {
err = errors.New("destination can't be empty")
return
}
url := *templateURL
url.Path += fmt.Sprintf("/links/%s", id)
err = c.request(ctx, "POST", url, payload, &link, []int{http.StatusForbidden, http.StatusNotFound})
return
}
// LinksDelete deletes a link identified by id.
func (c *Controller) LinksDelete(id string) (link Link, err error) {
return c.LinksDeleteCtx(context.Background(), id)
}
// LinksDeleteCtx deletes a link identified by id.
func (c *Controller) LinksDeleteCtx(ctx context.Context, id string) (link Link, err error) {
if id == "" {
err = errors.New("id can't be empty")
return
}
url := *templateURL
url.Path += fmt.Sprintf("/links/%s", id)
err = c.request(ctx, "DELETE", url, nil, &link, []int{http.StatusNotFound})
return
}