/
response.go
173 lines (152 loc) · 3.6 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
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
package rest
import (
"context"
"fmt"
"strings"
"sync"
"github.com/KarpelesLab/pjson"
"github.com/KarpelesLab/typutil"
)
type Param map[string]any
type Response struct {
Result string `json:"result"` // "success" or "error" (or "redirect")
Data pjson.RawMessage `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Code int `json:"code,omitempty"` // for errors
Extra string `json:"extra,omitempty"`
Token string `json:"token,omitempty"`
Paging any `json:"paging,omitempty"`
Job any `json:"job,omitempty"`
Time any `json:"time,omitempty"`
Access any `json:"access,omitempty"`
Exception string `json:"exception,omitempty"`
RedirectUrl string `json:"redirect_url,omitempty"`
RedirectCode int `json:"redirect_code,omitempty"`
dataParsed any
dataError error
dataParse sync.Once
}
func (r *Response) ReadValue(ctx context.Context) (any, error) {
return r.Value()
}
func (r *Response) OffsetGet(ctx context.Context, key string) (any, error) {
if strings.HasPrefix(key, "@") {
switch key[1:] {
case "error":
return r.Error, nil
case "code":
return r.Code, nil
case "extra":
return r.Extra, nil
case "token":
return r.Token, nil
case "paging":
return r.Paging, nil
case "job":
return r.Job, nil
case "time":
return r.Time, nil
case "access":
return r.Access, nil
case "exception":
return r.Exception, nil
}
}
// return value
return r.Get(key)
}
// Raw is implemented as r.Value() for compatibility
func (r *Response) Raw() (any, error) {
return r.Value()
}
// FullRaw fetches the whole raw object including "Response" own data
func (r *Response) FullRaw() (map[string]any, error) {
data, err := r.Value()
if err != nil {
return nil, err
}
resp := map[string]any{"result": r.Result, "data": data}
if r.Error != "" {
resp["error"] = r.Error
}
if r.Code != 0 {
resp["code"] = r.Code
}
if r.Extra != "" {
resp["extra"] = r.Extra
}
if r.Token != "" {
resp["token"] = r.Token
}
if r.Paging != nil {
resp["paging"] = r.Paging
}
if r.Job != nil {
resp["job"] = r.Job
}
if r.Time != nil {
resp["time"] = r.Time
}
if r.Access != nil {
resp["access"] = r.Access
}
if r.Exception != "" {
resp["exception"] = r.Exception
}
if r.RedirectUrl != "" {
resp["redirect_url"] = r.RedirectUrl
}
if r.RedirectCode != 0 {
resp["redirect_code"] = r.RedirectCode
}
return resp, nil
}
func (r *Response) Apply(v any) error {
return pjson.Unmarshal(r.Data, v)
}
func (r *Response) ApplyContext(ctx context.Context, v any) error {
return pjson.UnmarshalContext(ctx, r.Data, v)
}
func (r *Response) Value() (any, error) {
r.dataParse.Do(r.ParseData)
return r.dataParsed, r.dataError
}
func (r *Response) ValueContext(ctx context.Context) (any, error) {
r.dataParse.Do(r.ParseData)
return r.dataParsed, r.dataError
}
func (r *Response) ParseData() {
r.dataError = pjson.Unmarshal(r.Data, &r.dataParsed)
}
func (r *Response) Get(v string) (any, error) {
va := strings.Split(v, "/")
cur, err := r.Value()
if err != nil {
return nil, err
}
for _, sub := range va {
if sub == "" {
continue
}
// we assume each sub will be an index in cur as a map
cur, err = typutil.OffsetGet(context.Background(), cur, sub)
if err != nil {
return cur, err
}
if cur == nil {
return nil, nil
}
}
return cur, nil
}
func (r *Response) GetString(v string) (string, error) {
res, err := r.Get(v)
if err != nil {
return "", err
}
str, ok := res.(string)
if !ok {
return fmt.Sprintf("%v", str), fmt.Errorf("unexpected type %T for string %s", res, v)
}
return str, nil
}