hocki1 commited on
Commit
2c8c8ba
·
1 Parent(s): 8feee09

first_commit

Browse files
Files changed (26) hide show
  1. .gitignore +5 -0
  2. README.md +70 -0
  3. auth.png +3 -0
  4. auth/auth.go +72 -0
  5. auth/options.go +41 -0
  6. blog.go +75 -0
  7. boosty.go +30 -0
  8. boosty_test.go +19 -0
  9. call.go +37 -0
  10. cmd/boosty/main.go +60 -0
  11. current.go +29 -0
  12. current_test.go +79 -0
  13. device.png +3 -0
  14. go.mod +11 -0
  15. go.sum +10 -0
  16. options.go +13 -0
  17. request/options.go +33 -0
  18. request/request.go +134 -0
  19. stats.go +51 -0
  20. stats_test.go +779 -0
  21. subscribers.go +60 -0
  22. subscribers_test.go +172 -0
  23. subscriptions.go +64 -0
  24. subscriptions_test.go +207 -0
  25. targets.go +37 -0
  26. targets_test.go +92 -0
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .idea/*
2
+ *.iml
3
+ .boosty
4
+ http/*
5
+ bin/*
README.md ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # boosty
2
+
3
+ Библиотека для работы с приватным API boosty
4
+
5
+ ## Использование
6
+
7
+ Установка чуть нестандартная. Нужно использовать отдельный домен для go get:
8
+
9
+ ```shell
10
+ go get gohome.4gophers.ru/getapp/boosty
11
+ ```
12
+
13
+ Пакет будет устанавливаться из оригинального репозитория https://gitflic.ru/project/getapp/boosty
14
+
15
+ Для инициализации необходимо указать блог и токен. Токен можно забрать из браузера
16
+
17
+ ```golang
18
+ auth, err := auth.New(
19
+ auth.WithFile(".boosty"),
20
+ // auth.WithInfo(auth.Info{}),
21
+ auth.WithInfoUpdateCallback(func (i auth.Info) {
22
+ log.Printf("info update: %+v\n", i)
23
+ }),
24
+ )
25
+ if err != nil {
26
+ log.Fatal(err)
27
+ }
28
+
29
+ request, err := request.New(
30
+ //request.WithUrl("https://api.boosty.to"),
31
+ request.WithClient(&http.Client{}),
32
+ request.WithAuth(auth),
33
+ )
34
+ if err != nil {
35
+ log.Fatal(err)
36
+ }
37
+
38
+ b, err := boosty.New("getapp", boosty.WithRequest(request))
39
+ if err != nil {
40
+ log.Fatal(err)
41
+ }
42
+ ```
43
+
44
+ ## Откуда брать авторизацию
45
+
46
+ Данные авторизации нужно забрать из cookies
47
+
48
+ ![auth.png](auth.png)
49
+
50
+ Эти данные нужно перенести в JSON в файл .boosty - этот файл используется по умолчанию
51
+
52
+ ```json
53
+ {
54
+ "accessToken":"xxxxxxxxxxxxxxx",
55
+ "refreshToken":"xxxxxxxxxxxxxxx",
56
+ "expiresAt":1710966525,
57
+ "deviceId":"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
58
+ }
59
+ ```
60
+
61
+ `deviceId` - этот параметр нужно получить отдельно из cookie:
62
+
63
+ ![device.png](device.png)
64
+
65
+ Если данные авторизации протухнут, то библиотека сама попробует обновить
66
+ авторизационные данные и сохранить из в файле .boosty
67
+
68
+ ## Обновления
69
+
70
+ Канал с новостями [@kodikapusta](https://t.me/kodikapusta)
auth.png ADDED

Git LFS Details

  • SHA256: b4ab8a106330468e2ed59abe615adda13b3bd673eaf23f7ac312045430415935
  • Pointer size: 131 Bytes
  • Size of remote file: 446 kB
auth/auth.go ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package auth
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "os"
7
+ )
8
+
9
+ type Callback func(info Info)
10
+
11
+ type Info struct {
12
+ AccessToken string `json:"accessToken"`
13
+ RefreshToken string `json:"refreshToken"`
14
+ ExpiresAt int64 `json:"expiresAt"`
15
+ DeviceId string `json:"deviceId"`
16
+ }
17
+
18
+ type Auth struct {
19
+ file string
20
+ info Info
21
+ callback Callback
22
+ }
23
+
24
+ func New(options ...Option) (*Auth, error) {
25
+ auth := &Auth{
26
+ file: "",
27
+ info: Info{},
28
+ }
29
+
30
+ for _, o := range options {
31
+ if err := o(auth); err != nil {
32
+ return nil, err
33
+ }
34
+ }
35
+
36
+ return auth, nil
37
+ }
38
+
39
+ func (a *Auth) Info() Info {
40
+ return a.info
41
+ }
42
+
43
+ func (a *Auth) Update(info Info) {
44
+ a.info = info
45
+ if a.callback != nil {
46
+ a.callback(info)
47
+ }
48
+ }
49
+
50
+ func (a *Auth) BearerHeader() string {
51
+ return "Bearer " + a.info.AccessToken
52
+ }
53
+
54
+ func (a *Auth) RefreshToken() string {
55
+ return a.info.RefreshToken
56
+ }
57
+
58
+ func (a *Auth) DeviceId() string {
59
+ return a.info.DeviceId
60
+ }
61
+
62
+ func (a *Auth) Save() error {
63
+ if a.file == "" {
64
+ return nil
65
+ }
66
+
67
+ data, err := json.Marshal(a.info)
68
+ if err != nil {
69
+ return fmt.Errorf("error on marshal auth data: %w", err)
70
+ }
71
+ return os.WriteFile(a.file, data, 0644)
72
+ }
auth/options.go ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package auth
2
+
3
+ import (
4
+ "encoding/json"
5
+ "os"
6
+ )
7
+
8
+ type Option func(a *Auth) error
9
+
10
+ func WithFile(file string) Option {
11
+ return func(a *Auth) error {
12
+ data, err := os.ReadFile(file)
13
+ if err != nil {
14
+ return err
15
+ }
16
+
17
+ if err = json.Unmarshal(data, &(a.info)); err != nil {
18
+ return err
19
+ }
20
+
21
+ a.file = file
22
+
23
+ return nil
24
+ }
25
+ }
26
+
27
+ func WithInfo(info Info) Option {
28
+ return func(a *Auth) error {
29
+ a.info = info
30
+
31
+ return nil
32
+ }
33
+ }
34
+
35
+ func WithInfoUpdateCallback(callback Callback) Option {
36
+ return func(a *Auth) error {
37
+ a.callback = callback
38
+
39
+ return nil
40
+ }
41
+ }
blog.go ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Blog struct {
10
+ Owner struct {
11
+ ID int `json:"id"`
12
+ HasAvatar bool `json:"hasAvatar"`
13
+ Name string `json:"name"`
14
+ AvatarURL string `json:"avatarUrl"`
15
+ } `json:"owner"`
16
+ Title string `json:"title"`
17
+ IsReadOnly bool `json:"isReadOnly"`
18
+ Flags struct {
19
+ ShowPostDonations bool `json:"showPostDonations"`
20
+ AllowGoogleIndex bool `json:"allowGoogleIndex"`
21
+ HasTargets bool `json:"hasTargets"`
22
+ AcceptDonationMessages bool `json:"acceptDonationMessages"`
23
+ AllowIndex bool `json:"allowIndex"`
24
+ IsRssFeedEnabled bool `json:"isRssFeedEnabled"`
25
+ HasSubscriptionLevels bool `json:"hasSubscriptionLevels"`
26
+ } `json:"flags"`
27
+ SignedQuery string `json:"signedQuery"`
28
+ IsBlackListedByUser bool `json:"isBlackListedByUser"`
29
+ IsSubscribed bool `json:"isSubscribed"`
30
+ Subscription interface{} `json:"subscription"`
31
+ IsTotalBaned bool `json:"isTotalBaned"`
32
+ AccessRights struct {
33
+ CanSetPayout bool `json:"canSetPayout"`
34
+ CanCreateComments bool `json:"canCreateComments"`
35
+ CanEdit bool `json:"canEdit"`
36
+ CanView bool `json:"canView"`
37
+ CanDeleteComments bool `json:"canDeleteComments"`
38
+ CanCreate bool `json:"canCreate"`
39
+ } `json:"accessRights"`
40
+ Count struct {
41
+ Subscribers int `json:"subscribers"`
42
+ Posts int `json:"posts"`
43
+ } `json:"count"`
44
+ BlogURL string `json:"blogUrl"`
45
+ IsOwner bool `json:"isOwner"`
46
+ PublicWebSocketChannel string `json:"publicWebSocketChannel"`
47
+ SubscriptionKind string `json:"subscriptionKind"`
48
+ IsBlackListed bool `json:"isBlackListed"`
49
+ AllowedPromoTypes []string `json:"allowedPromoTypes"`
50
+ Description []struct {
51
+ Type string `json:"type"`
52
+ Content string `json:"content"`
53
+ Explicit bool `json:"explicit,omitempty"`
54
+ URL string `json:"url,omitempty"`
55
+ Modificator string `json:"modificator,omitempty"`
56
+ } `json:"description"`
57
+ SocialLinks []struct {
58
+ URL string `json:"url"`
59
+ Type string `json:"type"`
60
+ } `json:"socialLinks"`
61
+ HasAdultContent bool `json:"hasAdultContent"`
62
+ CoverURL string `json:"coverUrl"`
63
+ }
64
+
65
+ func (b *Boosty) Blog() (*Blog, error) {
66
+ u := fmt.Sprintf("/v1/blog/%s", b.blog)
67
+ m := Method[Blog]{
68
+ request: b.request,
69
+ method: http.MethodGet,
70
+ url: u,
71
+ values: url.Values{},
72
+ }
73
+
74
+ return m.Call(Blog{})
75
+ }
boosty.go ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "gohome.4gophers.ru/getapp/boosty/request"
5
+ )
6
+
7
+ type Boosty struct {
8
+ blog string
9
+ request *request.Request
10
+ }
11
+
12
+ func New(blog string, options ...Option) (*Boosty, error) {
13
+ request, err := request.New()
14
+ if err != nil {
15
+ return nil, err
16
+ }
17
+
18
+ b := &Boosty{
19
+ blog: blog,
20
+ request: request,
21
+ }
22
+
23
+ for _, o := range options {
24
+ if err := o(b); err != nil {
25
+ return nil, err
26
+ }
27
+ }
28
+
29
+ return b, nil
30
+ }
boosty_test.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/stretchr/testify/suite"
7
+ )
8
+
9
+ type BoostyTestSuite struct {
10
+ suite.Suite
11
+ }
12
+
13
+ func (s *BoostyTestSuite) SetupTest() {
14
+ //
15
+ }
16
+
17
+ func TestBoostyTestSuite(t *testing.T) {
18
+ suite.Run(t, new(BoostyTestSuite))
19
+ }
call.go ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "net/url"
7
+
8
+ "gohome.4gophers.ru/getapp/boosty/request"
9
+ )
10
+
11
+ type Method[T interface{}] struct {
12
+ request *request.Request
13
+ method string
14
+ url string
15
+ values url.Values
16
+ }
17
+
18
+ func (m *Method[T]) Call(model T) (*T, error) {
19
+ u := m.url + m.values.Encode()
20
+
21
+ resp, err := m.request.Request(m.method, u, nil)
22
+ if err != nil {
23
+ return nil, fmt.Errorf("error on do request: %w", err)
24
+ }
25
+
26
+ defer resp.Body.Close()
27
+
28
+ if resp.StatusCode >= 400 {
29
+ return nil, fmt.Errorf("boosty request status error")
30
+ }
31
+
32
+ if err := json.NewDecoder(resp.Body).Decode(&model); err != nil {
33
+ return nil, fmt.Errorf("boosty request decode error: %w", err)
34
+ }
35
+
36
+ return &model, nil
37
+ }
cmd/boosty/main.go ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "log"
6
+ "net/http"
7
+ "net/url"
8
+
9
+ "gohome.4gophers.ru/getapp/boosty"
10
+ "gohome.4gophers.ru/getapp/boosty/auth"
11
+ "gohome.4gophers.ru/getapp/boosty/request"
12
+ )
13
+
14
+ func main() {
15
+ auth, err := auth.New(
16
+ auth.WithFile(".boosty"),
17
+ //auth.WithInfo(auth.Info{}),
18
+ auth.WithInfoUpdateCallback(func(i auth.Info) {
19
+ log.Printf("info update: %+v\n", i)
20
+ }),
21
+ )
22
+
23
+ if err != nil {
24
+ log.Fatal(err)
25
+ }
26
+
27
+ request, err := request.New(
28
+ //request.WithUrl("https://api.boosty.to"),
29
+ request.WithClient(&http.Client{}),
30
+ request.WithAuth(auth),
31
+ )
32
+ if err != nil {
33
+ log.Fatal(err)
34
+ }
35
+
36
+ b, err := boosty.New("getapp", boosty.WithRequest(request))
37
+ if err != nil {
38
+ log.Fatal(err)
39
+ }
40
+
41
+ s, err := b.Current()
42
+ if err != nil {
43
+ log.Fatal(err)
44
+ }
45
+
46
+ fmt.Printf("current: %+v\n\n", s)
47
+
48
+ v := url.Values{}
49
+ v.Add("offset", "0")
50
+ v.Add("limit", "100")
51
+
52
+ ss, err := b.Subscribers(v)
53
+ if err != nil {
54
+ log.Fatal(err)
55
+ }
56
+
57
+ for _, s := range ss.Data {
58
+ fmt.Printf("subscriber: %+v\n\n", s)
59
+ }
60
+ }
current.go ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Current struct {
10
+ PaidCount int `json:"paidCount"`
11
+ FollowersCount int `json:"followersCount"`
12
+ Hold int `json:"hold"`
13
+ Income int `json:"income"`
14
+ Balance int `json:"balance"`
15
+ PayoutSum int `json:"payoutSum"`
16
+ }
17
+
18
+ func (b *Boosty) Current() (*Current, error) {
19
+ u := fmt.Sprintf("/v1/blog/stat/%s/current", b.blog)
20
+
21
+ m := Method[Current]{
22
+ request: b.request,
23
+ method: http.MethodGet,
24
+ url: u,
25
+ values: url.Values{},
26
+ }
27
+
28
+ return m.Call(Current{})
29
+ }
current_test.go ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "testing"
8
+
9
+ "github.com/stretchr/testify/suite"
10
+ "gohome.4gophers.ru/getapp/boosty/auth"
11
+ "gohome.4gophers.ru/getapp/boosty/request"
12
+ )
13
+
14
+ type CurrentTestSuite struct {
15
+ suite.Suite
16
+ }
17
+
18
+ func (s *CurrentTestSuite) SetupTest() {
19
+ //
20
+ }
21
+
22
+ func (s *BoostyTestSuite) TestStats() {
23
+ tests := map[string]struct {
24
+ followersCount int
25
+ paidCount int
26
+ body string
27
+ token string
28
+ }{
29
+ "success stats": {
30
+ followersCount: 0, paidCount: 0, body: currentBody, token: "123",
31
+ },
32
+ }
33
+
34
+ for name, test := range tests {
35
+ s.T().Run(name, func(t *testing.T) {
36
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37
+ auth := r.Header.Get("Authorization")
38
+
39
+ s.Assert().Equal(auth, "Bearer "+test.token)
40
+
41
+ fmt.Fprintf(w, test.body)
42
+ }))
43
+ defer svr.Close()
44
+
45
+ auth, err := auth.New(auth.WithInfo(auth.Info{
46
+ AccessToken: test.token,
47
+ }))
48
+ s.Assert().NoError(err)
49
+
50
+ req, err := request.New(
51
+ request.WithUrl(svr.URL),
52
+ request.WithAuth(auth),
53
+ request.WithClient(&http.Client{}),
54
+ )
55
+ s.Assert().NoError(err)
56
+
57
+ b, err := New("", WithRequest(req))
58
+ s.Assert().NoError(err)
59
+
60
+ stats, err := b.Current()
61
+
62
+ s.Assert().NoError(err)
63
+ s.Assert().Equal(test.followersCount, stats.FollowersCount)
64
+ s.Assert().Equal(test.paidCount, stats.FollowersCount)
65
+
66
+ })
67
+ }
68
+ }
69
+
70
+ const currentBody = `
71
+ {
72
+ "followersCount": 0,
73
+ "income": 6210,
74
+ "balance": 0,
75
+ "payoutSum": 6210,
76
+ "paidCount": 2,
77
+ "hold": 0
78
+ }
79
+ `
device.png ADDED

Git LFS Details

  • SHA256: 7571639159113512d020b3fcaf63e62baa8d7a2ed63ee3e34d09a29e417ac244
  • Pointer size: 131 Bytes
  • Size of remote file: 306 kB
go.mod ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module gohome.4gophers.ru/getapp/boosty
2
+
3
+ go 1.21
4
+
5
+ require github.com/stretchr/testify v1.8.4
6
+
7
+ require (
8
+ github.com/davecgh/go-spew v1.1.1 // indirect
9
+ github.com/pmezard/go-difflib v1.0.0 // indirect
10
+ gopkg.in/yaml.v3 v3.0.1 // indirect
11
+ )
go.sum ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2
+ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3
+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4
+ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5
+ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6
+ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7
+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8
+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9
+ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10
+ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
options.go ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import "gohome.4gophers.ru/getapp/boosty/request"
4
+
5
+ type Option func(b *Boosty) error
6
+
7
+ func WithRequest(request *request.Request) Option {
8
+ return func(b *Boosty) error {
9
+ b.request = request
10
+
11
+ return nil
12
+ }
13
+ }
request/options.go ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package request
2
+
3
+ import (
4
+ "net/http"
5
+
6
+ "gohome.4gophers.ru/getapp/boosty/auth"
7
+ )
8
+
9
+ type Option func(b *Request) error
10
+
11
+ func WithClient(client *http.Client) Option {
12
+ return func(r *Request) error {
13
+ r.client = client
14
+
15
+ return nil
16
+ }
17
+ }
18
+
19
+ func WithAuth(auth *auth.Auth) Option {
20
+ return func(r *Request) error {
21
+ r.auth = auth
22
+
23
+ return nil
24
+ }
25
+ }
26
+
27
+ func WithUrl(url string) Option {
28
+ return func(r *Request) error {
29
+ r.url = url
30
+
31
+ return nil
32
+ }
33
+ }
request/request.go ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package request
2
+
3
+ import (
4
+ "encoding/json"
5
+ "errors"
6
+ "fmt"
7
+ "io"
8
+ "net/http"
9
+ "net/url"
10
+ "strings"
11
+ "time"
12
+
13
+ "gohome.4gophers.ru/getapp/boosty/auth"
14
+ )
15
+
16
+ type Request struct {
17
+ url string
18
+ client *http.Client
19
+ auth *auth.Auth
20
+ }
21
+
22
+ func New(options ...Option) (*Request, error) {
23
+ auth, err := auth.New()
24
+ if err != nil {
25
+ return nil, err
26
+ }
27
+
28
+ r := &Request{
29
+ url: "https://api.boosty.to",
30
+ client: &http.Client{},
31
+ auth: auth,
32
+ }
33
+
34
+ for _, o := range options {
35
+ if err := o(r); err != nil {
36
+ return nil, err
37
+ }
38
+ }
39
+
40
+ return r, nil
41
+ }
42
+
43
+ // check auth code and re request
44
+ func (b *Request) Request(method string, u string, body io.Reader) (*http.Response, error) {
45
+ resp, err := b.do(method, b.url+u, body)
46
+ if err != nil {
47
+ return nil, fmt.Errorf("error on do request: %w", err)
48
+ }
49
+
50
+ if resp.StatusCode == http.StatusUnauthorized {
51
+ if err := b.refresh(); err != nil {
52
+ return nil, fmt.Errorf("error on refresh token: %w", err)
53
+ }
54
+
55
+ resp, err = b.do(method, u, body)
56
+ if err != nil {
57
+ return nil, fmt.Errorf("error on do request: %w", err)
58
+ }
59
+ }
60
+
61
+ return resp, nil
62
+ }
63
+
64
+ // create request with auth token
65
+ func (b *Request) do(method string, u string, body io.Reader) (*http.Response, error) {
66
+ req, err := http.NewRequest(method, u, body)
67
+ if err != nil {
68
+ return nil, fmt.Errorf("boosty stats request error: %w", err)
69
+ }
70
+
71
+ req.Header.Add("Authorization", b.auth.BearerHeader())
72
+
73
+ resp, err := b.client.Do(req)
74
+ if err != nil {
75
+ return nil, fmt.Errorf("boosty stats do error: %w", err)
76
+ }
77
+
78
+ return resp, nil
79
+ }
80
+
81
+ type Refresh struct {
82
+ AccessToken string `json:"access_token"`
83
+ RefreshToken string `json:"refresh_token"`
84
+ ExpiresIn int64 `json:"expires_in"`
85
+ }
86
+
87
+ func (b *Request) refresh() error {
88
+ if b.auth.RefreshToken() == "" {
89
+ return errors.New("empty refresh token")
90
+ }
91
+
92
+ form := url.Values{}
93
+ form.Add("device_id", b.auth.DeviceId())
94
+ form.Add("device_os", "web")
95
+ form.Add("grant_type", "refresh_token")
96
+ form.Add("refresh_token", b.auth.RefreshToken())
97
+
98
+ req, err := http.NewRequest(http.MethodPost, b.url+"/oauth/token/", strings.NewReader(form.Encode()))
99
+ if err != nil {
100
+ return fmt.Errorf("boosty refresh request error: %w", err)
101
+ }
102
+
103
+ req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
104
+ req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36")
105
+ req.Header.Set("Authorization", b.auth.BearerHeader())
106
+ resp, err := b.client.Do(req)
107
+ if err != nil {
108
+ return fmt.Errorf("boosty refresh do error: %w", err)
109
+ }
110
+
111
+ if resp.StatusCode != http.StatusOK {
112
+ return fmt.Errorf("boosty refresh do error: %d", resp.StatusCode)
113
+ }
114
+
115
+ refresh := Refresh{}
116
+
117
+ if err := json.NewDecoder(resp.Body).Decode(&refresh); err != nil {
118
+ return fmt.Errorf("boosty refresh decode error: %w", err)
119
+ }
120
+
121
+ info := auth.Info{
122
+ AccessToken: refresh.AccessToken,
123
+ RefreshToken: refresh.RefreshToken,
124
+ ExpiresAt: time.Now().Unix() + refresh.ExpiresIn,
125
+ DeviceId: b.auth.DeviceId(),
126
+ }
127
+
128
+ b.auth.Update(info)
129
+ if err := b.auth.Save(); err != nil {
130
+ return fmt.Errorf("boosty refresh save error: %w", err)
131
+ }
132
+
133
+ return nil
134
+ }
stats.go ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Point struct {
10
+ Day int `json:"day"`
11
+ Year int `json:"year"`
12
+ Count int `json:"count"`
13
+ Month int `json:"month"`
14
+ }
15
+ type Stats struct {
16
+ PostSaleMoney []Point `json:"postSaleMoney"`
17
+ UpSubscribers []Point `json:"upSubscribers"`
18
+ MessagesSale []Point `json:"messagesSale"`
19
+ DecSubscribers []Point `json:"decSubscribers"`
20
+ PostsSale []Point `json:"postsSale"`
21
+ DonationsMoney []Point `json:"donationsMoney"`
22
+ GiftsSaleSaleMoney []Point `json:"giftsSaleSaleMoney"`
23
+ MessagesSaleMoney []Point `json:"messagesSaleMoney"`
24
+ TotalMoney []Point `json:"totalMoney"`
25
+ DecFollowers []Point `json:"decFollowers"`
26
+ IncSubscribersMoney []Point `json:"incSubscribersMoney"`
27
+ RecurrentsMoney []Point `json:"recurrentsMoney"`
28
+ Recurrents []Point `json:"recurrents"`
29
+ ReferalMoney []Point `json:"referalMoney"`
30
+ ReferalMoneyOut []Point `json:"referalMoneyOut"`
31
+ IncFollowers []Point `json:"incFollowers"`
32
+ Referal []Point `json:"referal"`
33
+ Donations []Point `json:"donations"`
34
+ IncSubscribers []Point `json:"incSubscribers"`
35
+ GiftsSale []Point `json:"giftsSale"`
36
+ UpSubscribersMoney []Point `json:"upSubscribersMoney"`
37
+ Holds []Point `json:"holds"`
38
+ }
39
+
40
+ func (b *Boosty) Stats(values url.Values) (*Stats, error) {
41
+ u := fmt.Sprintf("v1/blog/%s/stat/data/?%s", b.blog, values.Encode())
42
+
43
+ m := Method[Stats]{
44
+ request: b.request,
45
+ method: http.MethodGet,
46
+ url: u,
47
+ values: url.Values{},
48
+ }
49
+
50
+ return m.Call(Stats{})
51
+ }
stats_test.go ADDED
@@ -0,0 +1,779 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
8
+ "testing"
9
+
10
+ "github.com/stretchr/testify/suite"
11
+ "gohome.4gophers.ru/getapp/boosty/auth"
12
+ "gohome.4gophers.ru/getapp/boosty/request"
13
+ )
14
+
15
+ type StatsTestSuite struct {
16
+ suite.Suite
17
+ }
18
+
19
+ func (s *StatsTestSuite) SetupTest() {
20
+ //
21
+ }
22
+
23
+ func (s *StatsTestSuite) TestStats() {
24
+ tests := map[string]struct {
25
+ postSaleMoney int
26
+ decFollowers int
27
+ donations int
28
+ body string
29
+ token string
30
+ }{
31
+ "success stats": {
32
+ postSaleMoney: 5, decFollowers: 5, donations: 5, body: statsBody, token: "123",
33
+ },
34
+ }
35
+
36
+ for name, test := range tests {
37
+ s.T().Run(name, func(t *testing.T) {
38
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39
+ auth := r.Header.Get("Authorization")
40
+
41
+ s.Assert().Equal(auth, "Bearer "+test.token)
42
+
43
+ fmt.Fprintf(w, test.body)
44
+ }))
45
+ defer svr.Close()
46
+
47
+ auth, err := auth.New(auth.WithInfo(auth.Info{
48
+ AccessToken: test.token,
49
+ }))
50
+ s.Assert().NoError(err)
51
+
52
+ req, err := request.New(
53
+ request.WithUrl(svr.URL),
54
+ request.WithAuth(auth),
55
+ request.WithClient(&http.Client{}),
56
+ )
57
+ s.Assert().NoError(err)
58
+
59
+ b, err := New("", WithRequest(req))
60
+ s.Assert().NoError(err)
61
+
62
+ stats, err := b.Stats(url.Values{})
63
+
64
+ s.Assert().NoError(err)
65
+ s.Assert().Equal(test.postSaleMoney, len(stats.PostSaleMoney))
66
+ s.Assert().Equal(test.decFollowers, len(stats.DecFollowers))
67
+ s.Assert().Equal(test.donations, len(stats.Donations))
68
+ })
69
+ }
70
+ }
71
+
72
+ const statsBody = `
73
+ {
74
+ "postSaleMoney": [
75
+ {
76
+ "day": 20,
77
+ "year": 2023,
78
+ "count": 0,
79
+ "month": 8
80
+ },
81
+ {
82
+ "day": 5,
83
+ "year": 2023,
84
+ "month": 9,
85
+ "count": 0
86
+ },
87
+ {
88
+ "day": 6,
89
+ "year": 2023,
90
+ "count": 0,
91
+ "month": 9
92
+ },
93
+ {
94
+ "month": 9,
95
+ "count": 0,
96
+ "year": 2023,
97
+ "day": 7
98
+ },
99
+ {
100
+ "year": 2023,
101
+ "day": 18,
102
+ "month": 9,
103
+ "count": 0
104
+ }
105
+ ],
106
+ "upSubscribers": [
107
+ {
108
+ "year": 2023,
109
+ "day": 20,
110
+ "count": 0,
111
+ "month": 8
112
+ },
113
+ {
114
+ "month": 9,
115
+ "count": 0,
116
+ "day": 5,
117
+ "year": 2023
118
+ },
119
+ {
120
+ "count": 0,
121
+ "month": 9,
122
+ "year": 2023,
123
+ "day": 6
124
+ },
125
+ {
126
+ "count": 0,
127
+ "month": 9,
128
+ "day": 7,
129
+ "year": 2023
130
+ },
131
+ {
132
+ "year": 2023,
133
+ "day": 18,
134
+ "count": 0,
135
+ "month": 9
136
+ }
137
+ ],
138
+ "messagesSale": [
139
+ {
140
+ "count": 0,
141
+ "month": 8,
142
+ "year": 2023,
143
+ "day": 20
144
+ },
145
+ {
146
+ "month": 9,
147
+ "count": 0,
148
+ "year": 2023,
149
+ "day": 5
150
+ },
151
+ {
152
+ "count": 0,
153
+ "month": 9,
154
+ "day": 6,
155
+ "year": 2023
156
+ },
157
+ {
158
+ "month": 9,
159
+ "count": 0,
160
+ "year": 2023,
161
+ "day": 7
162
+ },
163
+ {
164
+ "day": 18,
165
+ "year": 2023,
166
+ "count": 0,
167
+ "month": 9
168
+ }
169
+ ],
170
+ "decSubscribers": [
171
+ {
172
+ "count": 0,
173
+ "month": 8,
174
+ "year": 2023,
175
+ "day": 20
176
+ },
177
+ {
178
+ "year": 2023,
179
+ "day": 5,
180
+ "count": 0,
181
+ "month": 9
182
+ },
183
+ {
184
+ "month": 9,
185
+ "count": 0,
186
+ "day": 6,
187
+ "year": 2023
188
+ },
189
+ {
190
+ "count": 0,
191
+ "month": 9,
192
+ "day": 7,
193
+ "year": 2023
194
+ },
195
+ {
196
+ "month": 9,
197
+ "count": 0,
198
+ "day": 18,
199
+ "year": 2023
200
+ }
201
+ ],
202
+ "postsSale": [
203
+ {
204
+ "day": 20,
205
+ "year": 2023,
206
+ "month": 8,
207
+ "count": 0
208
+ },
209
+ {
210
+ "count": 0,
211
+ "month": 9,
212
+ "year": 2023,
213
+ "day": 5
214
+ },
215
+ {
216
+ "day": 6,
217
+ "year": 2023,
218
+ "month": 9,
219
+ "count": 0
220
+ },
221
+ {
222
+ "month": 9,
223
+ "count": 0,
224
+ "year": 2023,
225
+ "day": 7
226
+ },
227
+ {
228
+ "day": 18,
229
+ "year": 2023,
230
+ "month": 9,
231
+ "count": 0
232
+ }
233
+ ],
234
+ "donationsMoney": [
235
+ {
236
+ "count": 0,
237
+ "month": 8,
238
+ "year": 2023,
239
+ "day": 20
240
+ },
241
+ {
242
+ "year": 2023,
243
+ "day": 5,
244
+ "month": 9,
245
+ "count": 0
246
+ },
247
+ {
248
+ "month": 9,
249
+ "count": 0,
250
+ "year": 2023,
251
+ "day": 6
252
+ },
253
+ {
254
+ "day": 7,
255
+ "year": 2023,
256
+ "count": 0,
257
+ "month": 9
258
+ },
259
+ {
260
+ "month": 9,
261
+ "count": 0,
262
+ "day": 18,
263
+ "year": 2023
264
+ }
265
+ ],
266
+ "giftsSaleSaleMoney": [
267
+ {
268
+ "month": 8,
269
+ "count": 0,
270
+ "day": 20,
271
+ "year": 2023
272
+ },
273
+ {
274
+ "month": 9,
275
+ "count": 0,
276
+ "day": 5,
277
+ "year": 2023
278
+ },
279
+ {
280
+ "year": 2023,
281
+ "day": 6,
282
+ "month": 9,
283
+ "count": 0
284
+ },
285
+ {
286
+ "month": 9,
287
+ "count": 0,
288
+ "day": 7,
289
+ "year": 2023
290
+ },
291
+ {
292
+ "day": 18,
293
+ "year": 2023,
294
+ "count": 0,
295
+ "month": 9
296
+ }
297
+ ],
298
+ "messagesSaleMoney": [
299
+ {
300
+ "month": 8,
301
+ "count": 0,
302
+ "year": 2023,
303
+ "day": 20
304
+ },
305
+ {
306
+ "year": 2023,
307
+ "day": 5,
308
+ "count": 0,
309
+ "month": 9
310
+ },
311
+ {
312
+ "year": 2023,
313
+ "day": 6,
314
+ "count": 0,
315
+ "month": 9
316
+ },
317
+ {
318
+ "day": 7,
319
+ "year": 2023,
320
+ "count": 0,
321
+ "month": 9
322
+ },
323
+ {
324
+ "day": 18,
325
+ "year": 2023,
326
+ "month": 9,
327
+ "count": 0
328
+ }
329
+ ],
330
+ "totalMoney": [
331
+ {
332
+ "day": 20,
333
+ "year": 2023,
334
+ "count": 0,
335
+ "month": 8
336
+ },
337
+ {
338
+ "month": 9,
339
+ "count": 0,
340
+ "year": 2023,
341
+ "day": 5
342
+ },
343
+ {
344
+ "year": 2023,
345
+ "day": 6,
346
+ "count": 300,
347
+ "month": 9
348
+ },
349
+ {
350
+ "year": 2023,
351
+ "day": 7,
352
+ "month": 9,
353
+ "count": 0
354
+ },
355
+ {
356
+ "count": 0,
357
+ "month": 9,
358
+ "day": 18,
359
+ "year": 2023
360
+ }
361
+ ],
362
+ "decFollowers": [
363
+ {
364
+ "count": 0,
365
+ "month": 8,
366
+ "year": 2023,
367
+ "day": 20
368
+ },
369
+ {
370
+ "day": 5,
371
+ "year": 2023,
372
+ "count": 0,
373
+ "month": 9
374
+ },
375
+ {
376
+ "month": 9,
377
+ "count": 0,
378
+ "day": 6,
379
+ "year": 2023
380
+ },
381
+ {
382
+ "year": 2023,
383
+ "day": 7,
384
+ "month": 9,
385
+ "count": 0
386
+ },
387
+ {
388
+ "count": 0,
389
+ "month": 9,
390
+ "year": 2023,
391
+ "day": 18
392
+ }
393
+ ],
394
+ "incSubscribersMoney": [
395
+ {
396
+ "year": 2023,
397
+ "day": 20,
398
+ "month": 8,
399
+ "count": 0
400
+ },
401
+ {
402
+ "count": 0,
403
+ "month": 9,
404
+ "day": 5,
405
+ "year": 2023
406
+ },
407
+ {
408
+ "day": 6,
409
+ "year": 2023,
410
+ "month": 9,
411
+ "count": 0
412
+ },
413
+ {
414
+ "month": 9,
415
+ "count": 0,
416
+ "year": 2023,
417
+ "day": 7
418
+ },
419
+ {
420
+ "year": 2023,
421
+ "day": 18,
422
+ "count": 0,
423
+ "month": 9
424
+ }
425
+ ],
426
+ "recurrentsMoney": [
427
+ {
428
+ "year": 2023,
429
+ "day": 20,
430
+ "month": 8,
431
+ "count": 0
432
+ },
433
+ {
434
+ "day": 5,
435
+ "year": 2023,
436
+ "count": 0,
437
+ "month": 9
438
+ },
439
+ {
440
+ "count": 300,
441
+ "month": 9,
442
+ "day": 6,
443
+ "year": 2023
444
+ },
445
+ {
446
+ "year": 2023,
447
+ "day": 7,
448
+ "count": 0,
449
+ "month": 9
450
+ },
451
+ {
452
+ "year": 2023,
453
+ "day": 18,
454
+ "count": 0,
455
+ "month": 9
456
+ }
457
+ ],
458
+ "recurrents": [
459
+ {
460
+ "month": 8,
461
+ "count": 0,
462
+ "day": 20,
463
+ "year": 2023
464
+ },
465
+ {
466
+ "year": 2023,
467
+ "day": 5,
468
+ "count": 0,
469
+ "month": 9
470
+ },
471
+ {
472
+ "day": 6,
473
+ "year": 2023,
474
+ "count": 1,
475
+ "month": 9
476
+ },
477
+ {
478
+ "year": 2023,
479
+ "day": 7,
480
+ "count": 0,
481
+ "month": 9
482
+ },
483
+ {
484
+ "month": 9,
485
+ "count": 0,
486
+ "day": 18,
487
+ "year": 2023
488
+ }
489
+ ],
490
+ "referalMoney": [
491
+ {
492
+ "year": 2023,
493
+ "day": 20,
494
+ "month": 8,
495
+ "count": 0
496
+ },
497
+ {
498
+ "year": 2023,
499
+ "day": 5,
500
+ "count": 0,
501
+ "month": 9
502
+ },
503
+ {
504
+ "month": 9,
505
+ "count": 0,
506
+ "year": 2023,
507
+ "day": 6
508
+ },
509
+ {
510
+ "year": 2023,
511
+ "day": 7,
512
+ "month": 9,
513
+ "count": 0
514
+ },
515
+ {
516
+ "count": 0,
517
+ "month": 9,
518
+ "day": 18,
519
+ "year": 2023
520
+ }
521
+ ],
522
+ "referalMoneyOut": [
523
+ {
524
+ "day": 20,
525
+ "year": 2023,
526
+ "count": 0,
527
+ "month": 8
528
+ },
529
+ {
530
+ "day": 5,
531
+ "year": 2023,
532
+ "count": 0,
533
+ "month": 9
534
+ },
535
+ {
536
+ "month": 9,
537
+ "count": 0,
538
+ "day": 6,
539
+ "year": 2023
540
+ },
541
+ {
542
+ "year": 2023,
543
+ "day": 7,
544
+ "count": 0,
545
+ "month": 9
546
+ },
547
+ {
548
+ "count": 0,
549
+ "month": 9,
550
+ "year": 2023,
551
+ "day": 18
552
+ }
553
+ ],
554
+ "incFollowers": [
555
+ {
556
+ "count": 0,
557
+ "month": 8,
558
+ "day": 20,
559
+ "year": 2023
560
+ },
561
+ {
562
+ "day": 5,
563
+ "year": 2023,
564
+ "month": 9,
565
+ "count": 0
566
+ },
567
+ {
568
+ "day": 6,
569
+ "year": 2023,
570
+ "month": 9,
571
+ "count": 0
572
+ },
573
+ {
574
+ "month": 9,
575
+ "count": 0,
576
+ "year": 2023,
577
+ "day": 7
578
+ },
579
+ {
580
+ "month": 9,
581
+ "count": 0,
582
+ "year": 2023,
583
+ "day": 18
584
+ }
585
+ ],
586
+ "referal": [
587
+ {
588
+ "count": 0,
589
+ "month": 8,
590
+ "year": 2023,
591
+ "day": 20
592
+ },
593
+ {
594
+ "year": 2023,
595
+ "day": 5,
596
+ "month": 9,
597
+ "count": 0
598
+ },
599
+ {
600
+ "day": 6,
601
+ "year": 2023,
602
+ "count": 0,
603
+ "month": 9
604
+ },
605
+ {
606
+ "year": 2023,
607
+ "day": 7,
608
+ "month": 9,
609
+ "count": 0
610
+ },
611
+ {
612
+ "year": 2023,
613
+ "day": 18,
614
+ "count": 0,
615
+ "month": 9
616
+ }
617
+ ],
618
+ "donations": [
619
+ {
620
+ "day": 20,
621
+ "year": 2023,
622
+ "month": 8,
623
+ "count": 0
624
+ },
625
+ {
626
+ "month": 9,
627
+ "count": 0,
628
+ "year": 2023,
629
+ "day": 5
630
+ },
631
+ {
632
+ "year": 2023,
633
+ "day": 6,
634
+ "month": 9,
635
+ "count": 0
636
+ },
637
+ {
638
+ "day": 7,
639
+ "year": 2023,
640
+ "month": 9,
641
+ "count": 0
642
+ },
643
+ {
644
+ "year": 2023,
645
+ "day": 18,
646
+ "count": 0,
647
+ "month": 9
648
+ }
649
+ ],
650
+ "incSubscribers": [
651
+ {
652
+ "year": 2023,
653
+ "day": 20,
654
+ "count": 0,
655
+ "month": 8
656
+ },
657
+ {
658
+ "day": 5,
659
+ "year": 2023,
660
+ "count": 0,
661
+ "month": 9
662
+ },
663
+ {
664
+ "year": 2023,
665
+ "day": 6,
666
+ "count": 0,
667
+ "month": 9
668
+ },
669
+ {
670
+ "day": 7,
671
+ "year": 2023,
672
+ "month": 9,
673
+ "count": 0
674
+ },
675
+ {
676
+ "year": 2023,
677
+ "day": 18,
678
+ "count": 0,
679
+ "month": 9
680
+ }
681
+ ],
682
+ "giftsSale": [
683
+ {
684
+ "day": 20,
685
+ "year": 2023,
686
+ "month": 8,
687
+ "count": 0
688
+ },
689
+ {
690
+ "day": 5,
691
+ "year": 2023,
692
+ "month": 9,
693
+ "count": 0
694
+ },
695
+ {
696
+ "count": 0,
697
+ "month": 9,
698
+ "day": 6,
699
+ "year": 2023
700
+ },
701
+ {
702
+ "count": 0,
703
+ "month": 9,
704
+ "day": 7,
705
+ "year": 2023
706
+ },
707
+ {
708
+ "month": 9,
709
+ "count": 0,
710
+ "day": 18,
711
+ "year": 2023
712
+ }
713
+ ],
714
+ "upSubscribersMoney": [
715
+ {
716
+ "year": 2023,
717
+ "day": 20,
718
+ "month": 8,
719
+ "count": 0
720
+ },
721
+ {
722
+ "day": 5,
723
+ "year": 2023,
724
+ "count": 0,
725
+ "month": 9
726
+ },
727
+ {
728
+ "month": 9,
729
+ "count": 0,
730
+ "year": 2023,
731
+ "day": 6
732
+ },
733
+ {
734
+ "year": 2023,
735
+ "day": 7,
736
+ "count": 0,
737
+ "month": 9
738
+ },
739
+ {
740
+ "day": 18,
741
+ "year": 2023,
742
+ "count": 0,
743
+ "month": 9
744
+ }
745
+ ],
746
+ "holds": [
747
+ {
748
+ "year": 2023,
749
+ "day": 20,
750
+ "month": 8,
751
+ "count": 0
752
+ },
753
+ {
754
+ "year": 2023,
755
+ "day": 5,
756
+ "month": 9,
757
+ "count": 0
758
+ },
759
+ {
760
+ "month": 9,
761
+ "count": 0,
762
+ "day": 6,
763
+ "year": 2023
764
+ },
765
+ {
766
+ "count": 0,
767
+ "month": 9,
768
+ "year": 2023,
769
+ "day": 7
770
+ },
771
+ {
772
+ "count": 0,
773
+ "month": 9,
774
+ "year": 2023,
775
+ "day": 18
776
+ }
777
+ ]
778
+ }
779
+ `
subscribers.go ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Subscriber struct {
10
+ HasAvatar bool `json:"hasAvatar"`
11
+ Payments int `json:"payments"`
12
+ Level struct {
13
+ Deleted bool `json:"deleted"`
14
+ Name string `json:"name"`
15
+ Price int `json:"price"`
16
+ OwnerID int `json:"ownerId"`
17
+ Data []struct {
18
+ Type string `json:"type"`
19
+ Content string `json:"content"`
20
+ Modificator string `json:"modificator"`
21
+ } `json:"data"`
22
+ ID int `json:"id"`
23
+ CurrencyPrices struct {
24
+ RUB int `json:"RUB"`
25
+ USD float64 `json:"USD"`
26
+ } `json:"currencyPrices"`
27
+ CreatedAt int `json:"createdAt"`
28
+ IsArchived bool `json:"isArchived"`
29
+ } `json:"level"`
30
+ Email string `json:"email"`
31
+ IsBlackListed bool `json:"isBlackListed"`
32
+ ID int `json:"id"`
33
+ Name string `json:"name"`
34
+ OnTime int `json:"onTime"`
35
+ Subscribed bool `json:"subscribed"`
36
+ NextPayTime int `json:"nextPayTime"`
37
+ Price int `json:"price"`
38
+ AvatarURL string `json:"avatarUrl"`
39
+ }
40
+
41
+ type Subscribers struct {
42
+ Offset int `json:"offset"`
43
+ Total int `json:"total"`
44
+ Limit int `json:"limit"`
45
+ Data []Subscriber `json:"data"`
46
+ }
47
+
48
+ func (b *Boosty) Subscribers(values url.Values) (*Subscribers, error) {
49
+ u := fmt.Sprintf("/v1/blog/%s/subscribers?%s", b.blog, values.Encode())
50
+ //u := fmt.Sprintf("/v1/blog/%s/subscribers?sort_by=on_time&offset=%d&limit=%d&order=gt", b.blog, offset, limit)
51
+
52
+ m := Method[Subscribers]{
53
+ request: b.request,
54
+ method: http.MethodGet,
55
+ url: u,
56
+ values: url.Values{},
57
+ }
58
+
59
+ return m.Call(Subscribers{})
60
+ }
subscribers_test.go ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
8
+ "testing"
9
+
10
+ "github.com/stretchr/testify/suite"
11
+ "gohome.4gophers.ru/getapp/boosty/auth"
12
+ "gohome.4gophers.ru/getapp/boosty/request"
13
+ )
14
+
15
+ type SubscribersTestSuite struct {
16
+ suite.Suite
17
+ }
18
+
19
+ func (s *SubscribersTestSuite) SetupTest() {
20
+ //
21
+ }
22
+
23
+ func (s *SubscribersTestSuite) TestSubscribers() {
24
+ tests := map[string]struct {
25
+ count int
26
+ body string
27
+ name string
28
+ token string
29
+ }{
30
+ "success count 2": {
31
+ count: 2, body: subscribersBody, name: "getapp.store", token: "123",
32
+ },
33
+ }
34
+
35
+ for name, test := range tests {
36
+ s.T().Run(name, func(t *testing.T) {
37
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38
+ a := r.Header.Get("Authorization")
39
+
40
+ s.Assert().Equal(a, "Bearer "+test.token)
41
+
42
+ fmt.Fprintf(w, test.body)
43
+ }))
44
+ defer svr.Close()
45
+
46
+ auth, err := auth.New(auth.WithInfo(auth.Info{
47
+ AccessToken: test.token,
48
+ }))
49
+ s.Assert().NoError(err)
50
+
51
+ req, err := request.New(
52
+ request.WithUrl(svr.URL),
53
+ request.WithAuth(auth),
54
+ request.WithClient(&http.Client{}),
55
+ )
56
+ s.Assert().NoError(err)
57
+
58
+ b, err := New("", WithRequest(req))
59
+ s.Assert().NoError(err)
60
+
61
+ v := url.Values{}
62
+ v.Add("offset", "0")
63
+ v.Add("limit", "10")
64
+ v.Add("order", "gt")
65
+ v.Add("sort_by", "on_time")
66
+
67
+ ss, err := b.Subscribers(v)
68
+
69
+ s.Assert().NoError(err)
70
+ s.Assert().Equal(test.count, len(ss.Data))
71
+ if len(ss.Data) > 0 {
72
+ s.Assert().Equal(test.name, ss.Data[0].Name)
73
+ }
74
+ })
75
+ }
76
+ }
77
+
78
+ const subscribersBody = `
79
+ {
80
+ "limit": 2,
81
+ "data": [
82
+ {
83
+ "subscribed": true,
84
+ "price": 300,
85
+ "payments": 2970,
86
+ "hasAvatar": true,
87
+ "nextPayTime": 1699209830,
88
+ "id": 3684586,
89
+ "level": {
90
+ "ownerId": 10435460,
91
+ "price": 300,
92
+ "data": [
93
+ {
94
+ "type": "text",
95
+ "modificator": "",
96
+ "content": "[\"Узнавай про фикс багов самый первый\",\"unstyled\",[]]"
97
+ },
98
+ {
99
+ "type": "text",
100
+ "modificator": "BLOCK_END",
101
+ "content": ""
102
+ },
103
+ {
104
+ "type": "text",
105
+ "modificator": "BLOCK_END",
106
+ "content": ""
107
+ }
108
+ ],
109
+ "isArchived": false,
110
+ "name": "Стандартная подписка",
111
+ "createdAt": 1664319534,
112
+ "id": 1091773,
113
+ "currencyPrices": {
114
+ "RUB": 300,
115
+ "USD": 3.2
116
+ },
117
+ "deleted": false
118
+ },
119
+ "name": "getapp.store",
120
+ "avatarUrl": "https://images.boosty.to/user/3684586/avatar?change_time=1664275665",
121
+ "email": "artem.kovardin@gmail.com",
122
+ "onTime": 1670697830,
123
+ "isBlackListed": false
124
+ },
125
+ {
126
+ "price": 300,
127
+ "subscribed": true,
128
+ "id": 11222871,
129
+ "nextPayTime": 1697748053,
130
+ "hasAvatar": true,
131
+ "payments": 3240,
132
+ "email": "",
133
+ "avatarUrl": "https://images.boosty.to/user/11222871/avatar?change_time=1666643907",
134
+ "name": "Lena Nesterenko",
135
+ "level": {
136
+ "data": [
137
+ {
138
+ "type": "text",
139
+ "modificator": "",
140
+ "content": "[\"Узнавай про фикс багов самый первый\",\"unstyled\",[]]"
141
+ },
142
+ {
143
+ "type": "text",
144
+ "modificator": "BLOCK_END",
145
+ "content": ""
146
+ },
147
+ {
148
+ "modificator": "BLOCK_END",
149
+ "type": "text",
150
+ "content": ""
151
+ }
152
+ ],
153
+ "isArchived": false,
154
+ "createdAt": 1664319534,
155
+ "name": "Стандартная подписка",
156
+ "ownerId": 10435460,
157
+ "price": 300,
158
+ "currencyPrices": {
159
+ "RUB": 300,
160
+ "USD": 3.2
161
+ },
162
+ "deleted": false,
163
+ "id": 1091773
164
+ },
165
+ "isBlackListed": false,
166
+ "onTime": 1666644053
167
+ }
168
+ ],
169
+ "offset": 2,
170
+ "total": 2
171
+ }
172
+ `
subscriptions.go ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Subscription struct {
10
+ IsArchived bool `json:"isArchived"`
11
+ Deleted bool `json:"deleted"`
12
+ Badges []interface{} `json:"badges"`
13
+ ID int `json:"id"`
14
+ Data []struct {
15
+ Modificator string `json:"modificator"`
16
+ Type string `json:"type"`
17
+ Content string `json:"content"`
18
+ } `json:"data"`
19
+ Name string `json:"name"`
20
+ ExternalApps struct {
21
+ Telegram struct {
22
+ Groups []interface{} `json:"groups"`
23
+ IsConfigured bool `json:"isConfigured"`
24
+ } `json:"telegram"`
25
+ Discord struct {
26
+ Data struct {
27
+ Role struct {
28
+ Name string `json:"name"`
29
+ ID string `json:"id"`
30
+ } `json:"role"`
31
+ } `json:"data"`
32
+ IsConfigured bool `json:"isConfigured"`
33
+ } `json:"discord"`
34
+ } `json:"externalApps"`
35
+ Price int `json:"price"`
36
+ Promos []interface{} `json:"promos"`
37
+ OwnerID int `json:"ownerId"`
38
+ CreatedAt int `json:"createdAt"`
39
+ CurrencyPrices struct {
40
+ USD float64 `json:"USD"`
41
+ RUB int `json:"RUB"`
42
+ } `json:"currencyPrices"`
43
+ }
44
+
45
+ type Subscriptions struct {
46
+ Offset int `json:"offset"`
47
+ Total int `json:"total"`
48
+ Limit int `json:"limit"`
49
+ Data []Subscription `json:"data"`
50
+ }
51
+
52
+ func (b *Boosty) Subscriptions(values url.Values) (*Subscriptions, error) {
53
+ u := fmt.Sprintf("/v1/blog/%s/subscription_level/?%s", b.blog, values.Encode())
54
+ //u := fmt.Sprintf("/v1/blog/%s/subscription_level/?show_free_level=true&sort_by=on_time&offset=%d&limit=%d&order=gt", b.blog, offset, limit)
55
+
56
+ m := Method[Subscriptions]{
57
+ request: b.request,
58
+ method: http.MethodGet,
59
+ url: u,
60
+ values: url.Values{},
61
+ }
62
+
63
+ return m.Call(Subscriptions{})
64
+ }
subscriptions_test.go ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
8
+ "testing"
9
+
10
+ "github.com/stretchr/testify/suite"
11
+ "gohome.4gophers.ru/getapp/boosty/auth"
12
+ "gohome.4gophers.ru/getapp/boosty/request"
13
+ )
14
+
15
+ type SubscriptionsTestSuite struct {
16
+ suite.Suite
17
+ }
18
+
19
+ func (s *SubscriptionsTestSuite) SetupTest() {
20
+ //
21
+ }
22
+
23
+ func (s *SubscriptionsTestSuite) TestSubscriptions() {
24
+ tests := map[string]struct {
25
+ count int
26
+ body string
27
+ name string
28
+ token string
29
+ }{
30
+ "success count 3": {
31
+ count: 3, body: subscriptionsBody, name: "Follower", token: "123",
32
+ },
33
+ }
34
+
35
+ for name, test := range tests {
36
+ s.T().Run(name, func(t *testing.T) {
37
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38
+ auth := r.Header.Get("Authorization")
39
+
40
+ s.Assert().Equal(auth, "Bearer "+test.token)
41
+
42
+ fmt.Fprintf(w, test.body)
43
+ }))
44
+ defer svr.Close()
45
+
46
+ auth, err := auth.New(auth.WithInfo(auth.Info{
47
+ AccessToken: test.token,
48
+ }))
49
+ s.Assert().NoError(err)
50
+
51
+ req, err := request.New(
52
+ request.WithUrl(svr.URL),
53
+ request.WithAuth(auth),
54
+ request.WithClient(&http.Client{}),
55
+ )
56
+ s.Assert().NoError(err)
57
+
58
+ b, err := New("", WithRequest(req))
59
+ s.Assert().NoError(err)
60
+
61
+ v := url.Values{}
62
+ v.Add("show_free_level", "true")
63
+ v.Add("sort_by", "on_time")
64
+ v.Add("offset", "0")
65
+ v.Add("limit", "10")
66
+ v.Add("order", "gt")
67
+
68
+ ss, err := b.Subscriptions(v)
69
+
70
+ s.Assert().NoError(err)
71
+ s.Assert().Equal(test.count, len(ss.Data))
72
+ if len(ss.Data) > 0 {
73
+ s.Assert().Equal(test.name, ss.Data[0].Name)
74
+ }
75
+ })
76
+ }
77
+ }
78
+
79
+ const subscriptionsBody = `
80
+ {
81
+ "currentId": null,
82
+ "subscriptions": [],
83
+ "nextId": null,
84
+ "data": [
85
+ {
86
+ "currencyPrices": {
87
+ "USD": 0,
88
+ "RUB": 0
89
+ },
90
+ "promos": [],
91
+ "externalApps": {
92
+ "telegram": {
93
+ "groups": [],
94
+ "isConfigured": false
95
+ },
96
+ "discord": {
97
+ "isConfigured": false,
98
+ "data": {
99
+ "role": {
100
+ "name": "",
101
+ "id": ""
102
+ }
103
+ }
104
+ }
105
+ },
106
+ "id": 1091770,
107
+ "price": 0,
108
+ "ownerId": 10435460,
109
+ "isArchived": false,
110
+ "createdAt": 1664319306,
111
+ "name": "Follower",
112
+ "badges": [],
113
+ "data": [],
114
+ "deleted": false
115
+ },
116
+ {
117
+ "promos": [],
118
+ "currencyPrices": {
119
+ "RUB": 300,
120
+ "USD": 3.2
121
+ },
122
+ "ownerId": 10435460,
123
+ "isArchived": false,
124
+ "externalApps": {
125
+ "telegram": {
126
+ "isConfigured": false,
127
+ "groups": []
128
+ },
129
+ "discord": {
130
+ "isConfigured": false,
131
+ "data": {
132
+ "role": {
133
+ "name": "",
134
+ "id": ""
135
+ }
136
+ }
137
+ }
138
+ },
139
+ "price": 300,
140
+ "id": 1091773,
141
+ "createdAt": 1664319534,
142
+ "deleted": false,
143
+ "data": [
144
+ {
145
+ "modificator": "",
146
+ "type": "text",
147
+ "content": "[\"Узнавай про фикс багов самый первый\",\"unstyled\",[]]"
148
+ },
149
+ {
150
+ "modificator": "BLOCK_END",
151
+ "type": "text",
152
+ "content": ""
153
+ },
154
+ {
155
+ "content": "",
156
+ "type": "text",
157
+ "modificator": "BLOCK_END"
158
+ }
159
+ ],
160
+ "name": "Стандартная подписка",
161
+ "badges": []
162
+ },
163
+ {
164
+ "isArchived": false,
165
+ "ownerId": 10435460,
166
+ "price": 700,
167
+ "id": 1122632,
168
+ "externalApps": {
169
+ "discord": {
170
+ "isConfigured": false,
171
+ "data": {
172
+ "role": {
173
+ "id": "",
174
+ "name": ""
175
+ }
176
+ }
177
+ },
178
+ "telegram": {
179
+ "groups": [],
180
+ "isConfigured": false
181
+ }
182
+ },
183
+ "promos": [],
184
+ "currencyPrices": {
185
+ "USD": 7.5,
186
+ "RUB": 700
187
+ },
188
+ "deleted": false,
189
+ "data": [
190
+ {
191
+ "modificator": "",
192
+ "type": "text",
193
+ "content": "[\"Заказывай новые фичи\",\"unstyled\",[]]"
194
+ },
195
+ {
196
+ "content": "",
197
+ "type": "text",
198
+ "modificator": "BLOCK_END"
199
+ }
200
+ ],
201
+ "badges": [],
202
+ "name": "Расширенная подписка",
203
+ "createdAt": 1665493091
204
+ }
205
+ ]
206
+ }
207
+ `
targets.go ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/url"
7
+ )
8
+
9
+ type Targets struct {
10
+ Data []Target `json:"data"`
11
+ }
12
+
13
+ type Target struct {
14
+ CreatedAt int `json:"createdAt"`
15
+ Type string `json:"type"`
16
+ Priority int `json:"priority"`
17
+ TargetSum int `json:"targetSum"`
18
+ Description string `json:"description"`
19
+ FinishTime interface{} `json:"finishTime"`
20
+ BloggerID int `json:"bloggerId"`
21
+ CurrentSum int `json:"currentSum"`
22
+ ID int `json:"id"`
23
+ BloggerURL string `json:"bloggerUrl"`
24
+ }
25
+
26
+ func (b *Boosty) Targets(values url.Values) (*Targets, error) {
27
+ u := fmt.Sprintf("/v1/target/%s/?%s", b.blog, values.Encode())
28
+
29
+ m := Method[Targets]{
30
+ request: b.request,
31
+ method: http.MethodGet,
32
+ url: u,
33
+ values: values,
34
+ }
35
+
36
+ return m.Call(Targets{})
37
+ }
targets_test.go ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package boosty
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "net/url"
8
+ "testing"
9
+
10
+ "github.com/stretchr/testify/suite"
11
+ "gohome.4gophers.ru/getapp/boosty/auth"
12
+ "gohome.4gophers.ru/getapp/boosty/request"
13
+ )
14
+
15
+ type TargetsTestSuite struct {
16
+ suite.Suite
17
+ }
18
+
19
+ func (s *TargetsTestSuite) SetupTest() {
20
+ //
21
+ }
22
+
23
+ func (s *TargetsTestSuite) TestStats() {
24
+ tests := map[string]struct {
25
+ targetsCount int
26
+ bloggerId int
27
+ targetSum int
28
+ body string
29
+ token string
30
+ }{
31
+ "success stats": {
32
+ targetsCount: 1, bloggerId: 10435460, targetSum: 1000, body: targetsBody, token: "123",
33
+ },
34
+ }
35
+
36
+ for name, test := range tests {
37
+ s.T().Run(name, func(t *testing.T) {
38
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39
+ auth := r.Header.Get("Authorization")
40
+
41
+ s.Assert().Equal(auth, "Bearer "+test.token)
42
+
43
+ fmt.Fprintf(w, test.body)
44
+ }))
45
+ defer svr.Close()
46
+
47
+ auth, err := auth.New(auth.WithInfo(auth.Info{
48
+ AccessToken: test.token,
49
+ }))
50
+ s.Assert().NoError(err)
51
+
52
+ req, err := request.New(
53
+ request.WithUrl(svr.URL),
54
+ request.WithAuth(auth),
55
+ request.WithClient(&http.Client{}),
56
+ )
57
+ s.Assert().NoError(err)
58
+
59
+ b, err := New("", WithRequest(req))
60
+ s.Assert().NoError(err)
61
+
62
+ values := url.Values{}
63
+ values.Add("show_deleted", "true")
64
+
65
+ stats, err := b.Targets(values)
66
+
67
+ s.Assert().NoError(err)
68
+ s.Assert().Equal(test.targetsCount, len(stats.Data))
69
+ s.Assert().Equal(test.bloggerId, stats.Data[0].BloggerID)
70
+ s.Assert().Equal(test.targetSum, stats.Data[0].TargetSum)
71
+ })
72
+ }
73
+ }
74
+
75
+ const targetsBody = `
76
+ {
77
+ "data": [
78
+ {
79
+ "currentSum": 4,
80
+ "bloggerId": 10435460,
81
+ "bloggerUrl": "getapp",
82
+ "id": 242127,
83
+ "type": "subscribers",
84
+ "createdAt": 1665345059,
85
+ "targetSum": 1000,
86
+ "description": "С 1 000 подписчиков будет значительно проще заниматься проектом. Появится возможность сделать демо версию и оказывать поддержку.",
87
+ "priority": 0,
88
+ "finishTime": null
89
+ }
90
+ ]
91
+ }
92
+ `