-
Notifications
You must be signed in to change notification settings - Fork 145
/
mock_tags.go
149 lines (128 loc) · 3.21 KB
/
mock_tags.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
package mailgun
import (
"net/http"
"net/url"
"time"
"github.com/go-chi/chi/v5"
)
func (ms *mockServer) addTagsRoutes(r chi.Router) {
r.Get("/{domain}/tags", ms.listTags)
r.Get("/{domain}/tags/{tag}", ms.getTags)
r.Delete("/{domain}/tags/{tag}", ms.deleteTags)
r.Put("/{domain}/tags/{tag}", ms.createUpdateTags)
tenMinutesBefore := time.Now().Add(-10 * time.Minute)
now := time.Now()
ms.tags = append(ms.tags, Tag{
Value: "test",
Description: "test description",
FirstSeen: &tenMinutesBefore,
LastSeen: &now,
})
ms.tags = append(ms.tags, Tag{
Value: "test2",
Description: "test2 description",
FirstSeen: &tenMinutesBefore,
LastSeen: &now,
})
}
func (ms *mockServer) listTags(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var idx []string
for _, t := range ms.unsubscribes {
idx = append(idx, t.Address)
}
limit := stringToInt(r.FormValue("limit"))
if limit == 0 {
limit = 100
}
page := r.FormValue("page")
var pivot string
if len(page) != 0 {
pivot = r.FormValue("p")
if pivot == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"message\": \"Invalid parameter: pivot \"}"))
return
}
}
start, end := pageOffsets(idx, page, pivot, limit)
var nextAddress, prevAddress string
var results []Tag
if start != end {
results = ms.tags[start:end]
nextAddress = results[len(results)-1].Value
prevAddress = results[0].Value
} else {
results = []Tag{}
nextAddress = pivot
prevAddress = pivot
}
toJSON(w, tagsResponse{
Paging: Paging{
First: getPageURL(r, url.Values{
"page": []string{"first"},
}),
Last: getPageURL(r, url.Values{
"page": []string{"last"},
}),
Next: getPageURL(r, url.Values{
"page": []string{"next"},
"p": []string{nextAddress},
}),
Previous: getPageURL(r, url.Values{
"page": []string{"prev"},
"p": []string{prevAddress},
}),
},
Items: results,
})
}
func (ms *mockServer) getTags(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for _, tag := range ms.tags {
if tag.Value == chi.URLParam(r, "tag") {
toJSON(w, tag)
return
}
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{\"message\": \"Tag not found\"}"))
}
func (ms *mockServer) createUpdateTags(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
tag := chi.URLParam(r, "tag")
description := r.FormValue("description")
var tagExists bool
for i, existingTag := range ms.tags {
if tag == existingTag.Value {
ms.tags[i].Description = description
tagExists = true
}
}
if !tagExists {
ms.tags = append(ms.tags, Tag{Value: tag, Description: description})
}
toJSON(w, map[string]interface{}{
"message": "Tag updated",
})
}
func (ms *mockServer) deleteTags(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for i, existingTag := range ms.tags {
if existingTag.Value == chi.URLParam(r, "tag") {
ms.tags = append(ms.tags[:i], ms.tags[i+1:len(ms.tags)]...)
}
}
toJSON(w, map[string]interface{}{
"message": "Tag deleted",
})
}