author | Mikael Berthe <mikael@lilotux.net> |
Wed, 12 Apr 2017 23:36:46 +0200 | |
changeset 94 | beee0238a82e |
parent 92 | 05c201b548b0 |
child 98 | 5d803adfc57e |
permissions | -rw-r--r-- |
88 | 1 |
package gondole |
2 |
||
3 |
import ( |
|
4 |
"encoding/json" |
|
5 |
"fmt" |
|
6 |
"strings" |
|
7 |
||
8 |
"github.com/sendgrid/rest" |
|
9 |
) |
|
10 |
||
11 |
// GetTimelines returns a timeline (a list of statuses |
|
12 |
// timeline can be "home", "public", or a hashtag (":hashtag") |
|
13 |
func (g *Client) GetTimelines(timeline string) ([]Status, error) { |
|
14 |
var endPoint string |
|
15 |
var tl []Status |
|
16 |
||
17 |
if timeline == "home" || timeline == "public" { |
|
18 |
endPoint = "timelines/" + timeline |
|
19 |
} else if strings.HasPrefix(timeline, ":") { |
|
94
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
20 |
hashtag := timeline[1:] |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
21 |
if hashtag == "" { |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
22 |
return tl, fmt.Errorf("timelines API: empty hashtag") |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
23 |
} |
beee0238a82e
Timelines: fix hashtag timelines
Mikael Berthe <mikael@lilotux.net>
parents:
92
diff
changeset
|
24 |
endPoint = "timelines/tag/" + hashtag |
88 | 25 |
} else { |
26 |
return tl, fmt.Errorf("GetTimelines: bad timelines argument") |
|
27 |
} |
|
28 |
||
29 |
req := g.prepareRequest(endPoint) |
|
30 |
r, err := rest.API(req) |
|
31 |
if err != nil { |
|
32 |
return tl, fmt.Errorf("timelines API query: %s", err.Error()) |
|
33 |
} |
|
34 |
||
35 |
err = json.Unmarshal([]byte(r.Body), &tl) |
|
36 |
if err != nil { |
|
92
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
37 |
var res struct { |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
38 |
Error string `json:"error"` |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
39 |
} |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
40 |
err2 := json.Unmarshal([]byte(r.Body), &res) |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
41 |
if err2 == nil { |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
42 |
return tl, fmt.Errorf("%s", res.Error) |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
43 |
} |
88 | 44 |
return tl, fmt.Errorf("timelines API: %s", err.Error()) |
45 |
} |
|
46 |
||
47 |
return tl, nil |
|
48 |
} |