Skip to content

Commit

Permalink
preparing for v2 (g8rswimmer#136)
Browse files Browse the repository at this point in the history
* updated readmes

* updated v2 readme

* added max results check for user tweet timeline

* added max results check for user mention timeline

* added content type for the tweet hide replies

* updated comment for the delete user retweet

* updated the list meta data with optional parameters

* added the optional data from the update

* took at the header for content type
  • Loading branch information
g8rswimmer authored Apr 3, 2022
1 parent bc067b1 commit f8a6937
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 73 deletions.
45 changes: 2 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,15 @@ The library is based off of [version 2](https://developer.twitter.com/en/docs/tw

## Version 2

Currently, version 2 is in development. Please refer to [here](./v2/README.md) for more information. Please note, that version 1 is still going to be maintained for some time after version 2 is released.
Version 2 has been released. This is the current version that will be updated and maintained going forward. Please refer to the readme [here](./v2)

```
go get -u github.com/g8rswimmer/go-twitter/v2
```

## Version 1
## Version 1 [deprecated]

```
go get -u github.com/g8rswimmer/go-twitter
```

### Examples
To run all examples, the user is required to provide their developer account credentials. The library does not share any credentials.

#### Tweet
The following examples demostrate the tweet APIs.

##### Lookup
The tweet lookup API example is located [here](./_examples/tweet/lookup).

##### Recent Search
The tweet recent search API example is locate [here](./_examples/tweet/recent-search).

##### Filtered Search
The tweet filtered search API example is locate [here](./_examples/tweet/filtered-search).

##### Sampled Search
The tweet sampled search API example is locate [here](./_examples/tweet/sampled-search).

##### Hide Replies
The tweet hide replies API example is locate [here](./_examples/tweet/hide).

#### User
The following example demostrate the user APIs

##### Lookup by ID
The user lookup API example is located [here](./_examples/user/lookup-id)

##### Lookup by User Name
The user lookup API example is located [here](./_examples/user/lookup-name)

##### Following by User Id
The user following API example is located [here](./_examples/user/following)

##### Followers by User Id
The user followers API example is located [here](./_examples/user/followers)

##### Tweet Timeline by User Id
The user tweet timeline API example is located [here](./_examples/user/tweets)

##### Mention Timeline by User Id
The user tweet timeline API example is located [here](./_examples/user/mentions)
41 changes: 41 additions & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Examples
To run all examples, the user is required to provide their developer account credentials. The library does not share any credentials.

### Tweet
The following examples demonstrate the tweet APIs.

#### Lookup
The tweet lookup API example is located [here](./tweet/lookup/main.go).

#### Recent Search
The tweet recent search API example is locate [here](./tweet/recent-search/main.go).

#### Filtered Search
The tweet filtered search API example is locate [here](./tweet/filtered-search/main.go).

#### Sampled Search
The tweet sampled search API example is locate [here](./tweet/sampled-search/main.go).

#### Hide Replies
The tweet hide replies API example is locate [here](./tweet/hide/main.go).

### User
The following example demostrate the user APIs

#### Lookup by ID
The user lookup API example is located [here](./user/lookup-id/main.go)

#### Lookup by User Name
The user lookup API example is located [here](./user/lookup-name/main.go)

#### Following by User Id
The user following API example is located [here](./user/following/main.go)

#### Followers by User Id
The user followers API example is located [here](./user/followers/main.go)

#### Tweet Timeline by User Id
The user tweet timeline API example is located [here](./user/tweets/main.go)

#### Mention Timeline by User Id
The user tweet timeline API example is located [here](./user/mentions/main.go)
2 changes: 1 addition & 1 deletion v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ The following APIs are supported, with the examples [here](./_examples/complianc
* [Compliance Batch](https://developer.twitter.com/en/docs/twitter-api/compliance/batch-compliance/introduction)

## Rate Limiting
With each response, the rate limits from the response header is returned. This allows the caller to manage any limits that are imposed. Along with the response, errors that are returned may have rate limits as well. If the error occurs after the request is sent, then rate limits may apply and are returned.
With each response, the rate limits from the response header are returned. This allows the caller to manage any limits that are imposed. Along with the response, errors that are returned may have rate limits as well. If the error occurs after the request is sent, then rate limits may apply and are returned.

There is an example of rate limiting from a response [here](./_examples/misc/rate-limit/main.go).

Expand Down
72 changes: 46 additions & 26 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,42 @@ import (
)

const (
tweetMaxIDs = 100
userMaxIDs = 100
spaceMaxIDs = 100
spaceByCreatorMaxIDs = 100
userMaxNames = 100
tweetRecentSearchQueryLength = 512
tweetSearchQueryLength = 1024
tweetRecentCountsQueryLength = 512
tweetAllCountsQueryLength = 1024
userBlocksMaxResults = 1000
userMutesMaxResults = 1000
likesMaxResults = 100
likesMinResults = 10
sampleStreamMaxBackoffMin = 5
userListMaxResults = 100
listTweetMaxResults = 100
userListMembershipMaxResults = 100
listUserMemberMaxResults = 100
userListFollowedMaxResults = 100
listuserFollowersMaxResults = 100
quoteTweetMaxResults = 100
quoteTweetMinResults = 10
tweetBookmarksMaxResults = 100
tweetMaxIDs = 100
userMaxIDs = 100
spaceMaxIDs = 100
spaceByCreatorMaxIDs = 100
userMaxNames = 100
tweetRecentSearchQueryLength = 512
tweetSearchQueryLength = 1024
tweetRecentCountsQueryLength = 512
tweetAllCountsQueryLength = 1024
userBlocksMaxResults = 1000
userMutesMaxResults = 1000
likesMaxResults = 100
likesMinResults = 10
sampleStreamMaxBackoffMin = 5
userListMaxResults = 100
listTweetMaxResults = 100
userListMembershipMaxResults = 100
listUserMemberMaxResults = 100
userListFollowedMaxResults = 100
listuserFollowersMaxResults = 100
quoteTweetMaxResults = 100
quoteTweetMinResults = 10
tweetBookmarksMaxResults = 100
userTweetTimelineMinResults = 5
userTweetTimelineMaxResults = 100
userMentionTimelineMinResults = 5
userMentionTimelineMaxResults = 100
)

// Client is used to make twitter v2 API callouts.
//
// Authorizer is used to add auth to the request
//
// Client is the HTTP client to use for all requests
//
// Host is the base URL to use like, https://api.twitter.com
type Client struct {
Authorizer Authorizer
Client *http.Client
Expand Down Expand Up @@ -103,7 +113,7 @@ func (c *Client) CreateTweet(ctx context.Context, tweet CreateTweetRequest) (*Cr
// DeleteTweet allow the user to delete a specific tweet
func (c *Client) DeleteTweet(ctx context.Context, id string) (*DeleteTweetResponse, error) {
if len(id) == 0 {
return nil, fmt.Errorf("delete tweet id is required")
return nil, fmt.Errorf("delete tweet id is required %w", ErrParameter)
}
ep := tweetDeleteEndpoint.urlID(c.Host, id)

Expand Down Expand Up @@ -1349,6 +1359,11 @@ func (c *Client) UserTweetTimeline(ctx context.Context, userID string, opts User
switch {
case len(userID) == 0:
return nil, fmt.Errorf("user tweet timeline: a query is required: %w", ErrParameter)
case opts.MaxResults == 0:
case opts.MaxResults < userTweetTimelineMinResults:
return nil, fmt.Errorf("user tweet timeline: max results [%d] have a min[%d] %w", opts.MaxResults, userTweetTimelineMinResults, ErrParameter)
case opts.MaxResults > userTweetTimelineMaxResults:
return nil, fmt.Errorf("user tweet timeline: max results [%d] have a max[%d] %w", opts.MaxResults, userTweetTimelineMaxResults, ErrParameter)
default:
}

Expand Down Expand Up @@ -1416,6 +1431,11 @@ func (c *Client) UserMentionTimeline(ctx context.Context, userID string, opts Us
switch {
case len(userID) == 0:
return nil, fmt.Errorf("user mention timeline: a query is required: %w", ErrParameter)
case opts.MaxResults == 0:
case opts.MaxResults < userMentionTimelineMinResults:
return nil, fmt.Errorf("user mention timeline: max results [%d] have a min[%d] %w", opts.MaxResults, userMentionTimelineMinResults, ErrParameter)
case opts.MaxResults > userMentionTimelineMaxResults:
return nil, fmt.Errorf("user mention timeline: max results [%d] have a max[%d] %w", opts.MaxResults, userMentionTimelineMaxResults, ErrParameter)
default:
}

Expand Down Expand Up @@ -1498,6 +1518,7 @@ func (c Client) TweetHideReplies(ctx context.Context, id string, hide bool) (*Tw
return nil, fmt.Errorf("tweet hide replies request: %w", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
c.Authorizer.Add(req)

resp, err := c.Client.Do(req)
Expand Down Expand Up @@ -1601,7 +1622,7 @@ func (c *Client) UserRetweet(ctx context.Context, userID, tweetID string) (*User
return raw, nil
}

// DeleteUserRetweet will return a retweet from a user
// DeleteUserRetweet will delete a retweet from a user
func (c *Client) DeleteUserRetweet(ctx context.Context, userID, tweetID string) (*DeleteUserRetweetResponse, error) {
switch {
case len(userID) == 0:
Expand Down Expand Up @@ -2661,7 +2682,6 @@ func (c *Client) DeleteList(ctx context.Context, listID string) (*ListDeleteResp
if err != nil {
return nil, fmt.Errorf("delete list request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
c.Authorizer.Add(req)

Expand Down
6 changes: 3 additions & 3 deletions v2/list_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package twitter

// ListMetaData is a list meta data
type ListMetaData struct {
Name *string `json:"name"`
Description *string `json:"description"`
Private *bool `json:"private"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Private *bool `json:"private,omitempty"`
}

// ListCreateResponse is the response to creating a list
Expand Down

0 comments on commit f8a6937

Please sign in to comment.