report.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 15 Apr 2017 21:08:34 +0200
changeset 125 2bbb72b9ebf6
parent 120 579912e9d0ef
child 130 c450bb73f59a
permissions -rw-r--r--
Rework the API wrappers to handle arrays of parameters This make some API calls work better (reports with several statuses, statuses with several attachments, relationships for multiple accounts...).

package gondole

import (
	"fmt"
	"strconv"

	"github.com/sendgrid/rest"
)

// GetReports returns the current user's reports
func (g *Client) GetReports() ([]Report, error) {
	var reports []Report
	if err := g.apiCall("reports", rest.Get, nil, &reports); err != nil {
		return nil, err
	}
	return reports, nil
}

// ReportUser reports the user account
func (g *Client) ReportUser(accountID int, statusIDs []int, comment string) (*Report, error) {
	if accountID < 1 || comment == "" || len(statusIDs) < 1 {
		return nil, ErrInvalidParameter
	}

	params := make(apiCallParams)
	params["account_id"] = strconv.Itoa(accountID)
	params["comment"] = comment
	for i, id := range statusIDs {
		qID := fmt.Sprintf("status_ids[%d]", i+1)
		params[qID] = strconv.Itoa(id)
	}

	var report Report
	if err := g.apiCall("reports", rest.Post, params, &report); err != nil {
		return nil, err
	}
	return &report, nil
}