timelines.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 14 Apr 2017 23:25:51 +0200
changeset 118 d9c798e09f0a
parent 98 5d803adfc57e
child 120 579912e9d0ef
permissions -rw-r--r--
Add an option to get the local timeline
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"encoding/json"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"fmt"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
	"strings"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	"github.com/sendgrid/rest"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
)
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
// GetTimelines returns a timeline (a list of statuses
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    12
// timeline can be "home", "public", or a hashtag (use ":hashtag")
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    13
// For the public timelines, you can set 'local' to true to get only the
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    14
// local instance.
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    15
func (g *Client) GetTimelines(timeline string, local bool) ([]Status, error) {
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	var endPoint string
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	var tl []Status
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    19
	switch {
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    20
	case timeline == "home", timeline == "public":
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
		endPoint = "timelines/" + timeline
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    22
	case strings.HasPrefix(timeline, ":"):
94
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    23
		hashtag := timeline[1:]
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    24
		if hashtag == "" {
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    25
			return tl, fmt.Errorf("timelines API: empty hashtag")
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    26
		}
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    27
		endPoint = "timelines/tag/" + hashtag
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    28
	default:
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		return tl, fmt.Errorf("GetTimelines: bad timelines argument")
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	req := g.prepareRequest(endPoint)
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    33
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    34
	if timeline == "public" && local {
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    35
		req.QueryParams["local"] = "true"
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    36
	}
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    37
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	r, err := rest.API(req)
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	if err != nil {
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
		return tl, fmt.Errorf("timelines API query: %s", err.Error())
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	err = json.Unmarshal([]byte(r.Body), &tl)
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	if err != nil {
98
5d803adfc57e Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents: 94
diff changeset
    45
		var errorRes Error
5d803adfc57e Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents: 94
diff changeset
    46
		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
92
05c201b548b0 Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents: 88
diff changeset
    47
		if err2 == nil {
98
5d803adfc57e Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents: 94
diff changeset
    48
			return tl, fmt.Errorf("%s", errorRes.Text)
92
05c201b548b0 Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents: 88
diff changeset
    49
		}
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		return tl, fmt.Errorf("timelines API: %s", err.Error())
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	return tl, nil
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
}