/
types.go
126 lines (102 loc) · 3.63 KB
/
types.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
/*
Copyright 2021 The Everoute Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"encoding/json"
"fmt"
"io"
"strconv"
)
type Request struct {
Query string `json:"query"`
OperationName string `json:"operationName,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"`
}
type Response struct {
Data json.RawMessage `json:"data"`
Errors []ResponseError `json:"errors,omitempty"`
}
type ResponseError struct {
Message string `json:"message"`
Code ErrorCode `json:"code,omitempty"`
}
func (e *ResponseError) UnmarshalJSON(data []byte) error {
var object map[string]interface{}
if err := json.Unmarshal(data, &object); err != nil {
return err
}
if msg, ok := object["message"].(string); ok {
e.Message = msg
}
switch code := object["code"].(type) {
case string:
e.Code = ErrorCode(code)
case float64:
e.Code = ErrorCode(strconv.FormatFloat(code, 'f', -1, 64))
}
return nil
}
type ErrorCode string
const (
PermissionDenied ErrorCode = "PERMISSION_DENIED"
LoginFailed ErrorCode = "LOGIN_FAILED"
UserNotFound ErrorCode = "USER_NOT_FOUND"
UserPasswordIncorrect ErrorCode = "USER_PASSWORD_INCORRECT"
NotMatchUser ErrorCode = "NOT_MATCH_USER"
LoadTokenFailed ErrorCode = "LOAD_TOKEN_FAILED" // #nosec
WebsocketConnectError ErrorCode = "WEBSOCKET_CONNECT_ERROR"
)
func (e ResponseError) Error() string {
return fmt.Sprintf("message: %s, errcode: %s", e.Message, e.Code)
}
func HasAuthError(errors []ResponseError) bool {
for _, err := range errors {
switch err.Code {
case PermissionDenied, LoginFailed, UserNotFound, UserPasswordIncorrect, NotMatchUser, LoadTokenFailed:
return true
}
}
return false
}
// Message is the request/response type when use the websocket connection
type Message struct {
ID string `json:"id,omitempty"`
Type MessageType `json:"type"`
PayLoad json.RawMessage `json:"payload,omitempty"`
}
type MessageType string
const (
ConnectionInitMsg MessageType = "connection_init" // Client -> Server
ConnectionTerminateMsg MessageType = "connection_terminate" // Client -> Server
StartMsg MessageType = "start" // Client -> Server
StopMsg MessageType = "stop" // Client -> Server
ConnectionAckMsg MessageType = "connection_ack" // Server -> Client
ConnectionErrorMsg MessageType = "connection_error" // Server -> Client
DataMsg MessageType = "data" // Server -> Client
ErrorMsg MessageType = "error" // Server -> Client
CompleteMsg MessageType = "complete" // Server -> Client
ConnectionKeepAliveMsg MessageType = "ka" // Server -> Client
)
type UserInfo struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
Source string `json:"source" yaml:"source"`
}
// Upload scalar to add support for GraphQL multipart requests
type Upload struct {
FileName string
File io.Reader
}
func (*Upload) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}