-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transport.go
78 lines (67 loc) · 1.73 KB
/
Transport.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
package VkApi
import (
"strings"
"time"
)
type TransportExternalData map[string][]string
func (d *TransportExternalData) toString() string {
str := ""
for k, v := range *d {
if str != "" {
str += "\n"
}
str += k + "=" + strings.Join(v, ",")
}
return str
}
type Transport interface {
call(method string, params P) ([]byte, error, TransportExternalData)
}
type TransportError struct {
Method string
Params P
Response []byte
Headers TransportExternalData
ParentError error
}
func (e *TransportError) Error() string {
if e.ParentError != nil {
return e.DebugInfo(e.ParentError.Error())
} else {
return e.DebugInfo("")
}
}
func (e *TransportError) DebugInfo(prefix string) string {
var s string
if len(e.Response) <= 1000 {
s = "TransportError: " + callToString(e.Method, e.Params) + "\n" + e.Headers.toString() + "\n" + string(e.Response)
} else {
startIndex := len(e.Response) - 1000
s := string(e.Response[:1000]) + "..." + string(e.Response[startIndex:])
s = "TransportError: " + callToString(e.Method, e.Params) + "\n" + e.Headers.toString() + "\n" + s
}
if prefix != "" {
s = "Error: " + prefix + "\n" + s
}
return s
}
type FakeTransport struct {
Response []byte
Err error
ExternalData TransportExternalData
SleepTime int64
}
func (t *FakeTransport) call(method string, params P) ([]byte, error, TransportExternalData) {
if t.SleepTime > 0 {
time.Sleep(time.Duration(t.SleepTime) * time.Millisecond)
}
return t.Response, t.Err, t.ExternalData
}
type FakeTransportPoll struct {
Data []FakeTransport
}
func (t *FakeTransportPoll) call(method string, params P) ([]byte, error, TransportExternalData) {
tr := t.Data[0]
t.Data = t.Data[1:]
return tr.call(method, params)
}