-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsearch_spaces.go
52 lines (45 loc) · 1.2 KB
/
search_spaces.go
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
package gotwtr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func searchSpaces(ctx context.Context, c *client, searchTerm string, opt ...*SearchSpacesOption) (*SearchSpacesResponse, error) {
if searchTerm == "" {
return nil, errors.New("search spaces: searchTerm parameter is required")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchSpacesURL, nil)
if err != nil {
return nil, fmt.Errorf("search spaces new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var sopt SearchSpacesOption
switch len(opt) {
case 0:
// do nothing
case 1:
sopt = *opt[0]
default:
return nil, errors.New("search spaces: too many options")
}
sopt.addQuery(req, searchTerm)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("search spaces: %w", err)
}
defer resp.Body.Close()
var ssr SearchSpacesResponse
if err := json.NewDecoder(resp.Body).Decode(&ssr); err != nil {
return nil, fmt.Errorf("search spaces: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, &HTTPError{
APIName: "search spaces",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &ssr, nil
}