Renamed apiv1 lib to xaapiv1.
[src/xds/xds-agent.git] / lib / agent / apiv1-events.go
1 package agent
2
3 import (
4         "net/http"
5
6         "github.com/gin-gonic/gin"
7         "github.com/iotbzh/xds-agent/lib/xaapiv1"
8         common "github.com/iotbzh/xds-common/golib"
9 )
10
11 // eventsList Registering for events that will be send over a WS
12 func (s *APIService) eventsList(c *gin.Context) {
13         c.JSON(http.StatusOK, s.events.GetList())
14 }
15
16 // eventsRegister Registering for events that will be send over a WS
17 func (s *APIService) eventsRegister(c *gin.Context) {
18         var args xaapiv1.EventRegisterArgs
19
20         if c.BindJSON(&args) != nil || args.Name == "" {
21                 common.APIError(c, "Invalid arguments")
22                 return
23         }
24
25         sess := s.webServer.sessions.Get(c)
26         if sess == nil {
27                 common.APIError(c, "Unknown sessions")
28                 return
29         }
30
31         // Register to all or to a specific events
32         if err := s.events.Register(args.Name, sess.ID); err != nil {
33                 common.APIError(c, err.Error())
34                 return
35         }
36
37         c.JSON(http.StatusOK, gin.H{"status": "OK"})
38 }
39
40 // eventsRegister Registering for events that will be send over a WS
41 func (s *APIService) eventsUnRegister(c *gin.Context) {
42         var args xaapiv1.EventUnRegisterArgs
43
44         if c.BindJSON(&args) != nil || args.Name == "" {
45                 common.APIError(c, "Invalid arguments")
46                 return
47         }
48
49         sess := s.webServer.sessions.Get(c)
50         if sess == nil {
51                 common.APIError(c, "Unknown sessions")
52                 return
53         }
54
55         // Register to all or to a specific events
56         if err := s.events.UnRegister(args.Name, sess.ID); err != nil {
57                 common.APIError(c, err.Error())
58                 return
59         }
60
61         c.JSON(http.StatusOK, gin.H{"status": "OK"})
62 }