File: patreon.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (85 lines) | stat: -rw-r--r-- 1,898 bytes parent folder | download | duplicates (2)
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
package patreon

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type PledgesApiResponse struct {
	Pledges []struct {
		Attributes struct {
			AmountCents int `json:"amount_cents"`
		} `json:"attributes"`
		Relationships struct {
			Patron struct {
				Data struct {
					Id Id `json:"id"`
				} `json:"data"`
			} `json:"patron"`
		} `json:"relationships"`
	} `json:"data"`
	Included []ApiUser `json:"included"`
}

type ApiUser struct {
	Attributes struct {
		Email           string `json:"email"`
		IsEmailVerified bool   `json:"is_email_verified"`
	} `json:"attributes"`
	Id Id `json:"id"`
}

type Pledge struct {
	Email         string
	EmailVerified bool
	AmountCents   int
}

type Id string

func makeUserMap(par *PledgesApiResponse) (ret map[Id]*ApiUser) {
	ret = make(map[Id]*ApiUser, len(par.Included))
	for i := range par.Included {
		au := &par.Included[i]
		ret[au.Id] = au
	}
	return
}

func ParsePledgesApiResponse(r io.Reader) (ps []Pledge, err error) {
	var ar PledgesApiResponse
	err = json.NewDecoder(r).Decode(&ar)
	if err != nil {
		return
	}
	userMap := makeUserMap(&ar)
	for _, p := range ar.Pledges {
		u := userMap[p.Relationships.Patron.Data.Id]
		ps = append(ps, Pledge{
			Email:         u.Attributes.Email,
			EmailVerified: u.Attributes.IsEmailVerified,
			AmountCents:   p.Attributes.AmountCents,
		})
	}
	return
}

func GetCampaignPledges(campaign Id, userAccessToken string) (ret []Pledge, err error) {
	req, err := http.NewRequest("GET", fmt.Sprintf("https://api.patreon.com/oauth2/api/campaigns/%s/pledges", campaign), nil)
	if err != nil {
		return
	}
	req.Header.Set("Authorization", "Bearer "+userAccessToken)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		err = fmt.Errorf("got http response code %d", resp.StatusCode)
		return
	}
	return ParsePledgesApiResponse(resp.Body)
}