timelines.go
changeset 120 579912e9d0ef
parent 118 d9c798e09f0a
child 127 96a7f2432d27
equal deleted inserted replaced
119:22c8c58ad61b 120:579912e9d0ef
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
       
     5 	"fmt"
     4 	"fmt"
     6 	"strings"
     5 	"strings"
     7 
     6 
     8 	"github.com/sendgrid/rest"
     7 	"github.com/sendgrid/rest"
     9 )
     8 )
    12 // timeline can be "home", "public", or a hashtag (use ":hashtag")
    11 // timeline can be "home", "public", or a hashtag (use ":hashtag")
    13 // For the public timelines, you can set 'local' to true to get only the
    12 // For the public timelines, you can set 'local' to true to get only the
    14 // local instance.
    13 // local instance.
    15 func (g *Client) GetTimelines(timeline string, local bool) ([]Status, error) {
    14 func (g *Client) GetTimelines(timeline string, local bool) ([]Status, error) {
    16 	var endPoint string
    15 	var endPoint string
    17 	var tl []Status
       
    18 
    16 
    19 	switch {
    17 	switch {
    20 	case timeline == "home", timeline == "public":
    18 	case timeline == "home", timeline == "public":
    21 		endPoint = "timelines/" + timeline
    19 		endPoint = "timelines/" + timeline
    22 	case strings.HasPrefix(timeline, ":"):
    20 	case strings.HasPrefix(timeline, ":"):
    23 		hashtag := timeline[1:]
    21 		hashtag := timeline[1:]
    24 		if hashtag == "" {
    22 		if hashtag == "" {
    25 			return tl, fmt.Errorf("timelines API: empty hashtag")
    23 			return nil, fmt.Errorf("timelines API: empty hashtag")
    26 		}
    24 		}
    27 		endPoint = "timelines/tag/" + hashtag
    25 		endPoint = "timelines/tag/" + hashtag
    28 	default:
    26 	default:
    29 		return tl, fmt.Errorf("GetTimelines: bad timelines argument")
    27 		return nil, fmt.Errorf("GetTimelines: bad timelines argument")
    30 	}
    28 	}
    31 
    29 
    32 	req := g.prepareRequest(endPoint)
    30 	params := make(apiCallParams)
    33 
       
    34 	if timeline == "public" && local {
    31 	if timeline == "public" && local {
    35 		req.QueryParams["local"] = "true"
    32 		params["local"] = "true"
    36 	}
    33 	}
    37 
    34 
    38 	r, err := rest.API(req)
    35 	var tl []Status
    39 	if err != nil {
    36 	if err := g.apiCall(endPoint, rest.Get, params, &tl); err != nil {
    40 		return tl, fmt.Errorf("timelines API query: %s", err.Error())
    37 		return nil, err
    41 	}
    38 	}
    42 
       
    43 	err = json.Unmarshal([]byte(r.Body), &tl)
       
    44 	if err != nil {
       
    45 		var errorRes Error
       
    46 		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
       
    47 		if err2 == nil {
       
    48 			return tl, fmt.Errorf("%s", errorRes.Text)
       
    49 		}
       
    50 		return tl, fmt.Errorf("timelines API: %s", err.Error())
       
    51 	}
       
    52 
       
    53 	return tl, nil
    39 	return tl, nil
    54 }
    40 }