forked from Henry-Sarabia/igdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.go
145 lines (120 loc) · 3.71 KB
/
website.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
package igdb
import (
"encoding/json"
"github.com/Henry-Sarabia/sliceconv"
"github.com/pkg/errors"
"strconv"
)
//go:generate gomodifytags -file $GOFILE -struct Website -add-tags json -w
// Website represents a website and its URL; usually associated with a game.
// For more information visit: https://api-docs.igdb.com/#website
type Website struct {
ID int `json:"id"`
Category WebsiteCategory `json:"category"`
Trusted bool `json:"trusted"`
URL string `json:"url"`
}
// WebsiteCategory specifies a specific popular website.
type WebsiteCategory int
//go:generate stringer -type=WebsiteCategory
// Expected WebsiteCategory enums from the IGDB.
const (
WebsiteOfficial WebsiteCategory = iota + 1
WebsiteWikia
WebsiteWikipedia
WebsiteFacebook
WebsiteTwitter
WebsiteTwitch
_
WebsiteInstagram
WebsiteYoutube
WebsiteIphone
WebsiteIpad
WebsiteAndroid
WebsiteSteam
WebsiteReddit
WebsiteDiscord
WebsiteGooglePlus
WebsiteTumblr
WebsiteLinkedin
WebsitePinterest
WebsiteSoundcloud
)
// WebsiteService handles all the API calls for the IGDB Website endpoint.
type WebsiteService service
// Get returns a single Website 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 Websites, an error is returned.
func (ws *WebsiteService) Get(id int, opts ...Option) (*Website, error) {
if id < 0 {
return nil, ErrNegativeID
}
var web []*Website
opts = append(opts, SetFilter("id", OpEquals, strconv.Itoa(id)))
err := ws.client.post(ws.end, &web, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Website with ID %v", id)
}
return web[0], nil
}
// List returns a list of Websites 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 Website is ignored. If none of the IDs
// match a Website, an error is returned.
func (ws *WebsiteService) List(ids []int, opts ...Option) ([]*Website, error) {
for len(ids) < 1 {
return nil, ErrEmptyIDs
}
for _, id := range ids {
if id < 0 {
return nil, ErrNegativeID
}
}
var web []*Website
opts = append(opts, SetFilter("id", OpContainsAtLeast, sliceconv.Itoa(ids)...))
err := ws.client.post(ws.end, &web, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Websites with IDs %v", ids)
}
return web, nil
}
// Index returns an index of Websites based solely on the provided functional
// options used to sort, filter, and paginate the results. If no Websites can
// be found using the provided options, an error is returned.
func (ws *WebsiteService) Index(opts ...Option) ([]*Website, error) {
var web []*Website
err := ws.client.post(ws.end, &web, opts...)
if err != nil {
return nil, errors.Wrap(err, "cannot get index of Websites")
}
return web, nil
}
// Count returns the number of Websites available in the IGDB.
// Provide the SetFilter functional option if you need to filter
// which Websites to count.
func (ws *WebsiteService) Count(opts ...Option) (int, error) {
ct, err := ws.client.getCount(ws.end, opts...)
if err != nil {
return 0, errors.Wrap(err, "cannot count Websites")
}
return ct, nil
}
// Fields returns the up-to-date list of fields in an
// IGDB Website object.
func (ws *WebsiteService) Fields() ([]string, error) {
f, err := ws.client.getFields(ws.end)
if err != nil {
return nil, errors.Wrap(err, "cannot get Website fields")
}
return f, nil
}
type WebsiteWrapper struct {
Website
}
func (w *WebsiteWrapper) UnmarshalJSON(data []byte) error {
if id, err := strconv.Atoi(string(data)); err == nil {
w.ID = id
return nil
}
return json.Unmarshal(data, &w.Website)
}