-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
parameter.go
131 lines (102 loc) · 2.55 KB
/
parameter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package types
import (
"io"
"net/url"
"strings"
"github.com/michimani/gotwi/fields"
"github.com/michimani/gotwi/internal/util"
)
type ListInput struct {
accessToken string
// Query parameters
IDs []string
Expansions fields.ExpansionList
MediaFields fields.MediaFieldList
PlaceFields fields.PlaceFieldList
PollFields fields.PollFieldList
TweetFields fields.TweetFieldList
UserFields fields.UserFieldList
}
var listQueryParameters = map[string]struct{}{
"ids": {},
"expansions": {},
"media.fields": {},
"place.fields": {},
"poll.fields": {},
"tweet.fields": {},
"user.fields": {},
}
func (p *ListInput) SetAccessToken(token string) {
p.accessToken = token
}
func (p *ListInput) AccessToken() string {
return p.accessToken
}
func (p *ListInput) ResolveEndpoint(endpointBase string) string {
endpoint := endpointBase
if p.IDs == nil || len(p.IDs) == 0 {
return ""
}
pm := p.ParameterMap()
if len(pm) > 0 {
qs := util.QueryString(pm, listQueryParameters)
endpoint += "?" + qs
}
return endpoint
}
func (p *ListInput) Body() (io.Reader, error) {
return nil, nil
}
func (p *ListInput) ParameterMap() map[string]string {
m := map[string]string{}
m["ids"] = util.QueryValue(p.IDs)
m = fields.SetFieldsParams(m, p.Expansions, p.MediaFields, p.PlaceFields, p.PollFields, p.TweetFields, p.UserFields)
return m
}
type GetInput struct {
accessToken string
// Path parameter
ID string
// Query parameters
Expansions fields.ExpansionList
MediaFields fields.MediaFieldList
PlaceFields fields.PlaceFieldList
PollFields fields.PollFieldList
TweetFields fields.TweetFieldList
UserFields fields.UserFieldList
}
var getQueryParameters = map[string]struct{}{
"expansions": {},
"media.fields": {},
"place.fields": {},
"poll.fields": {},
"tweet.fields": {},
"user.fields": {},
}
func (p *GetInput) SetAccessToken(token string) {
p.accessToken = token
}
func (p *GetInput) AccessToken() string {
return p.accessToken
}
func (p *GetInput) ResolveEndpoint(endpointBase string) string {
if p.ID == "" {
return ""
}
encoded := url.QueryEscape(p.ID)
endpoint := strings.Replace(endpointBase, ":id", encoded, 1)
pm := p.ParameterMap()
if len(pm) > 0 {
qs := util.QueryString(pm, getQueryParameters)
endpoint += "?" + qs
}
return endpoint
}
func (p *GetInput) Body() (io.Reader, error) {
return nil, nil
}
func (p *GetInput) ParameterMap() map[string]string {
m := map[string]string{}
m = fields.SetFieldsParams(m, p.Expansions, p.MediaFields, p.PlaceFields, p.PollFields, p.TweetFields, p.UserFields)
return m
}