status.go
changeset 103 9860bb68a0a6
parent 8 6d89be3dd966
child 105 e2a16e19eb8b
equal deleted inserted replaced
102:187aa2a668a5 103:9860bb68a0a6
     1 package gondole
     1 package gondole
       
     2 
       
     3 import (
       
     4 	"encoding/json"
       
     5 	"fmt"
       
     6 	"strconv"
       
     7 
       
     8 	"github.com/sendgrid/rest"
       
     9 )
       
    10 
       
    11 // queryStatusData queries the statuses API
       
    12 // The subquery can be empty (the status itself), "context", "card",
       
    13 // "reblogged_by", "favourited_by".
       
    14 func (g *Client) queryStatusData(statusID int, subquery string, data interface{}) error {
       
    15 	endPoint := "statuses/" + strconv.Itoa(statusID)
       
    16 
       
    17 	if statusID < 1 {
       
    18 		return ErrInvalidID
       
    19 	}
       
    20 
       
    21 	if subquery != "" {
       
    22 		// TODO: check subquery values?
       
    23 		endPoint += "/" + subquery
       
    24 	}
       
    25 	req := g.prepareRequest(endPoint)
       
    26 	r, err := rest.API(req)
       
    27 	if err != nil {
       
    28 		return fmt.Errorf("status/%s API query: %s", subquery, err.Error())
       
    29 	}
       
    30 
       
    31 	err = json.Unmarshal([]byte(r.Body), &data)
       
    32 	if err != nil {
       
    33 		var errorRes Error
       
    34 		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
       
    35 		if err2 == nil {
       
    36 			return fmt.Errorf("%s", errorRes.Text)
       
    37 		}
       
    38 		return fmt.Errorf("status/%s API: %s", subquery, err.Error())
       
    39 	}
       
    40 
       
    41 	return nil
       
    42 }
       
    43 
       
    44 // GetStatus returns a status
       
    45 // The returned status can be nil if there is an error or if the
       
    46 // requested ID does not exist.
       
    47 func (g *Client) GetStatus(id int) (*Status, error) {
       
    48 	var status Status
       
    49 
       
    50 	if err := g.queryStatusData(id, "", &status); err != nil {
       
    51 		return nil, err
       
    52 	}
       
    53 
       
    54 	if status.ID == 0 {
       
    55 		return nil, ErrEntityNotFound
       
    56 	}
       
    57 
       
    58 	return &status, nil
       
    59 }
       
    60 
       
    61 // GetStatusContext returns a status context
       
    62 func (g *Client) GetStatusContext(id int) (*Context, error) {
       
    63 	var context Context
       
    64 
       
    65 	if err := g.queryStatusData(id, "context", &context); err != nil {
       
    66 		return nil, err
       
    67 	}
       
    68 
       
    69 	return &context, nil
       
    70 }
       
    71 
       
    72 // GetStatusCard returns a status card
       
    73 func (g *Client) GetStatusCard(id int) (*Card, error) {
       
    74 	var card Card
       
    75 
       
    76 	if err := g.queryStatusData(id, "card", &card); err != nil {
       
    77 		return nil, err
       
    78 	}
       
    79 
       
    80 	return &card, nil
       
    81 }
       
    82 
       
    83 // GetStatusRebloggedBy returns a list of the accounts who reblogged a status
       
    84 func (g *Client) GetStatusRebloggedBy(id int) ([]Account, error) {
       
    85 	var accounts []Account
       
    86 	err := g.queryStatusData(id, "reblogged_by", &accounts)
       
    87 	return accounts, err
       
    88 }
       
    89 
       
    90 // GetStatusFavouritedBy returns a list of the accounts who favourited a status
       
    91 func (g *Client) GetStatusFavouritedBy(id int) ([]Account, error) {
       
    92 	var accounts []Account
       
    93 	err := g.queryStatusData(id, "favourited_by", &accounts)
       
    94 	return accounts, err
       
    95 }