Skip to content

Commit

Permalink
Switch from EventStore feed processing to StreamEventPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
pjvds committed Aug 13, 2013
1 parent 847b807 commit a3f76bd
Showing 1 changed file with 7 additions and 51 deletions.
58 changes: 7 additions & 51 deletions storage/eventstore/EventStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/pjvds/feeds"
"github.com/pjvds/go-cqrs/storage"
"github.com/pjvds/go-cqrs/storage/serialization"
"io/ioutil"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -80,64 +78,22 @@ func (store *EventStore) ReadStream(streamId storage.EventStreamId) ([]*storage.
}

for pointer != nil {
event, err := store.downloadEvent(pointer.EventUrl)
data, err := pointer.DownloadEvent()
if err != nil {
return nil, err
}

events = append(events, event)
next, err := pointer.Next()

pointer = next
}

return events, nil
}

func linksToMap(links []*feeds.AtomLink) map[string]string {
m := make(map[string]string, len(links))
for _, link := range links {
m[link.Rel] = link.Href
}

return m
}

func (store *EventStore) processFeed(feed *feeds.AtomFeed) ([]*storage.Event, error) {
result := make([]*storage.Event, len(feed.Entries))
for index, entry := range feed.Entries {
alternateLink := entry.Links[1]
eventUrl := alternateLink.Href

event, err := store.downloadEvent(eventUrl)
// Todo: remove eventname, this is not needed any more.
event, err := store.serializer.Deserialize(*storage.NewEventName(""), data)
if err != nil {
return nil, err
}
result[index] = event
}

return result, nil
}

func (store *EventStore) downloadEvent(url string) (*storage.Event, error) {
r, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

r.Header.Add("Accept", "application/json")
c := http.Client{}

response, err := c.Do(r)
if err != nil {
return nil, err
}
events = append(events, event)
next, err := pointer.Next()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
pointer = next
}

event, err := store.serializer.Deserialize(*storage.NewEventName(""), body)
return event, err
return events, nil
}

0 comments on commit a3f76bd

Please sign in to comment.