mirror of
https://github.com/bestnite/go-igdb.git
synced 2025-04-20 05:25:53 +08:00
43 lines
864 B
Go
43 lines
864 B
Go
package endpoint
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
pb "github.com/bestnite/go-igdb/proto"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type Keywords struct {
|
|
BaseEndpoint[pb.Keyword]
|
|
}
|
|
|
|
func NewKeywords(request RequestFunc) *Keywords {
|
|
a := &Keywords{
|
|
BaseEndpoint[pb.Keyword]{
|
|
endpointName: EPKeywords,
|
|
request: request,
|
|
},
|
|
}
|
|
a.queryFunc = a.Query
|
|
return a
|
|
}
|
|
|
|
func (a *Keywords) Query(query string) ([]*pb.Keyword, error) {
|
|
resp, err := a.request("POST", fmt.Sprintf("https://api.igdb.com/v4/%s.pb", a.endpointName), query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to request: %w", err)
|
|
}
|
|
|
|
data := pb.KeywordResult{}
|
|
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal: %w", err)
|
|
}
|
|
|
|
if len(data.Keywords) == 0 {
|
|
return nil, fmt.Errorf("no results: %s", query)
|
|
}
|
|
|
|
return data.Keywords, nil
|
|
}
|