-
Notifications
You must be signed in to change notification settings - Fork 54
/
fetch_game.go
67 lines (55 loc) 路 1.46 KB
/
fetch_game.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
package fetch
import (
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/cmd/operate"
"github.com/itchio/butler/database/hades"
"github.com/itchio/butler/database/models"
itchio "github.com/itchio/go-itchio"
"github.com/pkg/errors"
)
func FetchGame(rc *butlerd.RequestContext, params *butlerd.FetchGameParams) (*butlerd.FetchGameResult, error) {
consumer := rc.Consumer
if params.GameID == 0 {
return nil, errors.New("gameId must be non-zero")
}
sendDBGame := func() error {
game := models.GameByID(rc.DB(), params.GameID)
if game != nil {
err := messages.FetchGameYield.Notify(rc, &butlerd.FetchGameYieldNotification{Game: game})
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
err := sendDBGame()
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Debugf("Querying API...")
creds := operate.CredentialsForGameID(rc.DB(), params.GameID)
client, err := operate.ClientFromCredentials(creds)
if err != nil {
return nil, errors.WithStack(err)
}
gameRes, err := client.GetGame(&itchio.GetGameParams{
GameID: params.GameID,
})
if err != nil {
return nil, errors.WithStack(err)
}
c := HadesContext(rc)
err = c.Save(rc.DB(), &hades.SaveParams{
Record: gameRes.Game,
})
if err != nil {
return nil, errors.WithStack(err)
}
err = sendDBGame()
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.FetchGameResult{}
return res, nil
}