-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.go
146 lines (119 loc) · 2.83 KB
/
search.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
package tcabcireadgoclient
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type OrderBy string
func (t OrderBy) IsValid() bool {
switch t {
case ASC:
return true
case DESC:
return true
default:
return false
}
}
const (
ASC OrderBy = "ASC"
DESC OrderBy = "DESC"
)
type HeightOperator string
func (ho HeightOperator) IsValid() bool {
switch ho {
case Equal:
return true
case Less:
return true
case Greater:
return true
case EqualOrLess:
return true
case EqualOrGreater:
return true
default:
return false
}
}
const (
Equal HeightOperator = "="
Less HeightOperator = "<"
Greater HeightOperator = ">"
EqualOrLess HeightOperator = "<="
EqualOrGreater HeightOperator = ">="
)
type Search struct {
Limit uint `json:"limit"`
Height uint64 `json:"-"`
Offset uint64 `json:"offset"`
MaxHeight uint64 `json:"max_height"`
LastOrder uint64 `json:"last_order"`
Type Type `json:"typ,omitempty"`
PHeight string `json:"height,omitempty"`
OrderBy OrderBy `json:"order_by,omitempty"`
OrderField string `json:"order_field,omitempty"`
HeightOperator HeightOperator `json:"-"`
RecipientAddresses []string `json:"recipient_addrs,omitempty"`
SenderAddresses []string `json:"sender_addrs,omitempty"`
Hashes []string `json:"hashes,omitempty"`
}
func (s *Search) URI() string {
return "/v1/tx_search/p"
}
func (s *Search) IsValid() bool {
if s.Height < 0 {
return false
}
if !s.HeightOperator.IsValid() {
return false
}
if len(s.RecipientAddresses) > 251 {
return false
}
if len(s.SenderAddresses) > 251 {
return false
}
if len(s.Hashes) > 100 {
return false
}
if s.Type != "" && !s.Type.IsValid() {
return false
}
if s.Limit > 100 {
return false
}
if s.OrderBy != "" && !s.OrderBy.IsValid() {
return false
}
return true
}
func (s *Search) ToJSON() ([]byte, error) {
s.PHeight = fmt.Sprintf("%s %d", s.HeightOperator, s.Height)
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(s); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *Search) ToRequest() (*http.Request, error) {
b, err := s.ToJSON()
if err != nil {
return nil, err
}
return http.NewRequest(http.MethodPost, "", bytes.NewReader(b))
}
type SearchResponse struct {
TXS []*Transaction `json:"data"`
TotalCount uint64 `json:"total_count"`
}
type Response struct {
Data interface{} `json:"data"`
TotalCount uint64 `json:"total_count"`
Error bool `json:"error"`
Errors map[string]string `json:"errors"`
Detail string `json:"detail"`
}