Skip to content

Commit

Permalink
Implement GET and POST /events
Browse files Browse the repository at this point in the history
  • Loading branch information
khlieng committed Jul 7, 2015
1 parent 3c14c44 commit b6d343f
Show file tree
Hide file tree
Showing 12 changed files with 1,001 additions and 593 deletions.
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func createRouter() *echo.Echo {
episodes.Get("/episode/:id", getEpisode)
episodes.Get("/episodes/label/:label", getEpisodesByLabel)

/*events := r.Group("/library/events")
events.Get("")
events.Post("")
events := r.Group("/library/events")
events.Get("", getEvents)
events.Post("", addEvents)

labels := r.Group("/library/labels")
/*labels := r.Group("/library/labels")
labels.Get("")
labels.Post("")
labels.Put("/:id")
Expand Down
16 changes: 16 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func initTestData() {
UUID: "real_unique",
Name: "Castcloud",
})
store.AddClient(1, &Client{
Token: "evtest1",
UUID: "evtestuuid1",
Name: "Castcloud",
})
store.SaveCast(&Cast{
URL: "test.go",
Name: "test",
Expand All @@ -68,6 +73,17 @@ func initTestData() {
CrawlTS: 32503680001,
})
store.AddSubscription(1, 1)
store.AddEvents([]Event{
Event{
Type: 30,
EpisodeID: 10,
PositionTS: 911,
ClientTS: 10,
ConcurrentOrder: 0,
ClientName: "Castcloud",
ClientDescription: "oink",
},
}, 1, "evtestuuid1")
}

func testHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 2 additions & 0 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func auth() echo.HandlerFunc {
if user != nil {
c.Set("user", user)
c.Set("token", token)
c.Set("uuid", user.UUID(token))
return nil
}

Expand All @@ -31,6 +32,7 @@ func auth() echo.HandlerFunc {
authCache.set(token, user)
c.Set("user", user)
c.Set("token", token)
c.Set("uuid", user.UUID(token))
}

return nil
Expand Down
63 changes: 63 additions & 0 deletions api/events.go
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
package api

import (
"encoding/json"
"strconv"
"time"

"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo"

. "github.com/Castcloud/castcloud-go-server/api/schema"
)

type events struct {
Timestamp int64 `json:"timestamp"`
Events []Event `json:"events"`
}

//
// GET /library/events
//
func getEvents(c *echo.Context) error {
now := time.Now().Unix()
var err error
var ts uint64
since := c.Request().URL.Query().Get("since")
if since != "" {
ts, err = strconv.ParseUint(since, 10, 64)
if err != nil {
return c.NoContent(400)
}
}

user := c.Get("user").(*User)
uuid := ""
excludeSelf := c.Request().URL.Query().Get("exclude_self")
if excludeSelf == "true" {
uuid = c.Get("uuid").(string)
}

return c.JSON(200, events{
Timestamp: now,
Events: store.GetEvents(user.ID, ts, uuid),
})
}

//
// POST /library/events
//
func addEvents(c *echo.Context) error {
data := form(c, "json")
if data == "" {
return c.NoContent(400)
}

events := []Event{}
err := json.Unmarshal([]byte(data), &events)
if err != nil {
return c.NoContent(400)
}

user := c.Get("user").(*User)
uuid := c.Get("uuid").(string)
return store.AddEvents(events, user.ID, uuid)
}
71 changes: 71 additions & 0 deletions api/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package api

import (
"encoding/json"
"net/url"
"testing"
"time"

"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/stretchr/testify/assert"

. "github.com/Castcloud/castcloud-go-server/api/schema"
)

func TestGetEvents(t *testing.T) {
r := createRouter()

// It should return 400 if since is bad
req := testRequest(r, "GET", "/library/events?since=yesterday", nil)
req.Header.Set("Authorization", "evtest1")
assert.Equal(t, 400, req.send().Code)

// It should return 200 with a timestamp and a list of events
req.URL, _ = url.Parse("/library/events?since=5")
res := checkEvents(t, req)
assert.Len(t, res.Events, 1)

// It should exclude events sent from this clients UUID when
// exclude_self is true
req.URL, _ = url.Parse("/library/events?since=5&exclude_self=true")
res = checkEvents(t, req)
assert.Len(t, res.Events, 0)
}

func checkEvents(t *testing.T, req testReq) events {
now := time.Now().Unix()
res := req.send()
assert.Equal(t, 200, res.Code)
data := events{}
err := json.Unmarshal(res.Body.Bytes(), &data)
assert.Nil(t, err)
assert.True(t, data.Timestamp >= now)
return data
}

func TestAddEvents(t *testing.T) {
r := createRouter()

// It should return 400 if json is not set
req := testRequest(r, "POST", "/library/events", nil)
req.Header.Set("Authorization", "evtest1")
assert.Equal(t, 400, req.send().Code)

// It should return 400 if the json is bad
req.PostForm = url.Values{}
req.PostForm.Set("json", "real_bad_json")
assert.Equal(t, 400, req.send().Code)

// It should return 200 when proper json is sent
req.PostForm.Set("json", testJSON([]Event{
Event{
Type: 30,
EpisodeID: 10,
PositionTS: 911,
ClientTS: 10,
ConcurrentOrder: 0,
ClientName: "Castcloud",
ClientDescription: "oink",
},
}))
assert.Equal(t, 200, req.send().Code)
}
18 changes: 17 additions & 1 deletion api/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ type Episode struct {
}

type Event struct {
ID uint64
Type int `json:"type"`
EpisodeID uint64 `json:"episodeid"`
PositionTS int `json:"positionts"`
ClientTS uint64 `json:"clientts"`
ConcurrentOrder int `json:"concurrentorder"`
ClientName string `json:"clientname"`
ClientDescription string `json:"clientdescription"`
ClientUUID string `json:"-"`
}

func (u *User) UUID(token string) string {
for _, client := range u.Clients {
if client.Token == token {
return client.UUID
}
}
return ""
}

func (c *Cast) EncodeFeed() {
Expand Down
Loading

0 comments on commit b6d343f

Please sign in to comment.