timelines.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 15 Apr 2017 10:26:36 +0200
changeset 120 579912e9d0ef
parent 118 d9c798e09f0a
child 127 96a7f2432d27
permissions -rw-r--r--
Refactor API calls
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
	"fmt"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"strings"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
	"github.com/sendgrid/rest"
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
)
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
// 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
    11
// 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
    12
// 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
    13
// local instance.
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    14
func (g *Client) GetTimelines(timeline string, local bool) ([]Status, error) {
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	var endPoint string
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    17
	switch {
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    18
	case timeline == "home", timeline == "public":
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
		endPoint = "timelines/" + timeline
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    20
	case strings.HasPrefix(timeline, ":"):
94
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    21
		hashtag := timeline[1:]
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    22
		if hashtag == "" {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    23
			return nil, fmt.Errorf("timelines API: empty hashtag")
94
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    24
		}
beee0238a82e Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents: 92
diff changeset
    25
		endPoint = "timelines/tag/" + hashtag
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    26
	default:
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    27
		return nil, fmt.Errorf("GetTimelines: bad timelines argument")
118
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    28
	}
d9c798e09f0a Add an option to get the local timeline
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    29
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    30
	params := make(apiCallParams)
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    31
	if timeline == "public" && local {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    32
		params["local"] = "true"
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    35
	var tl []Status
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    36
	if err := g.apiCall(endPoint, rest.Get, params, &tl); err != nil {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 118
diff changeset
    37
		return nil, err
88
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	}
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	return tl, nil
df00ec8423fe Add timelines support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
}