streams.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 15 Apr 2017 10:26:36 +0200
changeset 120 579912e9d0ef
parent 117 7f8ac782cf5d
child 122 50c7733ee886
permissions -rw-r--r--
Refactor API calls
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) {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 117
diff changeset
    31
	params := make(apiCallParams)
117
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
		}
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 117
diff changeset
    39
		params["tag"] = hashTag
117
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
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 117
diff changeset
    44
	req := g.prepareRequest("streaming/"+streamName, rest.Get, params)
117
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	reqObj, err := rest.BuildRequestObject(req)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		return nil, fmt.Errorf("cannot build stream request: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	}
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 117
diff changeset
    49
117
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	resp, err := rest.MakeRequest(reqObj)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
		return nil, fmt.Errorf("cannot open stream: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	if resp.StatusCode != 200 {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		resp.Body.Close()
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		return nil, errors.New(resp.Status)
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
	return resp, nil
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
// 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
    62
// 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
    63
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
    64
	defer r.Body.Close()
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
	reader := bufio.NewReader(r.Body)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
	var line, eventName string
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	for {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
		select {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
		case <-stopCh:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
			close(events)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
			return
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
		default:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
		lineBytes, partial, err := reader.ReadLine()
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
		if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
			if err == io.EOF {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
				e := fmt.Errorf("connection closed: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
				close(events)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
				return
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
			e := fmt.Errorf("ReadLine read error: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
			time.Sleep(10 * time.Second)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
		if partial {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
			e := fmt.Errorf("received incomplete line; not supported yet")
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
			time.Sleep(5 * time.Second)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
			continue // Skip this
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
		}
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
		line = string(bytes.TrimSpace(lineBytes))
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
		if line == "" {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
			continue // Skip empty line
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		if strings.HasPrefix(line, ":") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
			continue // Skip comment
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
		if strings.HasPrefix(line, "event: ") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
			eventName = line[7:]
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		if !strings.HasPrefix(line, "data: ") {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
			// XXX Needs improvement
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
			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
   117
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
		// This is a data line
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
		data := []byte(line[6:])
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
		var obj interface{}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		// Decode API object
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
		switch eventName {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
		case "update":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
			var s Status
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
			if err := json.Unmarshal(data, &s); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
			obj = s
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
		case "notification":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
			var notif Notification
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
			if err := json.Unmarshal(data, &notif); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
			obj = notif
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
		case "delete":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
			var statusID int
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
			if err := json.Unmarshal(data, &statusID); err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
				e := fmt.Errorf("could not unmarshal data: %s", err.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
				log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
				events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
				continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
			}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
			obj = statusID
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
		case "":
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
			fallthrough
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
		default:
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
			e := fmt.Errorf("unhandled event '%s'", eventName)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
			log.Printf("Stream Reader: %s", e.Error())
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
			events <- StreamEvent{Event: "error", Error: e}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
			continue
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
		}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
		// Send event to the channel
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
		events <- StreamEvent{Event: eventName, Data: obj}
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
}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
// StreamListener listens to a stream from the Mastodon server
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
// The stream 'name' can be "user", "public" or "hashtag".
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
// For 'hashtag', the hashTag argument cannot be empty.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
// 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
   174
// The streaming is terminated if the stop channel is closed.
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
// 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
   176
// 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
   177
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
   178
	resp, err := g.openStream(name, hashTag)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
	if err != nil {
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
		return err
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
	}
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
	go g.readStream(events, stopCh, resp)
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
	return nil
7f8ac782cf5d Add streaming API support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
}