File: search.go

package info (click to toggle)
golang-github-mckael-madon 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
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
}