-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexternalgame.go
134 lines (110 loc) · 3.77 KB
/
externalgame.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
package igdb
import (
"strconv"
"github.com/Henry-Sarabia/sliceconv"
"github.com/pkg/errors"
)
//go:generate gomodifytags -file $GOFILE -struct ExternalGame -add-tags json -w
// ExternalGame contains the ID and other metadata for a game
// on a third party service.
// For more information visit: https://api-docs.igdb.com/#external-game
type ExternalGame struct {
ID int `json:"id"`
Category ExternalGameCategory `json:"category"`
CreatedAt int `json:"created_at"`
Game int `json:"game"`
Name string `json:"name"`
UID string `json:"uid"`
UpdatedAt int `json:"updated_at"`
Url string `json:"url"`
Year int `json:"year"`
}
// ExternalGameCategory speficies an external game, platform, or media service.
type ExternalGameCategory int
//go:generate stringer -type=ExternalGameCategory
// Expected ExternalGameCategory enums from the IGDB.
const (
ExternalSteam ExternalGameCategory = iota + 1
_
_
_
ExternalGOG
_
_
_
_
ExternalYoutube
ExternalMicrosoft
_
ExternalApple
ExternalTwitch
ExternalAndroid
)
// ExternalGameService handles all the API calls for the IGDB ExternalGame endpoint.
type ExternalGameService service
// Get returns a single ExternalGame 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 ExternalGames, an error is returned.
func (es *ExternalGameService) Get(id int, opts ...Option) (*ExternalGame, error) {
if id < 0 {
return nil, ErrNegativeID
}
var ext []*ExternalGame
opts = append(opts, SetFilter("id", OpEquals, strconv.Itoa(id)))
err := es.client.post(es.end, &ext, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get ExternalGame with ID %v", id)
}
return ext[0], nil
}
// List returns a list of ExternalGames 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 ExternalGame is ignored. If none of the IDs
// match a ExternalGame, an error is returned.
func (es *ExternalGameService) List(ids []int, opts ...Option) ([]*ExternalGame, error) {
for len(ids) < 1 {
return nil, ErrEmptyIDs
}
for _, id := range ids {
if id < 0 {
return nil, ErrNegativeID
}
}
var ext []*ExternalGame
opts = append(opts, SetFilter("id", OpContainsAtLeast, sliceconv.Itoa(ids)...))
err := es.client.post(es.end, &ext, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get ExternalGames with IDs %v", ids)
}
return ext, nil
}
// Index returns an index of ExternalGames based solely on the provided functional
// options used to sort, filter, and paginate the results. If no ExternalGames can
// be found using the provided options, an error is returned.
func (es *ExternalGameService) Index(opts ...Option) ([]*ExternalGame, error) {
var ext []*ExternalGame
err := es.client.post(es.end, &ext, opts...)
if err != nil {
return nil, errors.Wrap(err, "cannot get index of ExternalGames")
}
return ext, nil
}
// Count returns the number of ExternalGames available in the IGDB.
// Provide the SetFilter functional option if you need to filter
// which ExternalGames to count.
func (es *ExternalGameService) Count(opts ...Option) (int, error) {
ct, err := es.client.getCount(es.end, opts...)
if err != nil {
return 0, errors.Wrap(err, "cannot count ExternalGames")
}
return ct, nil
}
// Fields returns the up-to-date list of fields in an
// IGDB ExternalGame object.
func (es *ExternalGameService) Fields() ([]string, error) {
f, err := es.client.getFields(es.end)
if err != nil {
return nil, errors.Wrap(err, "cannot get ExternalGame fields")
}
return f, nil
}