search.go
author Mikael Berthe <mikael@lilotux.net>
Fri, 07 Sep 2018 19:17:12 +0200
changeset 243 7386c6a454a8
parent 240 80c81e9b77b4
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 (
	"strings"

	"github.com/sendgrid/rest"
)

// Search search for contents (accounts or statuses) and returns a Results
func (mc *Client) searchV1(params apiCallParams) (*Results, error) {
	// We use a custom structure with shadowed Hashtags field,
	// since the v1 version only returns strings.
	var resultsV1 struct {
		Results
		Hashtags []string `json:"hashtags"`
	}
	if err := mc.apiCall("v1/"+"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
}

func (mc *Client) searchV2(params apiCallParams) (*Results, error) {
	var results Results
	if err := mc.apiCall("v2/"+"search", rest.Get, params, nil, nil, &results); err != nil {
		return nil, err
	}

	return &results, nil
}

// 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
	}

	// The parameters are the same in both v1 & v2 API versions
	params := make(apiCallParams)
	params["q"] = query
	if resolve {
		params["resolve"] = "true"
	}

	r, err := mc.searchV2(params)

	// This is not a very beautiful way to check the error cause, I admit.
	if err != nil && strings.Contains(err.Error(), "bad server status code (404)") {
		// Fall back to v1 API endpoint
		r, err = mc.searchV1(params)
	}

	return r, err
}