Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #53 from linksmart/notification_api_changes
Browse files Browse the repository at this point in the history
Pass type in path instead of query. Change full to diff
  • Loading branch information
farshidtz authored Jun 7, 2021
2 parents 939e38a + a429ee3 commit f62ea7a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func setupHTTPRouter(config *HTTPConfig, api *catalog.HTTPAPI, notifAPI *notific

//TD notification
r.get("/events", commonHandlers.ThenFunc(notifAPI.SubscribeEvent))
r.get("/events/{type}", commonHandlers.ThenFunc(notifAPI.SubscribeEvent))

logger := negroni.NewLogger()
logFlags := log.LstdFlags
Expand Down
8 changes: 4 additions & 4 deletions notification/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Controller struct {
type subscriber struct {
client chan Event
eventTypes []wot.EventType
full bool
diff bool
lastEventID string
}

Expand All @@ -48,10 +48,10 @@ func NewController(s EventQueue) *Controller {
return c
}

func (c *Controller) subscribe(client chan Event, eventTypes []wot.EventType, full bool, lastEventID string) error {
func (c *Controller) subscribe(client chan Event, eventTypes []wot.EventType, diff bool, lastEventID string) error {
s := subscriber{client: client,
eventTypes: eventTypes,
full: full,
diff: diff,
lastEventID: lastEventID,
}
c.subscribingClients <- s
Expand Down Expand Up @@ -174,7 +174,7 @@ func sendToSubscriber(s subscriber, event Event) {
// Send the notification if the type matches
if eventType == event.Type {
toSend := event
if !s.full {
if !s.diff {
toSend.Data = catalog.ThingDescription{wot.KeyThingID: toSend.Data[wot.KeyThingID]}
}
s.client <- toSend
Expand Down
2 changes: 1 addition & 1 deletion notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Event struct {
// NotificationController interface
type NotificationController interface {
// subscribe to the events. the caller will get events through the channel 'client' starting from 'lastEventID'
subscribe(client chan Event, eventTypes []wot.EventType, full bool, lastEventID string) error
subscribe(client chan Event, eventTypes []wot.EventType, diff bool, lastEventID string) error

// unsubscribe and close the channel 'client'
unsubscribe(client chan Event) error
Expand Down
50 changes: 24 additions & 26 deletions notification/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/linksmart/thing-directory/catalog"
"github.com/linksmart/thing-directory/wot"
)

const (
QueryParamType = "type"
QueryParamFull = "full"
QueryParamFull = "diff"
HeaderLastEventID = "Last-Event-ID"
)

Expand All @@ -35,7 +36,11 @@ func NewSSEAPI(controller NotificationController, version string) *SSEAPI {
}

func (a *SSEAPI) SubscribeEvent(w http.ResponseWriter, req *http.Request) {
eventTypes, full, err := parseQueryParameters(req)
diff, err := parseQueryParameters(req)
if err != nil {
catalog.ErrorResponse(w, http.StatusBadRequest, err)
}
eventTypes, err := parsePath(req)
if err != nil {
catalog.ErrorResponse(w, http.StatusBadRequest, err)
}
Expand All @@ -49,7 +54,7 @@ func (a *SSEAPI) SubscribeEvent(w http.ResponseWriter, req *http.Request) {
messageChan := make(chan Event)

lastEventID := req.Header.Get(HeaderLastEventID)
a.controller.subscribe(messageChan, eventTypes, full, lastEventID)
a.controller.subscribe(messageChan, eventTypes, diff, lastEventID)

go func() {
<-req.Context().Done()
Expand All @@ -71,36 +76,29 @@ func (a *SSEAPI) SubscribeEvent(w http.ResponseWriter, req *http.Request) {
}
}

func parseQueryParameters(req *http.Request) ([]wot.EventType, bool, error) {

full := false
func parseQueryParameters(req *http.Request) (bool, error) {
diff := false
req.ParseForm()

// Parse full or partial events
// Parse diff or just ID
if strings.EqualFold(req.Form.Get(QueryParamFull), "true") {
full = true
diff = true
}
return diff, nil
}

func parsePath(req *http.Request) ([]wot.EventType, error) {
// Parse event type to be subscribed to
queriedTypes := req.Form[QueryParamType]
if queriedTypes == nil {
return []wot.EventType{wot.EventTypeCreate, wot.EventTypeUpdate, wot.EventTypeDelete}, full, nil
params := mux.Vars(req)
event := params[QueryParamType]
if event == "" {
return []wot.EventType{wot.EventTypeCreate, wot.EventTypeUpdate, wot.EventTypeDelete}, nil
}

var eventTypes []wot.EventType
loopQueriedTypes:
for _, v := range queriedTypes {
eventType := wot.EventType(v)
if !eventType.IsValid() {
return nil, false, fmt.Errorf("invalid type parameter")
}
for _, existing := range eventTypes {
if existing == eventType {
continue loopQueriedTypes
}
}
eventTypes = append(eventTypes, eventType)
eventType := wot.EventType(event)
if !eventType.IsValid() {
return nil, fmt.Errorf("invalid type in path")
}

return eventTypes, full, nil
return []wot.EventType{eventType}, nil

}

0 comments on commit f62ea7a

Please sign in to comment.