author | Mikael Berthe <mikael@lilotux.net> |
Wed, 12 Apr 2017 23:02:15 +0200 | |
changeset 92 | 05c201b548b0 |
parent 88 | df00ec8423fe |
child 94 | beee0238a82e |
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, ":") { |
|
20 |
endPoint = "timelines/tag/" + timeline |
|
21 |
} else { |
|
22 |
return tl, fmt.Errorf("GetTimelines: bad timelines argument") |
|
23 |
} |
|
24 |
||
25 |
req := g.prepareRequest(endPoint) |
|
26 |
r, err := rest.API(req) |
|
27 |
if err != nil { |
|
28 |
return tl, fmt.Errorf("timelines API query: %s", err.Error()) |
|
29 |
} |
|
30 |
||
31 |
err = json.Unmarshal([]byte(r.Body), &tl) |
|
32 |
if err != nil { |
|
92
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
33 |
var res struct { |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
34 |
Error string `json:"error"` |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
35 |
} |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
36 |
err2 := json.Unmarshal([]byte(r.Body), &res) |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
37 |
if err2 == nil { |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
38 |
return tl, fmt.Errorf("%s", res.Error) |
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
39 |
} |
88 | 40 |
return tl, fmt.Errorf("timelines API: %s", err.Error()) |
41 |
} |
|
42 |
||
43 |
return tl, nil |
|
44 |
} |