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-- |
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 |
|
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 | 16 |
var endPoint string |
17 |
var tl []Status |
|
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 | 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 | 29 |
return tl, fmt.Errorf("GetTimelines: bad timelines argument") |
30 |
} |
|
31 |
||
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 | 38 |
r, err := rest.API(req) |
39 |
if err != nil { |
|
40 |
return tl, fmt.Errorf("timelines API query: %s", err.Error()) |
|
41 |
} |
|
42 |
||
43 |
err = json.Unmarshal([]byte(r.Body), &tl) |
|
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 | 50 |
return tl, fmt.Errorf("timelines API: %s", err.Error()) |
51 |
} |
|
52 |
||
53 |
return tl, nil |
|
54 |
} |