endorsements.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 07 Sep 2018 19:17:12 +0200
changeset 243 7386c6a454a8
parent 238 1c0042e76902
permissions -rw-r--r--
Change the way parameter lists are handled internally Instead of trying to guess if a query key is a list (to strip the index number, since Rails expects list IDs without index number), we prefix the key name with the index when dealing with lists. E.g.: [0]ids: "one" [1]ids: "two" will be sent as ids[]=one&ids[]=two It makes it more reliable and let us differenciate between arrays and objects (objects are untouched and sent as-is).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
235
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
Copyright 2018 Mikael Berthe
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
*/
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
package madon
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"github.com/sendgrid/rest"
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
)
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
// GetEndorsements returns the list of user's endorsements
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
func (mc *Client) GetEndorsements(lopt *LimitParams) ([]Account, error) {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	endPoint := "endorsements"
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	method := rest.Get
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	var accountList []Account
238
1c0042e76902 Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents: 235
diff changeset
    18
	if err := mc.apiCall("v1/"+endPoint, method, nil, lopt, nil, &accountList); err != nil {
235
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
		return nil, err
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	return accountList, nil
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// PinAccount adds the account to the endorsement list
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
func (mc *Client) PinAccount(accountID int64) (*Relationship, error) {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	rel, err := mc.updateRelationship("pin", accountID, nil)
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	if err != nil {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
		return nil, err
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	if rel == nil {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
		return nil, ErrEntityNotFound
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	return rel, nil
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
// UnpinAccount removes the account from the endorsement list
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
func (mc *Client) UnpinAccount(accountID int64) (*Relationship, error) {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	rel, err := mc.updateRelationship("unpin", accountID, nil)
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	if err != nil {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
		return nil, err
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	if rel == nil {
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
		return nil, ErrEntityNotFound
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	}
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	return rel, nil
263da7f71f03 Add endorsement commands (GetEndorsements, {Pin,Unpin}Account)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
}