search.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 17:27:15 +0200
changeset 156 70aadba26338
parent 155 0c581e0108da
child 207 301d5b94be3f
permissions -rw-r--r--
Add field "All" to LimitParams, change Limit behaviour If All is true, the library will send several requests (if needed) until the API server has sent all the results. If not, and if a Limit is set, the library will try to fetch at least this number of results.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     7
package madon
113
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"github.com/sendgrid/rest"
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
)
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
// Search search for contents (accounts or statuses) and returns a Results
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    14
func (mc *Client) Search(query string, resolve bool) (*Results, error) {
113
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	if query == "" {
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
		return nil, ErrInvalidParameter
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	}
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 113
diff changeset
    18
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 113
diff changeset
    19
	params := make(apiCallParams)
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 113
diff changeset
    20
	params["q"] = query
113
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	if resolve {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 113
diff changeset
    22
		params["resolve"] = "true"
113
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	}
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	var results Results
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    26
	if err := mc.apiCall("search", rest.Get, params, nil, nil, &results); err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 113
diff changeset
    27
		return nil, err
113
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	}
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	return &results, nil
bb9aaa5440c1 Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
}