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