forked from Henry-Sarabia/igdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
161 lines (134 loc) · 4.6 KB
/
page.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package igdb
import (
"github.com/Henry-Sarabia/sliceconv"
"github.com/pkg/errors"
"strconv"
)
//go:generate gomodifytags -file $GOFILE -struct Page -add-tags json -w
// Page represents an entry in the multipurpose page system
// currently used for youtubers and media organizations.
// For more information visit: https://api-docs.igdb.com/#page
type Page struct {
ID int `json:"id"`
Background int `json:"background"`
Battlenet string `json:"battlenet"`
Category PageCategory `json:"category"`
Color PageColor `json:"color"`
Company int `json:"company"`
Country int `json:"country"`
CreatedAt int `json:"created_at"`
Description string `json:"description"`
Feed int `json:"feed"`
Game int `json:"game"`
Name string `json:"name"`
Origin string `json:"origin"`
PageFollowsCount int `json:"page_follows_count"`
PageLogo int `json:"page_logo"`
Slug string `json:"slug"`
SubCategory PageSubCategory `json:"sub_category"`
UpdatedAt int `json:"updated_at"`
Uplay string `json:"uplay"`
URL string `json:"url"`
User int `json:"user"`
Websites []int `json:"websites"`
}
//go:generate stringer -type=PageCategory,PageSubCategory,PageColor
// PageCategory specifies the type of media associated with a particular page.
type PageCategory int
// Expected PageCategory enums from the IGDB.
const (
PagePersonality PageCategory = iota + 1
PageMediaOrganization
PageContentCreator
PageClanTeam
)
// PageSubCategory specifies a specific field of media for a particular page.
type PageSubCategory int
// Expected PageSubCategory enums from the IGDB.
const (
PageUser PageSubCategory = iota + 1
PageGame
PageCompany
PageConsumer
PageIndustry
PageESports
)
// PageColor specifies a particular color.
type PageColor int
// Expected PageColor enums from the IGDB.
const (
PageGreen PageColor = iota
PageBlue
PageRed
PageOrange
PagePink
PageYellow
)
// PageService handles all the API calls for the IGDB Page endpoint.
type PageService service
// Get returns a single Page identified by the provided IGDB ID. Provide
// the SetFields functional option if you need to specify which fields to
// retrieve. If the ID does not match any Pages, an error is returned.
func (ps *PageService) Get(id int, opts ...Option) (*Page, error) {
if id < 0 {
return nil, ErrNegativeID
}
var pg []*Page
opts = append(opts, SetFilter("id", OpEquals, strconv.Itoa(id)))
err := ps.client.get(ps.end, &pg, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Page with ID %v", id)
}
return pg[0], nil
}
// List returns a list of Pages identified by the provided list of IGDB IDs.
// Provide functional options to sort, filter, and paginate the results.
// Any ID that does not match a Page is ignored. If none of the IDs
// match a Page, an error is returned.
func (ps *PageService) List(ids []int, opts ...Option) ([]*Page, error) {
for len(ids) < 1 {
return nil, ErrEmptyIDs
}
for _, id := range ids {
if id < 0 {
return nil, ErrNegativeID
}
}
var pg []*Page
opts = append(opts, SetFilter("id", OpContainsAtLeast, sliceconv.Itoa(ids)...))
err := ps.client.get(ps.end, &pg, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Pages with IDs %v", ids)
}
return pg, nil
}
// Index returns an index of Pages based solely on the provided functional
// options used to sort, filter, and paginate the results. If no Pages can
// be found using the provided options, an error is returned.
func (ps *PageService) Index(opts ...Option) ([]*Page, error) {
var pg []*Page
err := ps.client.get(ps.end, &pg, opts...)
if err != nil {
return nil, errors.Wrap(err, "cannot get index of Pages")
}
return pg, nil
}
// Count returns the number of Pages available in the IGDB.
// Provide the SetFilter functional option if you need to filter
// which Pages to count.
func (ps *PageService) Count(opts ...Option) (int, error) {
ct, err := ps.client.getCount(ps.end, opts...)
if err != nil {
return 0, errors.Wrap(err, "cannot count Pages")
}
return ct, nil
}
// Fields returns the up-to-date list of fields in an
// IGDB Page object.
func (ps *PageService) Fields() ([]string, error) {
f, err := ps.client.getFields(ps.end)
if err != nil {
return nil, errors.Wrap(err, "cannot get Page fields")
}
return f, nil
}