-
Notifications
You must be signed in to change notification settings - Fork 5
/
operationHelper.go
388 lines (315 loc) · 9.38 KB
/
operationHelper.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright (c) 2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package integration
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"strings"
"time"
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/repository"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils"
)
type TestWrapperService struct {
Client *PhanthauthService
ConfigRepository repository.ConfigRepository
TokenRepository repository.TokenRepository
}
var (
OAuth20PhantAuthService = &TestWrapperService{
Client: NewClientWithBasePath(getPhantauthURL(), ""),
ConfigRepository: &ConfigRepositoryPhantAuthImpl{},
TokenRepository: &TokenRepositoryPhantAuthImpl{},
}
)
func getPhantauthURL() string {
url := os.Getenv("AB_PHANTAUTH_BASE_URL")
if strings.HasPrefix(strings.ToLower(url), "https://") {
phanauthURL := strings.TrimPrefix(url, "https://")
return phanauthURL
}
return url
}
// SECTION 1 - parameters of GET
type GetPhantauthParams struct {
Username string
Kind string
RetryPolicy *utils.Retry
timeout time.Duration
AuthInfoWriter runtime.ClientAuthInfoWriter
Context context.Context
HTTPClient *http.Client
}
func (g GetPhantauthParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error {
if err := r.SetTimeout(g.timeout); err != nil {
return err
}
var res []error
// path param kind
if err := r.SetPathParam("kind", g.Kind); err != nil {
return err
}
// path param username
if err := r.SetPathParam("username", g.Username); err != nil {
return err
}
// setting the default header value
if err := r.SetHeaderParam("User-Agent", utils.UserAgentGen()); err != nil {
return err
}
if err := r.SetHeaderParam("X-Amzn-Trace-Id", utils.AmazonTraceIDGen()); err != nil {
return err
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
// SECTION 1 - parameters of POST
type PostPhantauthParams struct {
ClientID string
ClientSecret string
RedirectURI string
Code string
GrantType string
RetryPolicy *utils.Retry
timeout time.Duration
AuthInfoWriter runtime.ClientAuthInfoWriter
Context context.Context
HTTPClient *http.Client
}
func (p *PostPhantauthParams) Validate() error {
return nil
}
func (p *PostPhantauthParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error {
if err := r.SetTimeout(p.timeout); err != nil {
return err
}
var res []error
// form param client_id
frClientID := p.ClientID
fClientID := frClientID
if fClientID != "" {
if err := r.SetFormParam("client_id", fClientID); err != nil {
return err
}
}
// form param client_secret
frClientSecret := p.ClientSecret
fClientSecret := frClientSecret
if fClientSecret != "" {
if err := r.SetFormParam("client_secret", fClientSecret); err != nil {
return err
}
}
// form param code
frCode := p.Code
fCode := frCode
if fCode != "" {
if err := r.SetFormParam("code", fCode); err != nil {
return err
}
}
// form param grant_type
frGrantType := p.GrantType
fGrantType := frGrantType
if fGrantType != "" {
if err := r.SetFormParam("grant_type", fGrantType); err != nil {
return err
}
}
// form param redirect_uri
frRedirectURI := p.RedirectURI
fRedirectURI := frRedirectURI
if fRedirectURI != "" {
if err := r.SetFormParam("redirect_uri", fRedirectURI); err != nil {
return err
}
}
// setting the default header value
if err := r.SetHeaderParam("User-Agent", utils.UserAgentGen()); err != nil {
return err
}
if err := r.SetHeaderParam("X-Amzn-Trace-Id", utils.AmazonTraceIDGen()); err != nil {
return err
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
// SECTION 2 - response of GET
type GetReader struct{}
func (o *GetReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
switch response.Code() {
case 200:
data, err := ioutil.ReadAll(response.Body())
if err != nil {
return nil, err
}
result := &Get200Error{Payload: string(data)} // custom workaround for reading text/plain Content-Type response
return result, nil
default:
data, err := ioutil.ReadAll(response.Body())
if err != nil {
return nil, err
}
return nil, fmt.Errorf("requested url returns an error %d: %s", response.Code(), string(data))
}
}
// Get200Error got renamed because linter is complaining "should conform to the `XxxError` format (errname)"
type Get200Error struct {
Payload string
}
func (o *Get200Error) Error() string {
return fmt.Sprintf("[GET /user/{username}/token/{kind}][%d] getOK %+v", 200, o.Payload)
}
func (o *Get200Error) PostPayload() string {
return o.Payload
}
// SECTION 2 - response of POST
type PostReader struct {
}
func (p *PostReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
switch response.Code() {
case 200:
result := NewPostOK()
if err := result.readResponse(response, consumer); err != nil {
return nil, err
}
return result, nil
default:
data, err := ioutil.ReadAll(response.Body())
if err != nil {
return nil, err
}
return nil, fmt.Errorf("requested url returns an error %d: %s", response.Code(), string(data))
}
}
// Post200Error got renamed because linter is complaining "should conform to the `XxxError` format (errname)"
type Post200Error struct {
Payload *OauthmodelPhantauthToken
}
func NewPostOK() *Post200Error {
return &Post200Error{}
}
func (p *Post200Error) Error() string {
return fmt.Sprintf("[POST /auth/token][%d] postOK %+v", 200, p.Payload)
}
func (p *Post200Error) PostPayload() *OauthmodelPhantauthToken {
return p.Payload
}
func (p *Post200Error) readResponse(response runtime.ClientResponse, consumer runtime.Consumer) error {
p.Payload = new(OauthmodelPhantauthToken)
// response payload
if err := consumer.Consume(response.Body(), &p.Payload); err != nil && err != io.EOF {
return err
}
return nil
}
// OauthmodelPhantauthToken is an addition for SECTION 2 - response of POST
type OauthmodelPhantauthToken struct {
AccessToken *string `json:"access_token"`
IDToken *string `json:"id_token"`
RefreshToken *string `json:"refresh_token"`
TokenType *string `json:"token_type"`
}
// SECTION 3 - service client
type PhanthauthService struct {
ClientPhantauthService ClientService
Runtime *httptransport.Runtime
Transport runtime.ClientTransport
}
func New(transport runtime.ClientTransport, runtime *httptransport.Runtime) *PhanthauthService {
cli := new(PhanthauthService)
cli.Transport = transport
cli.Runtime = runtime
cli.ClientPhantauthService = NewService(transport)
return cli
}
func NewClientWithBasePath(url string, endpoint string) *PhanthauthService {
schemes := []string{"https"}
//if strings.HasSuffix(url, ":443") { // the phantauth does not have the suffix :443, so it uses directly to https for POST endpoint
// schemes = []string{"https"}
//}
transport := httptransport.New(url, endpoint, schemes)
transport.Transport = utils.SetLogger(transport.Transport)
return New(transport, transport)
}
// SECTION 4 - operation client
func NewService(transport runtime.ClientTransport) ClientService {
return &Client{transport: transport}
}
type Client struct {
transport runtime.ClientTransport
}
type ClientService interface {
Get(params *GetPhantauthParams) (*Get200Error, error)
Post(params *PostPhantauthParams) (*Post200Error, error)
SetTransport(transport runtime.ClientTransport)
}
func (a *Client) Get(params *GetPhantauthParams) (*Get200Error, error) {
if params.Context == nil {
params.Context = context.Background()
}
result, err := a.transport.Submit(&runtime.ClientOperation{
ID: "GetPhantauth",
Method: "GET",
PathPattern: "/user/{username}/token/{kind}",
ProducesMediaTypes: []string{"text/plain"},
ConsumesMediaTypes: []string{"application/json"},
Schemes: []string{"https"},
Reader: &GetReader{},
Params: params,
Context: params.Context,
Client: params.HTTPClient,
})
if err != nil {
return nil, err
}
switch v := result.(type) {
case *Get200Error:
return v, nil
default:
return nil, fmt.Errorf("unexpected Type %v", reflect.TypeOf(v))
}
}
func (a *Client) Post(params *PostPhantauthParams) (*Post200Error, error) {
if params.Context == nil {
params.Context = context.Background()
}
result, err := a.transport.Submit(&runtime.ClientOperation{
ID: "Post",
Method: "POST",
PathPattern: "/auth/token",
ProducesMediaTypes: []string{"application/json"},
ConsumesMediaTypes: []string{"application/x-www-form-urlencoded"},
Schemes: []string{"https"},
Reader: &PostReader{},
Params: params,
Context: params.Context,
Client: params.HTTPClient,
})
if err != nil {
return nil, err
}
switch v := result.(type) {
case *Post200Error:
return v, nil
default:
return nil, fmt.Errorf("unexpected Type %v", reflect.TypeOf(v))
}
}
// SetTransport changes the transport on the client
func (a *Client) SetTransport(transport runtime.ClientTransport) {
a.transport = transport
}