report.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).

/*
Copyright 2017-2018 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
// (I don't know if the limit options are used by the API server.)
func (mc *Client) GetReports(lopt *LimitParams) ([]Report, error) {
	var reports []Report
	if err := mc.apiCall("v1/reports", rest.Get, nil, lopt, nil, &reports); err != nil {
		return nil, err
	}
	return reports, nil
}

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

	params := make(apiCallParams)
	params["account_id"] = strconv.FormatInt(accountID, 10)
	params["comment"] = comment
	for i, id := range statusIDs {
		if id < 1 {
			return nil, ErrInvalidID
		}
		qID := fmt.Sprintf("[%d]status_ids", i)
		params[qID] = strconv.FormatInt(id, 10)
	}

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