-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpelotonstack.go
More file actions
154 lines (128 loc) · 3.78 KB
/
pelotonstack.go
File metadata and controls
154 lines (128 loc) · 3.78 KB
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
146
147
148
149
150
151
152
153
154
package main
import "log"
import "github.com/levigross/grequests"
type ViewUserStackResponseJSON struct {
ViewUserStackData ViewUserStackDataJSON `json:"data"`
}
type ViewUserStackDataJSON struct {
ViewUserStack ViewUserStackJSON `json:"ViewUserStack"`
}
type ViewUserStackJSON struct {
NumClasses int `json:"numClasses"`
}
type PelotonStack struct {
Endpoint string
Headers map[string]string
Session *grequests.Session
}
func NewPelotonStack(session *grequests.Session) *PelotonStack {
ps := PelotonStack{
Endpoint: "https://gql-graphql-gateway.prod.k8s.onepeloton.com/graphql",
Headers: map[string]string{
"Content-Type": "application/json",
"User-Agent": "peloton-scheduler",
"Peloton-Platform": "web",
},
Session: session,
}
return &ps
}
func (ps *PelotonStack) GetStack() {
query, err := Asset("data/queries/ViewUserStack.graphql")
if err != nil {
log.Fatalf("Unable to get ViewUserStack: ", err)
}
resp, _ := ps.Session.Post(
ps.Endpoint,
&grequests.RequestOptions{
JSON: map[string]string{
"operationName": "ViewUserStack",
"query": string(query),
},
Headers: ps.Headers,
},
)
if !resp.Ok {
log.Fatalf("Unable to get user stack: %d - %s", resp.StatusCode, resp.String())
}
viewuserstackjson := &ViewUserStackResponseJSON{}
err = resp.JSON(viewuserstackjson)
if err != nil {
log.Printf("Unable to serialize stack details JSON blob", err)
} else {
log.Printf("Number of classes in current stack: %d", viewuserstackjson.ViewUserStackData.ViewUserStack.NumClasses)
}
}
type ModifyStackRequestJSON struct {
OperationName string `json:"operationName"`
Variables ModifyStackRequestVariablesJSON `json:"variables"`
Query string `json:"query"`
}
type ModifyStackRequestVariablesJSON struct {
Input ModifyStackRequestInputJSON `json:"input"`
}
type ModifyStackRequestInputJSON struct {
PelotonClassIdList []string `json:"pelotonClassIdList"`
}
func (ps *PelotonStack) ClearStack() {
query, err := Asset("data/queries/ModifyStack.graphql")
if err != nil {
log.Fatalf("Unable to get ModifyStack.graphql: ", err)
}
clearStackRequest := ModifyStackRequestJSON{
OperationName: "ModifyStack",
Variables: ModifyStackRequestVariablesJSON{
Input: ModifyStackRequestInputJSON{
PelotonClassIdList: []string{},
},
},
Query: string(query),
}
resp, _ := ps.Session.Post(
ps.Endpoint,
&grequests.RequestOptions{
JSON: clearStackRequest,
Headers: ps.Headers,
},
)
if !resp.Ok {
log.Fatalf("Unable to clear user stack: %d - %s", resp.StatusCode, resp.String())
}
}
type AddClassToStackRequestJSON struct {
OperationName string `json:"operationName"`
Variables AddClassToStackRequestVariablesJSON `json:"variables"`
Query string `json:"query"`
}
type AddClassToStackRequestVariablesJSON struct {
Input AddClassToStackRequestInputJSON `json:"input"`
}
type AddClassToStackRequestInputJSON struct {
PelotonClassId string `json:"pelotonClassId"`
}
func (ps *PelotonStack) AddClassToStack(joinToken string) {
query, err := Asset("data/queries/AddClassToStack.graphql")
if err != nil {
log.Fatalf("Unable to get AddClassToStack.graphql: ", err)
}
addClassToStackRequest := AddClassToStackRequestJSON{
OperationName: "AddClassToStack",
Variables: AddClassToStackRequestVariablesJSON{
Input: AddClassToStackRequestInputJSON{
PelotonClassId: joinToken,
},
},
Query: string(query),
}
resp, _ := ps.Session.Post(
ps.Endpoint,
&grequests.RequestOptions{
JSON: addClassToStackRequest,
Headers: ps.Headers,
},
)
if !resp.Ok {
log.Fatalf("Unable to add class to user stack: %d - %s", resp.StatusCode, resp.String())
}
//fmt.Println("RESP:", resp.String())
}