report.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 28 Apr 2017 15:43:11 +0200
changeset 149 5f922977d7c7
parent 138 23d3a518d0ad
child 155 0c581e0108da
permissions -rw-r--r--
Add support for limits and paging ({since,max}_id) API parameters

/*
Copyright 2017 Mikael Berthe

Licensed under the MIT license.  Please see the LICENSE file is this directory.
*/

package madon

import (
	"fmt"
	"strconv"

	"github.com/sendgrid/rest"
)

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

// ReportUser reports the user account
func (mc *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 {
		if id < 1 {
			return nil, ErrInvalidID
		}
		qID := fmt.Sprintf("status_ids[%d]", i+1)
		params[qID] = strconv.Itoa(id)
	}

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