-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlist_lookup.go
110 lines (95 loc) · 2.79 KB
/
list_lookup.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gotwtr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func lookUpList(ctx context.Context, c *client, listID string, opt ...*LookUpListOption) (*ListResponse, error) {
if listID == "" {
return nil, errors.New("look up list: listID parameter is required")
}
ep := fmt.Sprintf(lookUpListURL, listID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("look up list new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var lopt LookUpListOption
switch len(opt) {
case 0:
// do nothing
case 1:
lopt = *opt[0]
default:
return nil, errors.New("look up list: only one option is allowed")
}
lopt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("look up list response: %w", err)
}
defer resp.Body.Close()
var lr ListResponse
if err := json.NewDecoder(resp.Body).Decode(&lr); err != nil {
return nil, fmt.Errorf("look up list decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &lr, &HTTPError{
APIName: "look up list",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &lr, nil
}
func lookUpAllListsOwned(ctx context.Context, c *client, userID string, opt ...*AllListsOwnedOption) (*AllListsOwnedResponse, error) {
if userID == "" {
return nil, errors.New("look up all lists owned: userID parameter is required")
}
ep := fmt.Sprintf(lookUpAllListsOwnedURL, userID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("look up all lists owned new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var aopt AllListsOwnedOption
switch len(opt) {
case 0:
// do nothing
case 1:
aopt = *opt[0]
default:
return nil, errors.New("look up all lists owned: only one option is allowed")
}
const (
minimumMaxResults = 1
maximumMaxResults = 100
defaultMaxResults = 100
)
if aopt.MaxResults == 0 {
aopt.MaxResults = defaultMaxResults
}
if aopt.MaxResults < minimumMaxResults || aopt.MaxResults > maximumMaxResults {
return nil, fmt.Errorf("look up all lists owned: max results must be between %d and %d", minimumMaxResults, maximumMaxResults)
}
aopt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("look up all lists owned response: %w", err)
}
defer resp.Body.Close()
var alor AllListsOwnedResponse
if err := json.NewDecoder(resp.Body).Decode(&alor); err != nil {
return nil, fmt.Errorf("look up all lists owned decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &alor, &HTTPError{
APIName: "look up all lists owned",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &alor, nil
}