streams.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 14 Apr 2017 22:56:50 +0200
changeset 117 7f8ac782cf5d
child 120 579912e9d0ef
permissions -rw-r--r--
Add streaming API support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
117
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"bufio"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"bytes"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
	"encoding/json"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
	"errors"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	"fmt"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"io"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"log"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"net/http"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"strings"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"time"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"github.com/sendgrid/rest"
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
// StreamEvent contains a single event from the streaming API
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
type StreamEvent struct {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	Event string      // Name of the event (error, update, notification or delete)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	Data  interface{} // Status, Notification or status ID
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	Error error       // Error message from the StreamListener
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
// openStream opens a stream URL and returns an http.Response
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
// Note that the caller should close the connection when it's done reading
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
// the stream.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
// The stream name can be "user", "public" or "hashtag".
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
// For "hashtag", the hashTag argument cannot be empty.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
func (g *Client) openStream(streamName, hashTag string) (*http.Response, error) {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	req := g.prepareRequest("streaming/" + streamName)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	switch streamName {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	case "user", "public":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	case "hashtag":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
		if hashTag == "" {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
			return nil, ErrInvalidParameter
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
		req.QueryParams["tag"] = hashTag
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	default:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		return nil, ErrInvalidParameter
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	reqObj, err := rest.BuildRequestObject(req)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		return nil, fmt.Errorf("cannot build stream request: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	resp, err := rest.MakeRequest(reqObj)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		return nil, fmt.Errorf("cannot open stream: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	if resp.StatusCode != 200 {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		resp.Body.Close()
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		return nil, errors.New(resp.Status)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
	return resp, nil
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
// readStream reads from the http.Response and sends events to the events channel
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
// It stops when the connection is closed or when teh stopCh channel is closed.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
func (g *Client) readStream(events chan<- StreamEvent, stopCh <-chan bool, r *http.Response) {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	defer r.Body.Close()
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	reader := bufio.NewReader(r.Body)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	var line, eventName string
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	for {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
		select {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
		case <-stopCh:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
			close(events)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
			return
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
		default:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		lineBytes, partial, err := reader.ReadLine()
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
		if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
			if err == io.EOF {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
				e := fmt.Errorf("connection closed: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
				close(events)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
				return
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
			e := fmt.Errorf("ReadLine read error: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
			time.Sleep(10 * time.Second)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		if partial {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
			e := fmt.Errorf("received incomplete line; not supported yet")
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
			time.Sleep(5 * time.Second)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
			continue // Skip this
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
		line = string(bytes.TrimSpace(lineBytes))
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
		if line == "" {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
			continue // Skip empty line
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
		if strings.HasPrefix(line, ":") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
			continue // Skip comment
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		if strings.HasPrefix(line, "event: ") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
			eventName = line[7:]
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
		if !strings.HasPrefix(line, "data: ") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
			// XXX Needs improvement
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
			e := fmt.Errorf("received unhandled event line '%s'", strings.Split(line, ":")[0])
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
		// This is a data line
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
		data := []byte(line[6:])
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
		var obj interface{}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
		// Decode API object
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
		switch eventName {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		case "update":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
			var s Status
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
			if err := json.Unmarshal(data, &s); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
			obj = s
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
		case "notification":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
			var notif Notification
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
			if err := json.Unmarshal(data, &notif); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
			obj = notif
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
		case "delete":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
			var statusID int
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
			if err := json.Unmarshal(data, &statusID); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
			obj = statusID
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
		case "":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
			fallthrough
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
		default:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
			e := fmt.Errorf("unhandled event '%s'", eventName)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
		// Send event to the channel
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
		events <- StreamEvent{Event: eventName, Data: obj}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   167
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   168
// StreamListener listens to a stream from the Mastodon server
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
// The stream 'name' can be "user", "public" or "hashtag".
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
// For 'hashtag', the hashTag argument cannot be empty.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
// The events are sent to the events channel (the errors as well).
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
// The streaming is terminated if the stop channel is closed.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
// Please note that this method launches a goroutine to listen to the events.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   174
// The 'events' channel is closed if the connection is closed by the server.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
func (g *Client) StreamListener(name, hashTag string, events chan<- StreamEvent, stopCh <-chan bool) error {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
	resp, err := g.openStream(name, hashTag)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
		return err
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
	go g.readStream(events, stopCh, resp)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
	return nil
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
}