https://github.com/mxpv/patreon-go
Patreon Go API client
https://github.com/mxpv/patreon-go
api-client awesome-go awesome-list go golang patreon patreon-api
Last synced: 8 months ago
JSON representation
Patreon Go API client
- Host: GitHub
- URL: https://github.com/mxpv/patreon-go
- Owner: mxpv
- License: mit
- Created: 2017-08-06T21:15:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-09-01T14:27:41.000Z (about 3 years ago)
- Last Synced: 2025-04-05T08:01:41.834Z (8 months ago)
- Topics: api-client, awesome-go, awesome-list, go, golang, patreon, patreon-api
- Language: Go
- Homepage:
- Size: 72.3 KB
- Stars: 44
- Watchers: 4
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - patreon-go - go) [![godoc][D]](https://godoc.org/github.com/mxpv/patreon-go) (第三方api / 实用程序/Miscellaneous)
- awesome-go - patreon-go - Patreon Go API client - ★ 14 (Third-party APIs)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Advanced Console UIs)
- awesome-go-cn - patreon-go
- awesome-go-plus - patreon-go - Go library for Patreon API.  (Third-party APIs / Utility/Miscellaneous)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go - patreon-go - | - | - | (Third-party APIs / HTTP Clients)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go - patreon-go - Go library for Patreon API. (<span id="第三方api-third-party-apis">第三方API Third-party APIs</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go - patreon-go - Go library for Patreon API. - :arrow_down:0 - :star:11 (Third-party APIs / HTTP Clients)
- fucking-awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go-cn - patreon-go - go) (第三方api / 实用程序/Miscellaneous)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / HTTP Clients)
- awesome-go-cn - patreon-go
- awesome-go-zh - patreon-go
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go-extra - patreon-go - 08-06T21:15:14Z|2019-09-17T02:27:28Z| (Third-party APIs / Fail injection)
- awesome-go-with-stars - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-go - patreon-go - Go library for Patreon API. (Third-party APIs / Utility/Miscellaneous)
- awesome-Char - patreon-go - Go library for Patreon API. (Third-party APIs / HTTP Clients)
README
[](https://travis-ci.org/mxpv/patreon-go)
[](https://godoc.org/github.com/mxpv/patreon-go/)
[](https://goreportcard.com/report/github.com/mxpv/patreon-go)
[](https://codecov.io/gh/mxpv/patreon-go)
[](./LICENSE)
[](https://www.patreon.com/podsync)
# patreon-go
`patreon-go` is a Go client library for accessing the [Patreon API](https://docs.patreon.com/#api).
## How to import ##
The `patreon-go` package may be installed by running:
```
go get gopkg.in/mxpv/patreon-go.v1
```
or
```
import "gopkg.in/mxpv/patreon-go.v1"
```
## Basic example ##
```go
import "gopkg.in/mxpv/patreon-go.v1"
func main() {
client := patreon.NewClient(nil)
user, err := client.FetchUser()
if err != nil {
// ...
}
print(user.Data.Id)
}
```
## Authentication ##
The `patreon-go` library does not directly handle authentication. Instead, when creating a new client, pass an `http.Client` that can handle authentication for you, most likely you will need [oauth2](https://github.com/golang/oauth2) package.
Here is an example with static token:
```go
import (
"gopkg.in/mxpv/patreon-go.v1"
"golang.org/x/oauth2"
)
func NewPatreonClient(ctx context.Context, token string) *patreon.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client := patreon.NewClient(tc)
return client
}
```
Automatically refresh token:
```go
func NewPatreonClient() (*patreon.Client, error) {
config := oauth2.Config{
ClientID: "",
ClientSecret: "",
Endpoint: oauth2.Endpoint{
AuthURL: AuthorizationURL,
TokenURL: AccessTokenURL,
},
Scopes: []string{"users", "pledges-to-me", "my-campaign"},
}
token := oauth2.Token{
AccessToken: "",
RefreshToken: "",
// Must be non-nil, otherwise token will not be expired
Expiry: time.Now().Add(-24 * time.Hour),
}
tc := config.Client(context.Background(), &token)
client := NewClient(tc)
_, err := client.FetchUser()
if err != nil {
panic(err)
}
print("OK")
}
```
## Look & Feel ##
```go
func Example_fetchPatronsAndPledges() {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: testAccessToken})
tc := oauth2.NewClient(oauth2.NoContext, ts)
// Create client with static access token
client := NewClient(tc)
// Get your campaign data
campaignResponse, err := client.FetchCampaign()
if err != nil {
panic(err)
}
campaignId := campaignResponse.Data[0].Id
cursor := ""
page := 1
for {
pledgesResponse, err := client.FetchPledges(campaignId,
WithPageSize(25),
WithCursor(cursor))
if err != nil {
panic(err)
}
// Get all the users in an easy-to-lookup way
users := make(map[string]*User)
for _, item := range pledgesResponse.Included.Items {
u, ok := item.(*User)
if !ok {
continue
}
users[u.Id] = u
}
fmt.Printf("Page %d\r\n", page)
// Loop over the pledges to get e.g. their amount and user name
for _, pledge := range pledgesResponse.Data {
amount := pledge.Attributes.AmountCents
patronId := pledge.Relationships.Patron.Data.Id
patronFullName := users[patronId].Attributes.FullName
fmt.Printf("%s is pledging %d cents\r\n", patronFullName, amount)
}
// Get the link to the next page of pledges
nextLink := pledgesResponse.Links.Next
if nextLink == "" {
break
}
cursor = nextLink
page++
}
fmt.Print("Done!")
}
```