-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnotify.go
69 lines (55 loc) · 1.78 KB
/
notify.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
package apiserver
import (
"fmt"
"github.com/laincloud/deployd/engine"
"github.com/mijia/sweb/form"
"github.com/mijia/sweb/server"
"golang.org/x/net/context"
"net/http"
"net/url"
)
type RestfulNotifies struct {
server.BaseResource
}
func (rc RestfulNotifies) Get(ctx context.Context, r *http.Request) (int, interface{}) {
notifies := getEngine(ctx).GetNotifies()
if len(notifies) == 0 {
return http.StatusNotFound, fmt.Sprintf("No notify found")
} else {
return http.StatusOK, notifies
}
}
func (rc RestfulNotifies) Post(ctx context.Context, r *http.Request) (int, interface{}) {
callback := form.ParamString(r, "callback", "")
if callback == "" {
return http.StatusBadRequest, "constaint type required"
}
if _, err := url.ParseRequestURI(callback); err != nil {
return http.StatusBadRequest, fmt.Sprintf("callback url not valid: %s", err)
}
if err := getEngine(ctx).AddNotify(callback); err != nil {
return http.StatusInternalServerError, err.Error()
}
urlReverser := getUrlReverser(ctx)
return http.StatusAccepted, map[string]string{
"message": "notify will be added",
"check_url": urlReverser.Reverse("Get_RestfulNotifies"),
}
}
func (rc RestfulNotifies) Delete(ctx context.Context, r *http.Request) (int, interface{}) {
callback := form.ParamString(r, "callback", "")
if callback == "" {
return http.StatusBadRequest, "callback value requird"
}
if err := getEngine(ctx).DeleteNotify(callback); err != nil {
if err == engine.ErrNotifyNotExists {
return http.StatusNotFound, err.Error()
}
return http.StatusInternalServerError, err.Error()
}
urlReverser := getUrlReverser(ctx)
return http.StatusAccepted, map[string]string{
"message": "notify uri will be deleted from the orc engine.",
"check_url": urlReverser.Reverse("Get_RestfulNotifies"),
}
}