mirror of
https://github.com/bestnite/go-igdb.git
synced 2025-04-20 00:45:54 +08:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package igdb
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
pb "github/bestnite/go-igdb/proto"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func (g *igdb) GetFranchises(query string) ([]*pb.Franchise, error) {
|
|
resp, err := g.Request("https://api.igdb.com/v4/franchises.pb", query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to request: %w", err)
|
|
}
|
|
|
|
data := pb.FranchiseResult{}
|
|
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal: %w", err)
|
|
}
|
|
|
|
if len(data.Franchises) == 0 {
|
|
return nil, fmt.Errorf("no results: %s", query)
|
|
}
|
|
|
|
return data.Franchises, nil
|
|
}
|
|
|
|
func (g *igdb) GetFranchiseByID(id uint64) (*pb.Franchise, error) {
|
|
query := fmt.Sprintf(`where id=%d; fields *;`, id)
|
|
franchises, err := g.GetFranchises(query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return franchises[0], nil
|
|
}
|
|
|
|
func (g *igdb) GetFranchisesByIDs(ids []uint64) ([]*pb.Franchise, error) {
|
|
idStrSlice := make([]string, len(ids))
|
|
for i, id := range ids {
|
|
idStrSlice[i] = fmt.Sprintf("%d", id)
|
|
}
|
|
|
|
idStr := fmt.Sprintf(`where id = (%s); fields *;`, strings.Join(idStrSlice, ","))
|
|
|
|
return g.GetFranchises(idStr)
|
|
}
|