-
Notifications
You must be signed in to change notification settings - Fork 14
/
discover_spaces.go
66 lines (57 loc) · 1.48 KB
/
discover_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gotwtr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func discoverSpaces(ctx context.Context, c *client, userIDs []string, opt ...*DiscoverSpacesOption) (*DiscoverSpacesResponse, error) {
switch {
case len(userIDs) == 0:
return nil, errors.New("discover spaces: ids parameter is required")
case len(userIDs) > discoverSpacesMaxIDs:
return nil, errors.New("discover spaces: ids parameter must be less than or equal to 100")
default:
}
ep := discoverSpacesURL
for i, uid := range userIDs {
if i+1 < len(userIDs) {
ep += fmt.Sprintf("%s,", uid)
} else {
ep += uid
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("discover spaces: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var dopt DiscoverSpacesOption
switch len(opt) {
case 0:
// do nothing
case 1:
dopt = *opt[0]
default:
return nil, errors.New("discover spaces: only one option is allowed")
}
dopt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("discover spaces: %w", err)
}
defer resp.Body.Close()
var dsr DiscoverSpacesResponse
if err := json.NewDecoder(resp.Body).Decode(&dsr); err != nil {
return nil, fmt.Errorf("discover spaces: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &dsr, &HTTPError{
APIName: "discover spaces",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &dsr, nil
}