-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.go
99 lines (90 loc) · 3 KB
/
collections.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package deviantart
import (
"fmt"
"github.com/dghubble/sling"
"github.com/google/uuid"
)
type Collection struct {
FolderID uuid.UUID `json:"folderid"`
Name string `json:"name"`
Description string `json:"description"`
Size uint32 `json:"size,omitempty"`
Thumb *Deviation `json:"thumb,omitempty"`
Deviations []Deviation `json:"deviations,omitempty"`
}
type CollectionsService struct {
sling *sling.Sling
*FoldersService[Collection]
}
func newCollectionsService(sling *sling.Sling) *CollectionsService {
base := sling.Path("collections/")
return &CollectionsService{
sling: base,
FoldersService: newFoldersService[Collection](base.New()),
}
}
type faveParams struct {
// ID of the Deviation to favourite.
DeviationID uuid.UUID `url:"deviationid"`
// Optional `UUID` of the Collection folder to add the favourite into.
FolderIDs []uuid.UUID `url:"folderid,omitempty"`
}
// Fave adds deviation to favourites.
//
// You can add deviation to multiple collections at once. If you omit `folderID`
// parameter, it will be added to Featured collection.
//
// Returns the total number of times this deviation was favourited after the
// fave event.
//
// Users can fave their own deviations, when this happens the fave is not
// counted but the item is added to the requested folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection
func (s *CollectionsService) Fave(deviationID uuid.UUID, folderIDs ...uuid.UUID) (int, error) {
var (
success map[string]any
failure Error
)
_, err := s.sling.New().Post("fave").BodyForm(&faveParams{DeviationID: deviationID, FolderIDs: folderIDs}).Receive(&success, &failure)
if err != nil {
return 0, fmt.Errorf("unable to fave the deviation: %w", err)
}
return success["favourites"].(int), nil
}
// Unfave removes deviation from favourites.
//
// You can remove deviation from multiple collections at once. If you omit
// `folderID` parameter, it will be removed from Featured collection.
//
// Returns the total number of times this deviation was favourited after the
// unfave event.
//
// If a user has faved their own deviation, unfave can be used to remove the
// deviation from a given folder. Favorite counts are not affected if the
// deviation is owned by the user.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection
func (s *CollectionsService) Unfave(deviationID uuid.UUID, folderIDs ...uuid.UUID) (int, error) {
var (
success map[string]any
failure Error
)
_, err := s.sling.New().Post("unfave").BodyForm(&faveParams{DeviationID: deviationID, FolderIDs: folderIDs}).Receive(&success, &failure)
if err != nil {
return 0, fmt.Errorf("unable to unfave the deviation: %w", err)
}
return success["favourites"].(int), nil
}