-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
256 lines (220 loc) · 7.38 KB
/
session.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package nestri
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"github.com/nestrilabs/nestri-go-sdk/internal/apijson"
"github.com/nestrilabs/nestri-go-sdk/internal/param"
"github.com/nestrilabs/nestri-go-sdk/internal/requestconfig"
"github.com/nestrilabs/nestri-go-sdk/option"
"github.com/nestrilabs/nestri-go-sdk/shared"
"github.com/tidwall/gjson"
)
// SessionService contains methods and other services that help with interacting
// with the nestri API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewSessionService] method instead.
type SessionService struct {
Options []option.RequestOption
Active *SessionActiveService
}
// NewSessionService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewSessionService(opts ...option.RequestOption) (r *SessionService) {
r = &SessionService{}
r.Options = opts
r.Active = NewSessionActiveService(opts...)
return
}
// Create a new gaming session for the currently authenticated user, enabling them
// to play a game
func (r *SessionService) New(ctx context.Context, body SessionNewParams, opts ...option.RequestOption) (res *SessionNewResponse, err error) {
opts = append(r.Options[:], opts...)
path := "sessions"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Fetches detailed information about a specific gaming session using its unique id
func (r *SessionService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *SessionGetResponse, err error) {
opts = append(r.Options[:], opts...)
if id == "" {
err = errors.New("missing required id parameter")
return
}
path := fmt.Sprintf("sessions/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// This endpoint allows a user to terminate an active gaming session by providing
// the session's unique ID
func (r *SessionService) Delete(ctx context.Context, id string, opts ...option.RequestOption) (res *SessionDeleteResponse, err error) {
opts = append(r.Options[:], opts...)
if id == "" {
err = errors.New("missing required id parameter")
return
}
path := fmt.Sprintf("sessions/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return
}
type SessionNewResponse struct {
Data SessionNewResponseData `json:"data,required"`
JSON sessionNewResponseJSON `json:"-"`
}
// sessionNewResponseJSON contains the JSON metadata for the struct
// [SessionNewResponse]
type sessionNewResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *SessionNewResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r sessionNewResponseJSON) RawJSON() string {
return r.raw
}
type SessionNewResponseData string
const (
SessionNewResponseDataOk SessionNewResponseData = "ok"
)
func (r SessionNewResponseData) IsKnown() bool {
switch r {
case SessionNewResponseDataOk:
return true
}
return false
}
type SessionGetResponse struct {
// Represents a single game play session, tracking its lifetime and accessibility
// settings.
Data SessionGetResponseData `json:"data,required"`
JSON sessionGetResponseJSON `json:"-"`
}
// sessionGetResponseJSON contains the JSON metadata for the struct
// [SessionGetResponse]
type sessionGetResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *SessionGetResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r sessionGetResponseJSON) RawJSON() string {
return r.raw
}
// Represents a single game play session, tracking its lifetime and accessibility
// settings.
type SessionGetResponseData struct {
// Unique object identifier. The format and length of IDs may change over time.
ID string `json:"id,required"`
// If true, the session is publicly viewable by all users. If false, only
// authorized users can access it
Public bool `json:"public,required"`
// The timestamp indicating when this session started.
StartedAt SessionGetResponseDataStartedAtUnion `json:"startedAt,required"`
// The timestamp indicating when this session was completed or terminated. Null if
// session is still active.
EndedAt SessionGetResponseDataEndedAtUnion `json:"endedAt"`
JSON sessionGetResponseDataJSON `json:"-"`
}
// sessionGetResponseDataJSON contains the JSON metadata for the struct
// [SessionGetResponseData]
type sessionGetResponseDataJSON struct {
ID apijson.Field
Public apijson.Field
StartedAt apijson.Field
EndedAt apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *SessionGetResponseData) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r sessionGetResponseDataJSON) RawJSON() string {
return r.raw
}
// The timestamp indicating when this session started.
//
// Union satisfied by [shared.UnionString] or [shared.UnionFloat].
type SessionGetResponseDataStartedAtUnion interface {
ImplementsSessionGetResponseDataStartedAtUnion()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*SessionGetResponseDataStartedAtUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
apijson.UnionVariant{
TypeFilter: gjson.Number,
Type: reflect.TypeOf(shared.UnionFloat(0)),
},
)
}
// The timestamp indicating when this session was completed or terminated. Null if
// session is still active.
//
// Union satisfied by [shared.UnionString] or [shared.UnionFloat].
type SessionGetResponseDataEndedAtUnion interface {
ImplementsSessionGetResponseDataEndedAtUnion()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*SessionGetResponseDataEndedAtUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
apijson.UnionVariant{
TypeFilter: gjson.Number,
Type: reflect.TypeOf(shared.UnionFloat(0)),
},
)
}
type SessionDeleteResponse struct {
Data SessionDeleteResponseData `json:"data,required"`
JSON sessionDeleteResponseJSON `json:"-"`
}
// sessionDeleteResponseJSON contains the JSON metadata for the struct
// [SessionDeleteResponse]
type sessionDeleteResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *SessionDeleteResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r sessionDeleteResponseJSON) RawJSON() string {
return r.raw
}
type SessionDeleteResponseData string
const (
SessionDeleteResponseDataOk SessionDeleteResponseData = "ok"
)
func (r SessionDeleteResponseData) IsKnown() bool {
switch r {
case SessionDeleteResponseDataOk:
return true
}
return false
}
type SessionNewParams struct {
// Whether the session is publicly viewable by all users. If false, only authorized
// users can access it
Public param.Field[bool] `json:"public,required"`
}
func (r SessionNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}