search.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 06 Sep 2018 00:23:40 +0200
changeset 236 5b87cc73ed97
parent 207 301d5b94be3f
child 238 1c0042e76902
permissions -rw-r--r--
Update search result type (us rich tag structure) This change will let us use the same 'Results' object for both versions of the search API endpoint (v1 & v2).

/*
Copyright 2017-2018 Mikael Berthe

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

package madon

import (
	"github.com/sendgrid/rest"
)

// Search search for contents (accounts or statuses) and returns a Results
func (mc *Client) Search(query string, resolve bool) (*Results, error) {
	if query == "" {
		return nil, ErrInvalidParameter
	}

	params := make(apiCallParams)
	params["q"] = query
	if resolve {
		params["resolve"] = "true"
	}

	var resultsV1 struct {
		Results
		Hashtags []string `json:"hashtags"`
	}
	if err := mc.apiCall("search", rest.Get, params, nil, nil, &resultsV1); err != nil {
		return nil, err
	}

	var results Results
	results.Accounts = resultsV1.Accounts
	results.Statuses = resultsV1.Statuses
	for _, t := range resultsV1.Hashtags {
		results.Hashtags = append(results.Hashtags, Tag{Name: t})
	}

	return &results, nil
}