author | Mikael Berthe <mikael@lilotux.net> |
Thu, 13 Apr 2017 00:17:57 +0200 | |
changeset 98 | 5d803adfc57e |
parent 94 | beee0238a82e |
child 118 | d9c798e09f0a |
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 { |
|
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
94
diff
changeset
|
37 |
var errorRes Error |
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
94
diff
changeset
|
38 |
err2 := json.Unmarshal([]byte(r.Body), &errorRes) |
92
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
39 |
if err2 == nil { |
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
94
diff
changeset
|
40 |
return tl, fmt.Errorf("%s", errorRes.Text) |
92
05c201b548b0
Timelines: Handle error from API server
Mikael Berthe <mikael@lilotux.net>
parents:
88
diff
changeset
|
41 |
} |
88 | 42 |
return tl, fmt.Errorf("timelines API: %s", err.Error()) |
43 |
} |
|
44 |
||
45 |
return tl, nil |
|
46 |
} |