Skip to content

Commit

Permalink
Feature/issue 70 list follows api (#103)
Browse files Browse the repository at this point in the history
* add user follow list manage

* get followed lists

* get list folloers

* typo

* added unit tests

* added examples

* updated readme

* added the comments
  • Loading branch information
g8rswimmer authored Feb 12, 2022
1 parent d5d4d6a commit 3f09833
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 2 deletions.
5 changes: 5 additions & 0 deletions v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Here are the current twitter `v2` API features supported:
* [user pin list](./examples/user-pin-list)
* [user unpin list](./examples/user-unpin-list)
* [user pinned lists](./examples/user-pinned-lists)
* [List Follows](https://developer.twitter.com/en/docs/twitter-api/lists/list-follows/introduction)
* [user follow list](./examples/user-follow-list)
* [user unfollow list](./examples/user-unfollow-list)
* [user followed lists](./examples/user-followed-lists)
* [list followers](./examples/list-followers)

## Examples
Much like `v1`, there is an `_example` directory to demostrate library usage. Refer to the [readme](./_examples) for more information.
Expand Down
28 changes: 28 additions & 0 deletions v2/_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,31 @@ This [example](./user-pinned-lists) demonstrates user pinned lists API.
```
go run *.go -token=YOUR_API_TOKEN -user_id='USER_ID'
```

## User Follow List
This [example](./user-follow-list) demonstrates user follow list API.

```
go run *.go -token=YOUR_API_TOKEN -user_id='USER_ID' -list_id='LIST_ID'
```

## User Unfollow List
This [example](./user-unfollow-list) demonstrates user unfollow list API.

```
go run *.go -token=YOUR_API_TOKEN -user_id='USER_ID' -list_id='LIST_ID'
```

## User Followed Lists
This [example](./user-followed-lists) demonstrates user followed lists API.

```
go run *.go -token=YOUR_API_TOKEN -user_id='USER_ID'
```

## List Followers
This [example](./list-followers) demonstrates list followers API.

```
go run *.go -token=YOUR_API_TOKEN -list_id='LIST_ID'
```
55 changes: 55 additions & 0 deletions v2/_examples/list-followers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"

twitter "github.com/g8rswimmer/go-twitter/v2"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of tweet ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
listID := flag.String("list_id", "", "list id")
flag.Parse()

client := &twitter.Client{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}

opts := twitter.ListUserFollowersOpts{
Expansions: []twitter.Expansion{twitter.ExpansionPinnedTweetID},
MaxResults: 5,
}

fmt.Println("Callout to list members callout")

listResponse, err := client.ListUserFollowers(context.Background(), *listID, opts)
if err != nil {
log.Panicf("list members error: %v", err)
}

enc, err := json.MarshalIndent(listResponse, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(enc))
}
51 changes: 51 additions & 0 deletions v2/_examples/user-follow-list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"

twitter "github.com/g8rswimmer/go-twitter/v2"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of tweet ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
userID := flag.String("user_id", "", "user id")
listID := flag.String("list_id", "", "list id")
flag.Parse()

client := &twitter.Client{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}

fmt.Println("Callout to user follow list callout")

listResponse, err := client.UserFollowList(context.Background(), *userID, *listID)
if err != nil {
log.Panicf(" user follow list error: %v", err)
}

enc, err := json.MarshalIndent(listResponse, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(enc))
}
55 changes: 55 additions & 0 deletions v2/_examples/user-followed-lists/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"

twitter "github.com/g8rswimmer/go-twitter/v2"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of tweet ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
userID := flag.String("user_id", "", "user id")
flag.Parse()

client := &twitter.Client{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}
opts := twitter.UserFollowedListsOpts{
Expansions: []twitter.Expansion{twitter.ExpansionOwnerID},
ListFields: []twitter.ListField{twitter.ListFieldFollowerCount},
UserFields: []twitter.UserField{twitter.UserFieldCreatedAt},
}

fmt.Println("Callout to user followed list lookup callout")

listResponse, err := client.UserFollowedLists(context.Background(), *userID, opts)
if err != nil {
log.Panicf(" user followed list lookup error: %v", err)
}

enc, err := json.MarshalIndent(listResponse, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(enc))
}
51 changes: 51 additions & 0 deletions v2/_examples/user-unfollow-list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"

twitter "github.com/g8rswimmer/go-twitter/v2"
)

type authorize struct {
Token string
}

func (a authorize) Add(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token))
}

/**
In order to run, the user will need to provide the bearer token and the list of tweet ids.
**/
func main() {
token := flag.String("token", "", "twitter API token")
userID := flag.String("user_id", "", "user id")
listID := flag.String("list_id", "", "list id")
flag.Parse()

client := &twitter.Client{
Authorizer: authorize{
Token: *token,
},
Client: http.DefaultClient,
Host: "https://api.twitter.com",
}

fmt.Println("Callout to user unfollow list callout")

listResponse, err := client.UserUnfollowList(context.Background(), *userID, *listID)
if err != nil {
log.Panicf(" user unfollow list error: %v", err)
}

enc, err := json.MarshalIndent(listResponse, "", " ")
if err != nil {
log.Panic(err)
}
fmt.Println(string(enc))
}
Loading

0 comments on commit 3f09833

Please sign in to comment.