Skip to content

Commit f133f86

Browse files
thincasivchari
authored andcommitted
Escape "query" parameter
"query" parameter may contains any characters such as "&".
1 parent 0743c3c commit f133f86

6 files changed

Lines changed: 15 additions & 16 deletions

File tree

endpoint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const (
66
retrieveSingleTweetURL = "https://api.twitter.com/2/tweets/%v"
77
userTweetTimelineURL = "https://api.twitter.com/2/users/%v/tweets"
88
userMentionTimelineURL = "https://api.twitter.com/2/users/%v/mentions"
9-
searchRecentTweetsURL = "https://api.twitter.com/2/tweets/search/recent?query=%v"
10-
countsRecentTweetsURL = "https://api.twitter.com/2/tweets/counts/recent?query=%v"
9+
searchRecentTweetsURL = "https://api.twitter.com/2/tweets/search/recent"
10+
countsRecentTweetsURL = "https://api.twitter.com/2/tweets/counts/recent"
1111
addOrDeleteRulesURL = "https://api.twitter.com/2/tweets/search/stream/rules"
1212
retrieveStreamRulesURL = "https://api.twitter.com/2/tweets/search/stream/rules"
1313
connectToStreamURL = "https://api.twitter.com/2/tweets/search/stream"
@@ -31,7 +31,7 @@ const (
3131
spacesURL = "https://api.twitter.com/2/spaces?ids="
3232
usersPurchasedSpaceTicketURL = "https://api.twitter.com/2/spaces/%v/buyers"
3333
discoverSpacesURL = "https://api.twitter.com/2/spaces/by/creator_ids?user_ids="
34-
searchSpacesURL = "https://api.twitter.com/2/spaces/search?query=%v"
34+
searchSpacesURL = "https://api.twitter.com/2/spaces/search"
3535
)
3636

3737
const (

search_spaces.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ func searchSpaces(ctx context.Context, c *client, searchTerm string, opt ...*Sea
1212
if searchTerm == "" {
1313
return nil, errors.New("search spaces: searchTerm parameter is required")
1414
}
15-
ep := fmt.Sprintf(searchSpacesURL, searchTerm)
1615

17-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
16+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchSpacesURL, nil)
1817
if err != nil {
1918
return nil, fmt.Errorf("search spaces new request with ctx: %w", err)
2019
}
@@ -29,7 +28,7 @@ func searchSpaces(ctx context.Context, c *client, searchTerm string, opt ...*Sea
2928
default:
3029
return nil, errors.New("search spaces: too many options")
3130
}
32-
sopt.addQuery(req)
31+
sopt.addQuery(req, searchTerm)
3332

3433
resp, err := c.client.Do(req)
3534
if err != nil {

search_tweet.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ func searchRecentTweets(ctx context.Context, c *client, tweet string, opt ...*Se
1616
return nil, errors.New("search recent tweets: tweet parameter must be less than or equal to 512 characters")
1717
}
1818

19-
ep := fmt.Sprintf(searchRecentTweetsURL, tweet)
20-
21-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
19+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchRecentTweetsURL, nil)
2220
if err != nil {
2321
return nil, fmt.Errorf("search recent tweets new request with ctx: %w", err)
2422
}
@@ -44,7 +42,7 @@ func searchRecentTweets(ctx context.Context, c *client, tweet string, opt ...*Se
4442
if sopt.MaxResults < minimumMaxResults || sopt.MaxResults > maximumMaxResults {
4543
return nil, fmt.Errorf("search recent tweets: max results must be between %d and %d", minimumMaxResults, maximumMaxResults)
4644
}
47-
sopt.addQuery(req)
45+
sopt.addQuery(req, tweet)
4846

4947
resp, err := c.client.Do(req)
5048
if err != nil {

space_option.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ type SearchSpacesOption struct {
1313
UserFields []UserField
1414
}
1515

16-
func (s *SearchSpacesOption) addQuery(req *http.Request) {
16+
func (s *SearchSpacesOption) addQuery(req *http.Request, searchTerm string) {
1717
q := req.URL.Query()
18+
q.Add("query", searchTerm)
1819
if len(s.Expansions) > 0 {
1920
q.Add("expansions", strings.Join(expansionsToString(s.Expansions), ","))
2021
}

tweet_counts.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ func countsRecentTweet(ctx context.Context, c *client, tweet string, opt ...*Twe
1212
if tweet == "" {
1313
return nil, errors.New("counts recent tweets: tweet parameter is required")
1414
}
15-
ep := fmt.Sprintf(countsRecentTweetsURL, tweet)
1615

17-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
16+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, countsRecentTweetsURL, nil)
1817
if err != nil {
1918
return nil, fmt.Errorf("counts recent tweets new request with ctx: %w", err)
2019
}
@@ -29,7 +28,7 @@ func countsRecentTweet(ctx context.Context, c *client, tweet string, opt ...*Twe
2928
default:
3029
return nil, errors.New("counts recent tweets: only one option is allowed")
3130
}
32-
topt.addQuery(req)
31+
topt.addQuery(req, tweet)
3332

3433
resp, err := c.client.Do(req)
3534
if err != nil {

tweet_option.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ type SearchTweetsOption struct {
176176
UserFields []UserField
177177
}
178178

179-
func (t SearchTweetsOption) addQuery(req *http.Request) {
179+
func (t SearchTweetsOption) addQuery(req *http.Request, tweet string) {
180180
q := req.URL.Query()
181+
q.Add("query", tweet)
181182
if !t.EndTime.IsZero() {
182183
q.Add("end_time", t.EndTime.Format(time.RFC3339))
183184
}
@@ -227,8 +228,9 @@ type TweetCountsOption struct {
227228
Granularity string
228229
}
229230

230-
func (t *TweetCountsOption) addQuery(req *http.Request) {
231+
func (t *TweetCountsOption) addQuery(req *http.Request, tweet string) {
231232
q := req.URL.Query()
233+
q.Add("query", tweet)
232234
if !t.StartTime.IsZero() {
233235
// YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339).
234236
q.Add("start_time", t.StartTime.Format(time.RFC3339))

0 commit comments

Comments
 (0)