-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.go
140 lines (119 loc) · 2.65 KB
/
Response.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
package VkApi
import (
"encoding/json"
"strconv"
)
type RequestedParams struct {
Key string `json:"key"`
Value string `json:"value"`
}
type ApiError struct {
Code int `json:"error_code"`
Message string `json:"error_msg"`
Params []RequestedParams `json:"request_params"`
CaptchaSid string `json:"captcha_sid"`
CaptchaImg string `json:"captcha_img"`
CallMethod *string
CallParams *P
}
func (e *ApiError) Error() string {
str := strconv.Itoa(e.Code) + " " + e.Message
if len(e.Params) > 0 {
str += "\n Params: " + requestParamsToString(e.Params)
}
if e.CallMethod != nil {
str += "\n Call method: " + string(*e.CallMethod)
}
if e.CallParams != nil {
str += "\n Call params: " + e.CallParams.toString()
}
return str
}
type Response struct {
Response *json.RawMessage `json:"response"`
Error ApiError `json:"error"`
ExecuteErrors []struct {
Method string `json:"method"`
Code int `json:"error_code"`
Message string `json:"error_msg"`
} `json:"execute_errors"`
}
func (r *Response) success() bool {
return r.Error.Code == 0
}
func (r *Response) canRetry() bool {
switch r.Error.Code {
case UnknownError:
return true
case TooManyRequests:
return true
case TooManyActions:
return true
case ServerError:
return true
case AdvError:
return true
default:
return false
}
}
func (r Response) Any() *AnyModel {
m := AnyModel(*r.Response)
return &m
}
func (r Response) Slice() ([]*AnyModel, error) {
a := r.Any()
return a.Slice()
}
func (r Response) FirstAny() (*AnyModel, error) {
a := r.Any()
s, err := a.Slice()
if err != nil {
return nil, err
}
return s[0], nil
}
func (r Response) GetString(k string) (string, error) {
a := r.Any()
return a.GetString(k)
}
func (r Response) GetInt(k string) (int, error) {
a := r.Any()
return a.GetInt(k)
}
func (r Response) GetSlice(k string) ([]*AnyModel, error) {
a := r.Any()
return a.GetSlice(k)
}
func (r *Response) QStringDef(k string, def string) string {
a := r.Any()
return a.QStringDef(k, def)
}
func (r *Response) QString(k string) *string {
a := r.Any()
return a.QString(k)
}
func (r *Response) QIntDef(k string, def int) int {
a := r.Any()
return a.QIntDef(k, def)
}
func (r *Response) QInt(k string) *int {
a := r.Any()
return a.QInt(k)
}
func (r Response) Unmarshal(i interface{}) error {
return json.Unmarshal([]byte(*r.Response), i)
}
func (r Response) String() string {
return string(*r.Response)
}
func (r Response) Int() (int, error) {
return strconv.Atoi(r.String())
}
func (r Response) IntDef(def int) int {
i, err := r.Int()
if err != nil {
return def
}
return i
}